Advertisement
ThegeekKnight16

Engineer Artem

Mar 14th, 2022 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 110;
  4. int Marc[MAXN][MAXN], A[MAXN][MAXN];
  5.  
  6. int dx[] = {0, 1, 0,-1};
  7. int dy[] = {1, 0,-1, 0};
  8.  
  9. bool OutOfRange(int x, int y, int N, int M)
  10. {
  11.     if (x <= 0 || x > N) return true;
  12.     if (y <= 0 || y > M) return true;
  13.     return false;
  14. }
  15.  
  16. void dfs(int x, int y, int N, int M)
  17. {
  18.     Marc[x][y] = 1;
  19.    
  20.     for (int k = 0; k < 4; k++)
  21.     {
  22.         int hx = x + dx[k];
  23.         int hy = y + dy[k];
  24.         if (OutOfRange(hx, hy, N, M)) continue;
  25.         if (Marc[hx][hy] == 1) continue;
  26.         if (A[x][y] + Marc[x][y] != A[hx][hy]) continue;
  27.         dfs(hx, hy, N, M);
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     ios_base::sync_with_stdio(false);
  34.     cin.tie(NULL);
  35.     int T;
  36.     cin >> T;
  37.     for (int t = 0; t < T; t++)
  38.     {
  39.         if (t > 0) cout << '\n';
  40.         int N, M;
  41.         cin >> N >> M;
  42.         for (int i = 1; i <= N; i++)
  43.         {
  44.             for (int j = 1; j <= M; j++)
  45.             {
  46.                 cin >> A[i][j];
  47.                 Marc[i][j] = 0;
  48.             }
  49.         }
  50.        
  51.         for (int i = 1; i <= N; i++)
  52.         {
  53.             for (int j = 1; j <= M; j++)
  54.             {
  55.                 for (int k = 0; k < 4; k++)
  56.                 {
  57.                     int hx = i + dx[k];
  58.                     int hy = j + dy[k];
  59.                     if (OutOfRange(hx, hy, N, M)) continue;
  60.                     if (Marc[hx][hy]) continue;
  61.                     if (A[i][j] + Marc[i][j] != A[hx][hy] + Marc[hx][hy]) continue;
  62.                     dfs(hx, hy, N, M);
  63.                 }
  64.             }
  65.         }
  66.        
  67.         for (int i = 1; i <= N; i++)
  68.         {
  69.             for (int j = 1; j <= M; j++)
  70.             {
  71.                 cout << A[i][j] + Marc[i][j] << " ";
  72.             }
  73.             cout << '\n';
  74.         }
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement