luogu P1162 填涂颜色
https://www.luogu.org/problem/show?pid=1162
//其实很简单的吧 //就是最外圈加一圈0 ,然后把外圈里面的0都遍历了 //剩下的0 就变成2 就行了 #include<bits/stdc++.h> using namespace std; typedef pair <int,int> pii; int n ,s[50][50]; bool vis[50][50]; int fx[]={-1,0,0,1}; int fy[]={0,-1,1,0}; bool check(int x,int y) { if(!vis[x][y] && s[x][y] == 0 && x>=0 && x<=n+1 && y>=0 && y<=n+1) return 1; return 0; } void bfs(int x,int y) { queue<pii > que; que.push({x,y}); //cout<<"yes1"<<endl; while (que.size() ) { pii now = que.front(); que.pop(); if( vis[now.first][now.second] ) continue; vis[now.first][now.second] = 1;//注意该点已经访问过 这里很容易忘记的吧 //cout<<"yes2"<<endl; for(int i=0;i<4;i++) { //cout<<"yes3"<<endl; int dx= now.first+fx[i]; int dy= now.second+fy[i]; if(check(dx,dy)) { //cout<<"yes3"<<endl; que.push({dx,dy}); } } } } int main () { scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&s[i][j]); //cout<<endl; //因为外圈已经本身就是0了 //从第0行到第n+1行 bfs(0,0); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if( vis[i][j] ) cout<<0<<" "; else if(s[i][j] == 1) cout<<1<<" "; else cout<<2<<" "; } cout<<endl; } }