求助

P1162 填涂颜色

``` // luogu-judger-enable-o2 #include<bits/stdc++.h> using namespace std; struct point{ int x,y; point(int xx,int yy){ x=xx; y=yy; } }; int a[32][32]; int book[32][32],don[4][2]={{-1,0},{1,0},{0,-1},{0,1}},n; int i,j,k,tx,ty; queue<point> q; void bfs(){ while(!q.empty()){ point p=q.front(); q.pop(); for(k=0;k<4;k++){ tx=p.x+don[k][0]; ty=p.y+don[k][1]; if(tx<0||tx>=n+2||ty<0||ty>=n+2) continue; if(a[tx][ty]==0&&book[tx][ty]==0){ a[tx][ty]=2; book[tx][ty]=1; point point1(tx,ty); q.push(point1); } } } } int main() { cin>>n; for(i=1;i<=n;i++) for(j=1;j<=n;j++){ cin>>a[i][j]; } q.push(point(0,0)); book[0][0]=1; bfs(); for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ cout<<2-a[i][j]<<' '; } cout<<endl; } return 0; } ```
by 听取MLE声一片 @ 2020-01-31 18:00:43


@[听取mc声一片](/user/253738) 边界是在n*n的方格里呀,没发现啥错呀
by 青苹果 @ 2020-01-31 18:03:35


@[青苹果](/user/118279) 您的遍历好像没有标记
by 听取MLE声一片 @ 2020-01-31 18:06:13



by 听取MLE声一片 @ 2020-01-31 18:06:37


@[听取mc声一片](/user/253738) 你结构体里面还套了函数,我看不太懂。。。
by 青苹果 @ 2020-01-31 18:08:55


我还是去写动规吧…
by 听取MLE声一片 @ 2020-01-31 18:09:12


```cpp void bfs(int x,int y) { b[x][y]=5; struct student c; c.a=x; c.b=y; q.push(c); while(!q.empty()) { c=q.front(); q.pop(); int xx=c.a; int yy=c.b; for(int i=0;i<=3;i++) { //cout<<"aaaaaa"<<endl; int t=xx+d[i][0]; int s=yy+d[i][1]; if(t>=1&&t<=n&&s>=1&&s<=n&&map[t][s]==0) { ////////// map[t][s]=1; b[t][s]=5; c.a=t; c.b=s; q.push(c); } } } ```
by 听取MLE声一片 @ 2020-01-31 18:12:01


```cpp #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; int n,map[35][35],b[35][35]; queue<struct student>q; struct student{ int a,b; }; int d[][2]={{-1,0},{0,1},{1,0},{0,-1}}; void bfs(int x,int y) { b[x][y]=5; struct student c; c.a=x; c.b=y; q.push(c); while(!q.empty()) { c=q.front(); q.pop(); int xx=c.a; int yy=c.b; for(int i=0;i<=3;i++) { //cout<<"aaaaaa"<<endl; int t=xx+d[i][0]; int s=yy+d[i][1]; if(t>=1&&t<=n&&s>=1&&s<=n&&map[t][s]==0) { map[t][s]=1; b[t][s]=5; c.a=t; c.b=s; q.push(c); } } } } int main() { int i,j; cin>>n; for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>map[i][j]; for(i=1;i<=n;i++)//第1,n行 { if(map[1][i]==0) bfs(1,i); if(map[n][i]==0) bfs(n,i); } for(i=1;i<=n;i++)//第1,n列 { if(map[i][1]==0) bfs(i,1); if(map[i][n]==0) bfs(i,n); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(b[i][j]==5) cout<<"0"<<" "; else if(map[i][j]==1) cout<<"1"<<" "; else if(map[i][j]==0) cout<<"2"<<" "; } cout<<endl; } return 0; } ```
by 听取MLE声一片 @ 2020-01-31 18:14:18


你再试试这一个
by 听取MLE声一片 @ 2020-01-31 18:15:12


@[青苹果](/user/118279)
by 听取MLE声一片 @ 2020-01-31 18:15:17


上一页 | 下一页