tle on #4

P1162 填涂颜色

@[高远哲](/user/384498) @[fast_photon](/user/302805)
by esquigybcu @ 2021-06-19 15:56:14


``` #include<bits/stdc++.h> using namespace std; const int maxn=50; struct node{ int x,y; }p; int n,a[maxn][maxn]; int X[4]={0,0,1,-1}; int Y[4]={1,-1,0,0}; bool inq[maxn][maxn]={false}; bool judge(int x,int y){ if(x<0||x>n+1||y<0||y>n+1) return false; else if(inq[x][y]==true||a[x][y]==1) return false; return true; } void BFS(){ queue<node> q; q.push(p); while(!q.empty()){ node top=q.front(); q.pop(); for(int i=0;i<4;i++){ int newX=top.x+X[i]; int newY=top.y+Y[i]; if(judge(newX,newY)){ node s; s.x=newX,s.y=newY; q.push(s); inq[newX][newY]=true; } } } } int main(){ cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j]; } } p.x=0,p.y=0; BFS(); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(a[i][j]!=0) inq[i][j]=true; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(inq[i][j]==false) cout<<2<<" "; else cout<<a[i][j]<<" "; } cout<<endl; } return 0; } ```
by zhangruozhong @ 2021-06-19 15:56:51


ACed. 把`dfs`里的`vis[i][j] = false;`删掉就行。
by esquigybcu @ 2021-06-19 16:10:35


|