1 条题解
-
1
这道题和老师讲过的没什么两样, 被洪水淹没的地区肯定和边缘相连, 所以从边缘出发做bfs就行了。
#include <bits/stdc++.h> using namespace std; struct Point{ int x, y; }; queue<Point> q; int n, m, dx[4]={1, 0, 0, -1}, dy[4]={0, 1, -1, 0}, diff[505][505], sum; char a[505][505]; void bfs(){ for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if ((i==1 || i==n || j==1 || j==m) && a[i][j] == '0'){ q.push((Point){i, j}); diff[i][j] = 1; } while (q.size()){ Point now = q.front(); q.pop(); for (int i = 0; i < 4; i++){ int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]=='0'&&!diff[nx][ny]){ diff[nx][ny] = 1; q.push((Point){nx, ny}); } } } } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; bfs(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] == '0' && diff[i][j] == 0) sum++; cout << sum; return 0; }
- 1
信息
- ID
- 909
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- 递交数
- 28
- 已通过
- 24
- 上传者