dingyd_ @ 2024-08-15 10:27:39
#include<bits/stdc++.h>
using namespace std;
int n;
int c[40][40];
void dfs(int i,int j){
if(i<0||j<0||i>n+1||j>n+1||c[i][j]==1) return ;
if(c[i][j]==2){
c[i][j] = 0;
}
dfs(i+1,j);
dfs(i-1,j);
dfs(i,j+1);
dfs(i,j-1);
return ;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>c[i][j];
if(c[i][j]==0){
c[i][j] = 2;
}
}
}
dfs(0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by _JTG_rabbit_ @ 2024-08-15 10:39:58
@dingyd_ dfs写溢出了
by 16squareZ @ 2024-08-15 10:44:50
if(i<0||j<0||i>n+1||j>n+1||c[i][j]==1||c[i][j]==0) return ;
//c[i][j]==0 则说明该位置已被搜索过了
//不加它可能会导致 无穷递归MLE
by dingyd_ @ 2024-08-15 11:07:35
@16squareZ 感谢大佬%%%