5号点MLE求条

P1162 填涂颜色

```cpp #include<bits/stdc++.h> using namespace std; int n,a[35][35],vis[35][35]; const int d[4][2]= {{-1,0},{1,0},{0,-1},{0,1}}; int bfs(int mode, int i, int j) { memset(vis,0,sizeof vis); queue<pair<int,int> >q; q.push({i,j}); while(!q.empty()) { int x=q.front().first, y=q.front().second; q.pop(); if(vis[x][y]) continue; vis[x][y]=1; for(int i=0; i<4; i++) if(a[x+d[i][0]][y+d[i][1]] == 0) q.push({x+d[i][0],y+d[i][1]}); if(x==1 || y==1 || x==n || y==n) return 0; if(mode) a[x][y]=2; } return 1; } int main() { 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) continue; if(bfs(0,i,j)) bfs(1,i,j); } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) cout<<a[i][j]<<' '; cout<<'\n'; } } ```
by chenbs @ 2024-10-21 13:04:58


bfs 不用写这么长啊
by chenbs @ 2024-10-21 13:05:28


|