Advertisement
Guest User

Untitled

a guest
May 24th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. const int COLOR[3] = {1, 2, 3}; //Red, Blue, Purple
  6. int Desk[6][4] = {0};
  7. ofstream out("DRAFT.OUT");
  8.  
  9. void print(int row, int col) {
  10.     int R = 0, B = 0, P = 0;
  11.     for (int i = 0; i < row; ++i) {
  12.         for (int j = 0; j <  col; ++j) {
  13.             out << Desk[i][j] << " ";
  14.             if (Desk[i][j] == COLOR[0]) ++R; //Red
  15.             else if (Desk[i][j] == COLOR[1]) ++B; //Blue
  16.             else ++P; //Purple
  17.         }
  18.         out << endl;
  19.     }
  20.     out << "Red: " << R << " Blue: " << B << " Purple: " << P << endl;
  21. }
  22.  
  23. bool canPlace(int color, int row, int col) {
  24.     return (row-1 > -1 && Desk[row-1][col] != color)
  25.         && (col-1 > -1 && Desk[row][col-1] != color);
  26. }
  27.  
  28. void solve(int row, int col, int p = 0) {
  29.     if (p >= row * col) {
  30.         print(row, col);
  31.         return;
  32.     }
  33.  
  34.     int r = p / col, c = p % col;
  35.     for (int i = 0; i < 3; ++i) {
  36.         if ( canPlace(COLOR[i], r, c) ) {
  37.             Desk[r][c] = COLOR[i];
  38.             solve(row, col, p + 1);
  39.             Desk[r][c] = 0;
  40.         }
  41.     }
  42. }
  43.  
  44. int main (void) {
  45.     solve(6, 4);
  46.     out.close();
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement