48求调

P1162 填涂颜色

```cpp #include<bits/stdc++.h> using namespace std; int n,a[37][37],cnt,col[37][37],t,vis[37][37]; const int dir[4][2]={ {0,1}, {0,-1}, {1,0}, {-1,0} }; int head,tail; struct point{ int x,y; }; point que[37*37]; bool check(int x,int y){ return x>=1&&x<=n&&y>=1&&y<=n; } bool bfs(){ bool flag=true; while(head<=tail){ point p=que[head]; head++; for(int i=0;i<4;i++){ point q; q.x=p.x+dir[i][0]; q.y=p.y+dir[i][1]; if(!check(q.x,q.y)){ flag=false; } if(check(q.x,q.y)&&a[q.x][q.y]==0&&!vis[q.x][q.y]){ tail++; que[tail]=q; col[q.x][q.y]=cnt; vis[q.x][q.y]=1; } } } return flag; } int main(){ cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j]; } } head=1; tail=0; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(vis[i][j]==0&&a[i][j]==0){ head=1; tail=0; tail++; que[tail].x=i; que[tail].y=j; cnt++; vis[i][j]=1; col[i][j]=cnt; if(bfs()){ t=cnt; } } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(col[i][j]==t){ a[i][j]=2; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<a[i][j]<<" "; } cout<<"\n"; } } ```
by guanzisheng2 @ 2024-08-15 11:09:06


``` #include <bits/stdc++.h> using namespace std; const int N = 35; int g[N][N]; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; int n; void dfs(int x, int y) { g[x][y] = 2; //把和边缘连通的0染成2 for(int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if(nx >= 1 && nx <= n && ny >= 1 && ny <= n && g[nx][ny] == 0) { dfs(nx, ny); } } } int main() { cin >> n; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> g[i][j]; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if((i == 1 || i== n || j == 1 || j == n) && g[i][j] == 0) dfs(i, j); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(g[i][j] == 0) cout << 2; else if(g[i][j] == 2) cout << 0; else cout << 1; cout << " "; } cout << endl; } return 0; }
by ZZyangchengzhuo @ 2024-08-15 11:10:06


求关注
by guanzisheng2 @ 2024-08-15 11:10:21


@[xuruizhe150711](/user/1397517) okay
by ZZyangchengzhuo @ 2024-08-15 11:11:17


我的AC code ``` #include<bits/stdc++.h> using namespace std; int a[114][114],step=1; int n,m,d,f; int fx[6]={0,0,1,0,-1}; int fy[6]={0,1,0,-1,0}; void dfs(int x,int y){ if(x<1||y<1||x>n||y>n||a[x][y]!=0)return; a[x][y]=2; dfs(x,y+1); dfs(x+1,y); dfs(x,y-1); dfs(x-1,y); } int main(){ ios::sync_with_stdio(0); cout.tie(0);cin.tie(0); cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(a[i][j]==1&&d==0){ d=i; f=j; } } } dfs(d+1,f+1); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<a[i][j]<<" "; }cout<<endl; } }
by 违规用户名K&xs3Z^ @ 2024-08-15 11:15:31


根据你的改 再找到一个点后 应从那个点再进行搜索 ``` #include <bits/stdc++.h> using namespace std; int n; int a[35][35], b[35][35]; int kx[5] = {0, 1, -1, 0, 0}; int ky[5] = {0, 0, 0, 1, -1}; void dfs(int x1, int y1){ b[x1][y1] = 3; for(int i = 1; i <= 4; i++){ int x2 = x1+kx[i]; int y2 = y1+ky[i]; if(x2<=n && y2<=n && x2>0 && y2>0 && b[x2][y2]==0){ dfs(x2,y2);//改动点 } } } int main(){ cin >> n; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cin >> a[i][j]; b[i][j] = a[i][j]; } } for(int i = 1; i <= n; i++){ if(a[i][1] == 0)dfs(i, 1); if(a[i][n] == 0)dfs(i, n); } for(int i = 1; i <= n; i++){ if(a[1][i] == 0)dfs(1, i); if(a[n][i] == 0)dfs(n, i); } for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(b[i][j] == 0)a[i][j] = 2; cout << a[i][j] << " "; } cout << endl; } } ```
by 违规用户名K&xs3Z^ @ 2024-08-15 11:20:39


@[违规用户名K&xs3Z^](/user/1271316) 感谢!!!十分感谢!!!!!已A
by H_Monkey @ 2024-08-15 18:51:12


@[ZZyangchengzhuo](/user/1271395) 感谢已A
by H_Monkey @ 2024-08-15 18:51:32


@[xuruizhe150711](/user/1397517) 感谢
by H_Monkey @ 2024-08-15 18:51:49


@[H_Monkey](/user/811570) 嗯嗯
by ZZyangchengzhuo @ 2024-08-15 19:00:28


|