|
198个广搜
- #include<iostream>
- using namespace std;
- #define FOR(iii,nn,mm) for(int iii=nn;iii<=mm;iii++)
- int dr[4]={-1,0,1,0};
- int dc[4]={0,-1,0,1};
- int q[2600][2];
- int a[51][51];
- bool b[51][51];
- int t,r,c,n,head,tail;
- int main()
- {
- cin>>n;
- FOR(i,0,n-1) FOR(j,0,n-1) cin>>a[i][j];
- FOR(i,0,n-1)if((!b[0][i])&&a[0][i]==0)
- {r=0;c=i;q[1][0]=r;q[1][1]=c;b[r][c]=1;head=0;tail=1;do{
- head++;r=q[head][0];c=q[head][1];FOR(k,0,3)
- if(r+dr[k]<=n-1&&c+dc[k]<=n-1&&(!b[r+dr[k]][c+dc[k]])&&a[r+dr[k]][c+dc[k]]==0)
- {tail++;b[r+dr[k]][c+dc[k]]=1;q[tail][0]=r+dr[k];q[tail][1]=c+dc[k];}
- }while(head<tail);}
- FOR(i,0,n-1)if((!b[n-1][i])&&a[n-1][i]==0)
- {r=n-1;c=i;q[1][0]=r;q[1][1]=c;b[r][c]=1;head=0;tail=1;do{
- head++;r=q[head][0];c=q[head][1];FOR(k,0,3)
- if(r+dr[k]<=n-1&&c+dc[k]<=n-1&&(!b[r+dr[k]][c+dc[k]])&&a[r+dr[k]][c+dc[k]]==0)
- {tail++;b[r+dr[k]][c+dc[k]]=1;q[tail][0]=r+dr[k];q[tail][1]=c+dc[k];}
- }while(head<tail);}
- FOR(i,1,n-2) if((!b[i][0])&&a[i][0]==0)
- {r=i;c=0;q[1][0]=r;q[1][1]=c;b[r][c]=1;head=0;tail=1;do{
- head++;r=q[head][0];c=q[head][1];FOR(k,0,3)
- if(r+dr[k]<=n-1&&c+dc[k]<=n-1&&(!b[r+dr[k]][c+dc[k]])&&a[r+dr[k]][c+dc[k]]==0)
- {tail++;b[r+dr[k]][c+dc[k]]=1;q[tail][0]=r+dr[k];q[tail][1]=c+dc[k];}
- }while(head<tail);}
- FOR(i,1,n-2)if((!b[i][n-1])&&a[i][n-1]==0)
- {r=i;c=n-1;q[1][0]=r;q[1][1]=c;b[r][c]=1;head=0;tail=1;do{
- head++;r=q[head][0];c=q[head][1];FOR(k,0,3)
- if(r+dr[k]<=n-1&&c+dc[k]<=n-1&&(!b[r+dr[k]][c+dc[k]])&&a[r+dr[k]][c+dc[k]]==0)
- {tail++;b[r+dr[k]][c+dc[k]]=1;q[tail][0]=r+dr[k];q[tail][1]=c+dc[k];}
- }while(head<tail);}
- ///FOR(i,0,n-1) {FOR(j,0,n-1) cout<<a[i][j]<<" ";cout<<endl;}
- FOR(i,0,n-1) {FOR(j,0,n-1)
- {if(a[i][j]==0)
- {
- if(b[i][j]) cout<<0;
- else cout<<2;
- }else cout<<1;
- cout<<" ";
- }cout<<endl;}
- return 0;
- }
复制代码 |
|