2 solutions
-
0
#include<queue> using namespace std; typedef pair<int,int> PAII; const int N=50; int g[N][N]; int n; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; void bfs(int x,int y) { queue<PAII> q; q.push({x,y}); while(q.size()) { auto t=q.front(); q.pop(); int a=t.first,b=t.second; for(int i=0;i<4;i++) { int xx=a+dx[i]; int yy=b+dy[i]; if(xx>=0&&yy>=0&&xx<=n+1&&yy<=n+1&&g[xx][yy]==0&&g[xx][yy]!=-1) { g[xx][yy]=-1; q.push({xx,yy}); } } } } int main(){ cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>g[i][j]; bfs(0,0); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(g[i][j]==-1) cout<<"0 "; if(!g[i][j]) cout<<"2 "; else if(g[i][j]==1) cout<<g[i][j]<<" "; } cout<<endl; } return 0; }
Information
- ID
- 1138
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 7
- Tags
- # Submissions
- 13
- Accepted
- 9
- Uploaded By