违规用户名J3c#3U1x @ 2023-08-16 11:52:28
#include <iostream>
using namespace std;
/*
Happy.Sun
*/
int n;
int a[35][35];
int vis[35][35];
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
void dfs(int x,int y){
if(x<-1||y<-1||x>n||y>n||vis[x][y]!=0) return ;
vis[x][y]=1;
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
dfs(xx,yy);
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j]==1) vis[i][j]=2;
}
}
dfs(0,0);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(vis[i][j]==0) cout<<2;
else cout<<a[i][j];
cout<<" ";
}
cout<<endl;
}
return 0;
}
by ABCgfed @ 2023-08-16 12:09:12
@Happy_Sun AC了
#include <iostream>
using namespace std;
int n;
int a[35][35];
int vis[35][35];
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
void dfs(int x,int y){
if(x<-1||y<-1||x>n||y>n||vis[x][y]!=0) return ;
vis[x][y]=1;
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
dfs(xx,yy);
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j]==1) vis[i][j]=2;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==0||j==0||i==n-1||j==n-1){//四面都有可能有圈外0
dfs(i,j);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(vis[i][j]==0) cout<<2;
else cout<<a[i][j];
cout<<" ";
}
cout<<endl;
}
return 0;
}
by 违规用户名J3c#3U1x @ 2023-08-16 14:06:00
@ABCgfed 谢谢大佬,我明白了