1 solutions
-
0
C++ :
#include <bits/stdc++.h> #define N 40 using namespace std; struct Point{ int x,y; }; int G[N][N],n,vis[N][N]; int dx[] = {-1,1,0,0}; int dy[] = {0,0,-1,1}; void bfs(){ queue<Point> que; for (int i = 1;i<=n;i++){ if (!G[1][i]) {que.push({1,i});vis[1][i] = 1;} if (!G[n][i]) {que.push({n,i});vis[n][i] = 1;} if (!G[i][1]) {que.push({i,1});vis[i][1] = 1;} if (!G[i][n]) {que.push({i,n});vis[i][n] = 1;} } while(!que.empty()){ Point u = que.front(); que.pop(); for (int i = 0;i<4;i++){ int x = u.x + dx[i]; int y = u.y + dy[i]; if (x>n || x<1 || y>n || y<1) continue; if (vis[x][y] || G[x][y]) continue; que.push({x,y}); vis[x][y] = 1; } } } int main(){ cin >> n; for (int i = 1;i<=n;i++){ for (int j = 1;j<=n;j++){ cin >> G[i][j]; } } bfs(); for (int i = 1;i<=n;i++){ for (int j = 1;j<=n;j++){ if (!vis[i][j] && !G[i][j]) cout << 2 << " "; else cout << G[i][j]<< " "; } cout << endl; } return 0; }
- 1
Information
- ID
- 220
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 10
- Tags
- # Submissions
- 2
- Accepted
- 2
- Uploaded By