Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 110;
- int Marc[MAXN][MAXN], A[MAXN][MAXN];
- int dx[] = {0, 1, 0,-1};
- int dy[] = {1, 0,-1, 0};
- bool OutOfRange(int x, int y, int N, int M)
- {
- if (x <= 0 || x > N) return true;
- if (y <= 0 || y > M) return true;
- return false;
- }
- void dfs(int x, int y, int N, int M)
- {
- Marc[x][y] = 1;
- for (int k = 0; k < 4; k++)
- {
- int hx = x + dx[k];
- int hy = y + dy[k];
- if (OutOfRange(hx, hy, N, M)) continue;
- if (Marc[hx][hy] == 1) continue;
- if (A[x][y] + Marc[x][y] != A[hx][hy]) continue;
- dfs(hx, hy, N, M);
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int T;
- cin >> T;
- for (int t = 0; t < T; t++)
- {
- if (t > 0) cout << '\n';
- int N, M;
- cin >> N >> M;
- for (int i = 1; i <= N; i++)
- {
- for (int j = 1; j <= M; j++)
- {
- cin >> A[i][j];
- Marc[i][j] = 0;
- }
- }
- for (int i = 1; i <= N; i++)
- {
- for (int j = 1; j <= M; j++)
- {
- for (int k = 0; k < 4; k++)
- {
- int hx = i + dx[k];
- int hy = j + dy[k];
- if (OutOfRange(hx, hy, N, M)) continue;
- if (Marc[hx][hy]) continue;
- if (A[i][j] + Marc[i][j] != A[hx][hy] + Marc[hx][hy]) continue;
- dfs(hx, hy, N, M);
- }
- }
- }
- for (int i = 1; i <= N; i++)
- {
- for (int j = 1; j <= M; j++)
- {
- cout << A[i][j] + Marc[i][j] << " ";
- }
- cout << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement