sky_233 @ 2024-03-30 11:45:49
#include<bits/stdc++.h>
using namespace std;
int n;
int mm[33][33];
bool st[33][33];
int ws1[4] = {-1,0,1,0};
int ad1[4] = {0,1,0,-1};
void dfs(int x,int y){
if(x<0||x>n+1||y<0||y>n+1)return;
for(int i=0;i<4;i++){
int a = x+ws1[i];
int b = y+ad1[i];
if(st[a][b]!=true&&mm[a][b]==0){
st[a][b] = true;
dfs(a,b);
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>mm[i][j];
}
}
dfs(0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(mm[i][j]==0&&st[i][j]==false){
mm[i][j]=2;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<mm[i][j]<<" ";
}cout<<endl;
}
return 0;
}
by Lg2307 @ 2024-03-30 12:06:08
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> >a(32,vector<int>(32));
void dfs(int x,int y){
if (x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1){
if (a[x][y] == 1 || a[x][y] == 3)
return;
else {
a[x][y] = 3;
dfs(x - 1,y);
dfs(x + 1,y);
dfs(x,y - 1);
dfs(x,y + 1);
}
}
}
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 ++){
if (a[i][j] == 3)
a[i][j] = 0;
else if (a[i][j] == 0)
a[i][j] = 2;
cout << a[i][j] << " ";
}
cout << "\n";
}
return 0;
}
@sky_233
by sky_233 @ 2024-03-30 17:13:48
@Lg2307 好的谢谢大佬