Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n = 4;
  6. int m = 6;
  7. int matrix[100][100] = { {0, 0, 0, 1, 1, 2},
  8.                          {2, 0, 0, 0, 1, 0},
  9.                          {2, 0, 1, 0, 0, 0},
  10.                          {0, 0, 1, 0, 0, 2}
  11.                         };
  12. bool visited[100][100];
  13.  
  14. const int dx[4] = {0, 0, 1, -1};
  15. const int dy[4] = {1, -1, 0 , 0};
  16.  
  17. int inside(int i, int j)
  18. {
  19.     return i >= 0 && i < n && j >= 0 && j < m;
  20. }
  21.  
  22. void Fill(int i, int j, int elem)
  23. {
  24.     visited[i][j] = true;
  25.     for (int k = 0; k <= 3; ++k)
  26.         if (inside(i + dx[k], j + dy[k]) && matrix[i + dx[k]][j + dy[k]] == elem)
  27.             Fill(i + dx[k], j + dy[k], elem);
  28. }
  29.  
  30. int CountAreas(int left1, int right1, int n, int m, int elem)
  31. {
  32.     cout << "(" << left1 << "," << right1 << ")" << " (" << n << "," << m << ")\n";
  33.     int l = n >> 1;
  34.     int c  = m >> 1;
  35.     cout << "l = " << l << " c = " << c << endl << endl;
  36.     if (left1 < n || right1 < m )
  37.     {
  38.         int z1 = CountAreas(left1, right1, l, c, elem); /// Z1
  39.         int z2 = CountAreas(left1, c + 1, l, m, elem); /// Z2
  40.         int z3 = CountAreas(l + 1, right1, n, c, elem); /// Z3
  41.         int z4 = CountAreas(l + 1, c + 1, n, m, elem); /// Z4
  42.         return z1 + z2 + z3 + z4;
  43.     }
  44.     else
  45.     {
  46.       //  cout << "Here with " << "(" << left1 << ""
  47.         if (!visited[left1][right1] && matrix[left1][right1] == elem)
  48.         {
  49.             Fill(left1, right1, elem);
  50.             return 1;
  51.         }
  52.         return 0;
  53.     }
  54. }
  55.  
  56. int GetWinner()
  57. {
  58.     int player1 = CountAreas(0, 0, n - 1, m - 1, 1);
  59.     int player2 = CountAreas(0, 0, n - 1, m - 1, 2);
  60.  
  61.     return player1 > player2 ? 1 : 2;
  62. }
  63.  
  64. int main()
  65. {
  66.  
  67.     for (int i = 0; i < n; ++i, cout << endl)
  68.         for (int j = 0; j < m; ++j)
  69.             cout << matrix[i][j] << " ";
  70.  
  71.     cout << endl << endl;
  72.     cout << GetWinner();
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement