提交时间:2025-06-09 14:41:17

运行 ID: 38027

#include<bits/stdc++.h> using namespace std; const int N = 1005; bool mp[N][N][4], book[N][N]; int n, xx, yy, mx[N][N], mem, ans[N][N], asx[N * N], asy[N * N], cnt, cg[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; inline bool dfs(int x, int y) { if(book[x][y]) return 0; if(x == n && y == yy) { //cout << 111111 << endl; mem = n; asx[0] = cnt + 1, asx[cnt + 1] = x, asy[cnt + 1] = y; ans[x][y] = 1; return 1; } ++cnt; book[x][y] = 1; //cout << x << " " << y << endl; for(int i = 3; ~i; --i) { if(mp[x][y][i]) { int nx = x + cg[i][0], ny = y + cg[i][1]; if(nx > 0 && nx <= n && ny > 0 && ny <= n) { if(dfs(nx, ny)) { ans[x][y] = 1, mx[x][y] = mem; if(mx[x][y] < x) ans[x][y] = 2; mem = min(mem, x); asx[cnt] = x, asy[cnt] = y; --cnt; return 1; } } } } return 0; } inline void dfs3(int x, int y, int zx, int zx2) { for(int i = 3; ~i; --i) { if(mp[x][y][i]) { int nx = x + cg[i][0], ny = y + cg[i][1]; if(nx > 0 && nx <= n && ny > 0 && ny <= n && !ans[nx][ny]) { if(nx < zx2) continue; if(nx >= zx) ans[nx][ny] = 2; else ans[nx][ny] = 1; dfs3(nx, ny, zx, zx2); } } } } inline void dfs2() { int zx = n + 5, zx2 = n + 5; for(int i = asx[0] - 1; i; --i) { int nx = asx[i], ny = asy[i]; zx2 = min(nx, zx2); if(ans[nx][ny] == 2) zx = min(zx, nx); if(asx[i + 1] == asx[i] + 1 && (ans[nx + 1][ny] ^ 2)) continue; else if(i == asx[0]) continue; dfs3(nx, ny, min(zx, nx + 1), zx2); } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) printf("%d ", ans[i][j]); putchar('\n'); } } int main() { scanf("%d%d%d", &n, &xx, &yy); for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) { int aa; scanf("%d", &aa); if(!aa) mp[i][j][0] = mp[i - 1][j][2] = 1; } for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) { int aa; scanf("%d", &aa); if(!aa) mp[i][j][3] = mp[i][j - 1][1] = 1; } // for(int i = 1; i <= n; ++i) { // for(int j = 1; j <= n; ++j) cout << i << " " << j << " " << mp[i][j][0] << " " << mp[i][j][1] << " " << mp[i][j][2] << " " << mp[i][j][3] << endl; // } dfs(1, xx); //cout << "wcnm" << endl; dfs2(); return 0; }