Linux 環境下安裝 Nexus 私服儲存庫
阿新 • • 發佈:2022-03-18
題目連結
題目思路
主要是找到被1圍著的0,但是剛開始找的最後一個,然後WA兩個點,最後找到第一個0的位置,然後寬搜成功AC
題目程式碼
#include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef pair<int, int> PII; const int N = 50; int g[N][N]; bool st[N][N]; int n; void bfs(int x, int y) { queue<PII> q; q.push({x, y}); g[x][y] = 2; st[x][y] = true; while(q.size()) { auto t = q.front(); q.pop(); int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; for(int i = 0; i < 4; i ++ ) { int a = t.first + dx[i], b = t.second + dy[i]; if(a >= 0 && a < n && b >= 0 && b < n && !st[a][b] && g[a][b] != 1) { if(g[a][b] == 0) g[a][b] = 2; st[a][b] = true; q.push({a, b}); } } } } int main() { cin >> n; int x, y, f = 0; for(int i = 0; i < n; i ++ ) for(int j = 0; j < n; j ++ ) { cin >> g[i][j]; } for(int i = 0; i < n; i ++ ) { for(int j = 0; j < n; j ++ ) { if(g[i][j] == 1) { x = i + 1, y = j + 1; f = 1; break; } } if(f) break; } // cout << x << ' ' << y << endl; bfs(x, y); for(int i = 0; i < n; i ++ ) { for(int j = 0; j < n; j ++ ) cout << g[i][j] << ' '; puts(""); } }