本质上来讲Flood Fill算法就是从一个点开始往外发散,将联通的位置覆盖为一整个部分,最常用的实现方法是BFS,当然DFS也是可以实现的。这类题在代码上是极其相似的,有着固定的套路,只不过在一些细节上需要处理一下。


池塘计数

  • 求二维地图中有多少个池塘
  • 需要判断八个方向
  • 注意标记遍历过的点 ```cpp

    include

    include

    include

    include

using namespace std; const int N = 1007, M = N * N;

define x first

define y second

typedef pair PII;

int n, m, cnt; char g[N][N]; // 地图

int dx[8] = {-1, 1, -1, 1, 0, 0, 1, -1}; int dy[8] = {-1, 1, 1, -1, -1, 1, 0, 0};

void bfs(int sx, int sy) { queue q; q.push({sx, sy}); while (!q.empty()) { PII buf = q.front(); q.pop(); for (int i = 0; i < 8; ++i) { int xx = buf.x + dx[i], yy = buf.y + dy[i]; if (xx < 0 || xx >= n || yy < 0 || yy >= m || g[xx][yy] == ‘.’) continue; q.push({xx, yy}); g[xx][yy] = ‘.’; } } }

int main() {

  1. scanf("%d%d", &n, &m);
  2. for (int i = 0; i < n; ++i) scanf("%s", g[i]);
  3. for (int i = 0; i < n; ++i) {
  4. for (int j = 0; j < m; ++j) {
  5. if (g[i][j] == 'W') {
  6. bfs(i, j);
  7. cnt++;
  8. }
  9. }
  10. }
  11. printf("%d\n", cnt);
  12. return 0;

}

  1. ---
  2. <a name="jyqvF"></a>
  3. # [城堡问题](https://www.acwing.com/problem/content/1100/)
  4. - 求二维城堡中有多少个房间
  5. - 需要判断四个方向
  6. - 用位运算来判墙,相比上一题,在判断上要稍微复杂一点
  7. ```cpp
  8. # include<iostream>
  9. # include<algorithm>
  10. # include<queue>
  11. using namespace std;
  12. # define x first
  13. # define y second
  14. typedef pair<int, int> PII;
  15. const int N = 55;
  16. int n, m;
  17. int g[N][N];
  18. bool st[N][N];
  19. queue<PII> q;
  20. int dx[4] = {0, -1, 0, 1}; // 左上右下,与墙的位对应
  21. int dy[4] = {-1, 0, 1, 0};
  22. int bfs(int sx, int sy) {
  23. int area = 0;
  24. q.push({sx, sy});
  25. st[sx][sy] = true;
  26. while (!q.empty()) {
  27. PII t = q.front(); q.pop();
  28. area++;
  29. for (int i = 0; i < 4; ++i) {
  30. int xx = t.x + dx[i], yy = t.y + dy[i];
  31. if (xx < 0 || xx >= n || yy < 0 || yy >= m || st[xx][yy]) continue;
  32. if (g[t.x][t.y] >> i & 1) continue; // 有墙
  33. q.push({xx, yy});
  34. st[xx][yy] = true;
  35. }
  36. }
  37. return area;
  38. }
  39. int main() {
  40. cin >> n >> m;
  41. for (int i = 0; i < n; ++i) {
  42. for (int j = 0; j < m; ++j) {
  43. cin >> g[i][j];
  44. }
  45. }
  46. int cnt = 0, area = 0;
  47. for (int i = 0; i < n; ++i) {
  48. for (int j = 0; j < m; ++j) {
  49. if (!st[i][j]) {
  50. area = max(area, bfs(i, j));
  51. cnt++;
  52. }
  53. }
  54. }
  55. cout << cnt << endl;
  56. cout << area;
  57. return 0;
  58. }

山峰和山谷

  • 求有多少山峰和山谷
  • 该题需要注意的依然是判断条件
  • 山谷的周围均高于其,山峰的周围均低于其 ```cpp

    include

    include

    include

using namespace std;

define x first

define y second

typedef pair PII;

const int N = 1010;

int n; int h[N][N]; queue q; bool st[N][N];

int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};

void bfs(int sx, int sy, bool& has_higher, bool& has_lower) { q.push({sx, sy}); st[sx][sy] = true;

  1. while (!q.empty()) {
  2. PII t = q.front(); q.pop();
  3. for (int i = 0; i < 8; ++i) {
  4. int xx = t.x + dx[i], yy = t.y + dy[i];
  5. if (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
  6. if (h[xx][yy] != h[t.x][t.y]) { // 想称为山峰或山谷必须得是一样高的,如不同则不为谋
  7. if (h[xx][yy] > h[t.x][t.y]) has_higher = true; // 当前位置不是山峰
  8. else has_lower = true; // 不是山谷
  9. } else if (!st[xx][yy]) {
  10. q.push({xx, yy});
  11. st[xx][yy] = true;
  12. }
  13. }
  14. }

}

int main() {

  1. cin >> n;
  2. for (int i = 0; i < n; ++i) {
  3. for (int j = 0; j < n; ++j) {
  4. cin >> h[i][j];
  5. }
  6. }
  7. int peak = 0, valley = 0; // 山峰与山谷的个数
  8. for (int i = 0; i < n; ++i) {
  9. for (int j = 0; j < n; ++j) {
  10. if (!st[i][j]) {
  11. bool has_higher = false, has_lower = false;
  12. bfs(i, j, has_higher, has_lower);
  13. if (!has_higher) peak++; // 一堆既可能是山峰也可能同时是山谷
  14. if (!has_lower) valley++;
  15. }
  16. }
  17. }
  18. cout << peak << ' ' << valley;
  19. return 0;

} ```