解题代码
#include<iostream>
#define N 32
using namespace std;
int n;
int a[N][N];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
void dfs(int x, int y){
a[x][y] = 2;
for(int i = 0; i < 4; i++){
int xx = x + dx[i], yy = y + dy[i];
if(xx >= 0 && xx <= n+1 && yy >= 0 && yy <= n+1 && a[xx][yy] != 1 && a[xx][yy] != 2){
dfs(xx, yy);
}
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
dfs(0, 0);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
switch (a[i][j]){
case 0: cout << 2 << ' '; break;
case 1: cout << 1 << ' '; break;
case 2: cout << 0 << ' '; break;
}
}
cout << endl;
}
return 0;
}