floodfill
1. floodfill原理
原理
- 洪水灌溉算法,基本问题是:有一个池塘,池塘内有几块陆地,找出这几块陆地。
- 可以通过宽搜或者深搜实现,最常用的是宽搜。
- 深搜可能会出现爆栈的风险。
- 可能出现的问题:(1)找出陆地的个数;(2)计算陆地的面积;(3)计算陆地的周长; 等
代码模板
// BFS
#include <iostream>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010, M = N * N;
int n, m;
char g[N][N]; // 存储地图
PII q[M];
bool st[N][N]; // 判重数组
void bfs(int sx, int sy) {
int hh = 0, tt = 0;
q[0] = {sx, sy};
st[sx][sy] = true;
while (hh <= tt) {
auto t = q[hh++];
for (int i = t.x - 1; i <= t.x + 1; i++)
for (int j = t.y - 1; j <= t.y + 1; j++) {
if (i == t.x && j == t.y) continue;
if (i < 0 || i >= n || j < 0 || j >= m) continue;
if (g[i][j] == '.' || st[i][j]) continue;
q[++tt] = {i, j};
st[i][j] = true;
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", g[i]);
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] == 'W' && !st[i][j]) {
bfs(i, j);
cnt++;
}
printf("%d\n", cnt);
return 0;
}
// DFS
#include <iostream>
#define x first
#define y second
using namespace std;
const int N = 1010;
int n, m;
char g[N][N]; // 存储地图
bool st[N][N]; // 判重数组
void dfs(int sx, int sy) {
st[sx][sy] = true;
for (int i = sx - 1; i <= sx + 1; i++)
for (int j = sy - 1; j <= sy + 1; j++) {
if (i >= 0 && i < n && j >= 0 && j < m && g[i][j] == 'W' && !st[i][j])
dfs(i, j);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", g[i]);
int cnt = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] == 'W' && !st[i][j]) {
dfs(i, j);
cnt++;
}
printf("%d\n", cnt);
return 0;
}
2. AcWing上的floodfill题目
AcWing 1098. 城堡问题
问题描述
-
问题链接:AcWing 1098. 城堡问题
分析
- 对于某一个方向是否有墙的判断:可以用二进制判断,左上右下分别用0、1、2、3代表,则可以使用
g[x][y] >> i & 1
判断(这里的i就是刚才的0、1、2、3)。其他直接使用floodfill
算法求解即可。
代码
- C++
// BFS
#include <iostream>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 55, M = N * N;
int n, m;
int g[N][N];
PII q[M];
bool st[N][N];
int bfs(int sx, int sy) {
int hh = 0, tt = 0;
int area = 0;
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0}; // 左上右下
q[0] = {sx, sy};
st[sx][sy] = true;
while (hh <= tt) {
auto t = q[hh++];
area++;
for (int i = 0; i < 4; i++) {
int a = t.x + dx[i], b = t.y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (st[a][b]) continue;
if (g[t.x][t.y] >> i & 1) continue;
q[++tt] = {a, b};
st[a][b] = true;
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> g[i][j];
int cnt = 0, area = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!st[i][j]) {
area = max(area, bfs(i, j));
cnt++;
}
cout << cnt << endl;
cout << area << endl;
return 0;
}
// DFS
#include <iostream>
#define x first
#define y second
using namespace std;
const int N = 55;
int n, m;
int g[N][N];
bool st[N][N];
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0}; // 上右下左
int dfs(int sx, int sy) {
st[sx][sy] = true;
int area = 1;
for (int i = 0; i < 4; i++) {
int a = sx + dx[i], b = sy + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && !(g[sx][sy] >> i & 1) && !st[a][b])
area += dfs(a, b);
}
return area;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> g[i][j];
int cnt = 0, area = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!st[i][j]) {
area = max(area, dfs(i, j));
cnt++;
}
cout << cnt << endl;
cout << area << endl;
return 0;
}
AcWing 1106. 山峰和山谷
问题描述
-
问题链接:AcWing 1106. 山峰和山谷
分析
- 在
floodfill
的同时记录每个连通块周围是否有高于或者低于当前连通块的山峰,根据这个就可以判断山峰山谷的个数。
代码
- C++
// BFS
#include <iostream>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010, M = N * N;
int n;
int h[N][N];
PII q[M];
bool st[N][N];
void bfs(int sx, int sy, int &has_higher, int &has_lower) {
int hh = 0, tt = 0;
q[0] = {sx, sy};
st[sx][sy] = true;
while (hh <= tt) {
PII t = q[hh++];
for (int i = t.x - 1; i <= t.x + 1; i++)
for (int j = t.y - 1; j <= t.y + 1; j++) {
if (i == t.x && j == t.y) continue;
if (i < 0 || i >= n || j < 0 || j >= n) continue;
if (h[t.x][t.y] != h[i][j]) {
if (h[i][j] > h[t.x][t.y]) has_higher = true;
else has_lower = true;
} else if (!st[i][j]) {
q[++tt] = {i, j};
st[i][j] = true;
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &h[i][j]);
int peak = 0, valley = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j< n; j++)
if (!st[i][j]) {
int has_higher = false, has_lower = false;
bfs(i, j, has_higher, has_lower);
if (!has_higher) peak++;
if (!has_lower) valley++;
}
printf("%d %d\n", peak, valley);
return 0;
}
// DFS
// 没有通过,原因:Memory Limit Exceeded
#include <iostream>
#define x first
#define y second
using namespace std;
const int N = 1010;
int n;
int h[N][N];
bool st[N][N];
void dfs(int sx, int sy, int &has_higher, int &has_lower) {
st[sx][sy] = true;
for (int i = sx - 1; i <= sx + 1; i++)
for (int j = sy - 1; j <= sy + 1; j++)
if (i >= 0 && i < n && j >= 0 && j < n) {
if (h[i][j] != h[sx][sy]) {
if (h[i][j] > h[sx][sy]) has_higher = true;
else has_lower = true;
} else if (!st[i][j]) {
dfs(i, j, has_higher, has_lower);
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &h[i][j]);
int peak = 0, valley = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j< n; j++)
if (!st[i][j]) {
int has_higher = false, has_lower = false;
dfs(i, j, has_higher, has_lower);
if (!has_higher) peak++;
if (!has_lower) valley++;
}
printf("%d %d\n", peak, valley);
return 0;
}
AcWing 1113 红与黑
问题描述
-
问题链接:AcWing 1113 红与黑
分析
- 找到你所在的位置,执行一遍
floodfill
算法即可。
代码
- C++
// BFS
#include <iostream>
#include <queue>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 25;
int n, m; // 行数,列数
char g[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int bfs(int sx, int sy) {
queue<PII> q;
q.push({sx, sy});
g[sx][sy] = '#';
int res = 0;
while (q.size()) {
auto t = q.front();
q.pop();
res++;
for (int i = 0; i < 4; i++) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] != '.') continue;
g[x][y] = '#';
q.push({x, y});
}
}
return res;
}
int main() {
while (cin >> m >> n, n || m) {
for (int i = 0; i < n; i++) cin >> g[i];
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (g[i][j] == '@') {
x = i, y = j;
break;
}
}
cout << bfs(x, y) << endl;
}
}
// DFS
#include <iostream>
using namespace std;
const int N = 25;
int n, m; // 行数,列数
char g[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int dfs(int x, int y) {
int res = 1;
g[x][y] = '#';
for (int i = 0; i < 4; i++) {
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && g[a][b] == '.')
res += dfs(a, b);
}
return res;
}
int main() {
while (cin >> m >> n, n || m) {
for (int i = 0; i < n; i++) cin >> g[i];
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (g[i][j] == '@') {
x = i, y = j;
break;
}
}
cout << dfs(x, y) << endl;
}
}
3. 力扣上的floodfill题目
Leetcode 0200 岛屿数量
问题描述
-
问题链接:Leetcode 0200 岛屿数量
分析
- 直接使用
floodfill
算法即可,BFS或者DFS均可
代码
- C++
/**
* 执行用时:16 ms, 在所有 C++ 提交中击败了98.27%的用户
* 内存消耗:9.4 MB, 在所有 C++ 提交中击败了93.39%的用户
*/
class Solution {
public:
vector<vector<char>> g;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int numIslands(vector<vector<char>> &grid) {
g = grid;
int cnt = 0;
for (int i = 0; i < g.size(); i++) {
for (int j = 0; j < g[0].size(); j++)
if (g[i][j] == '1') {
dfs(i, j);
cnt++;
}
}
return cnt;
}
void dfs(int x, int y) {
g[x][y] = '0';
for (int i = 0; i < 4; i++) {
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < g.size() && b >= 0 && b < g[0].size() && g[a][b] == '1')
dfs(a, b);
}
}
};
- Java
/**
* Date: 2020/9/3 16:21
* Content: 经典的 floodfill 算法
* 执行用时:2 ms, 在所有 Java 提交中击败了92.58%的用户
* 内存消耗:40.7 MB, 在所有 Java 提交中击败了84.35%的用户
*/
class Solution {
int[][] d = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 上右下左
int n, m; // 行数,列数
char[][] g;
boolean[][] st;
// 从grid[x][y]的位置开始(该位置要求为'1',即陆地),进行 floodfill
private void dfs(int x, int y) {
st[x][y] = true;
for (int i = 0; i < 4; i++) {
int a = x + d[i][0], b = y + d[i][1];
if (a >= 0 && a < n && b >= 0 && b < m && !st[a][b] && g[a][b] == '1')
dfs(a, b);
}
}
public int numIslands(char[][] grid) {
g = grid;
n = g.length;
if (n == 0) return 0;
m = g[0].length;
st = new boolean[n][m];
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (grid[i][j] == '1' && !st[i][j]) {
res++;
dfs(i, j);
}
return res;
}
}
Leetcode 0463 岛屿的长度
问题描述
-
问题链接:Leetcode 0463 岛屿的长度
分析
- 如果从陆地开始遍历,出界或者遍历到水,都说明是边界,结果加一
代码
- C++
/**
* 执行用时:164 ms, 在所有 C++ 提交中击败了44.11%的用户
* 内存消耗:93.7 MB, 在所有 C++ 提交中击败了59.12%的用户
*/
class Solution {
public:
int islandPerimeter(vector<vector<int>> &grid) {
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int res = 0;
for (int i = 0; i < grid.size(); i++)
for (int j = 0; j < grid[i].size(); j++)
if (grid[i][j] == 1) {
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size()) res++;
else if (grid[x][y] == 0) res++;
}
}
return res;
}
};
- Java
/**
* Date: 2020/10/30 10:07
* Content: Floodfill
* 执行用时:11 ms, 在所有 Java 提交中击败了32.41%的用户
* 内存消耗:39.4 MB, 在所有 Java 提交中击败了88.92%的用户
*/
public class Solution {
int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1}; // 上右下左
public int islandPerimeter(int[][] grid) {
int res = 0;
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[0].length; j++)
if (grid[i][j] == 1)
for (int k = 0; k < 4; k++) {
int a = i + dx[k], b = j + dy[k];
if (a < 0 || a >= grid.length || b < 0 || b >= grid[0].length) res++;
else if (grid[a][b] == 0) res++;
}
return res;
}
}
LeetCode 0695 岛屿的最大面积
问题描述
分析
- 直接使用
floodfill
算法即可,BFS或者DFS均可
代码
- C++
/**
* 执行用时:36 ms, 在所有 C++ 提交中击败了21.34%的用户
* 内存消耗:26.3 MB, 在所有 C++ 提交中击败了9.78%的用户
*/
class Solution {
public:
typedef pair<int, int> PII;
int n, m;
vector<vector<int>> g;
vector<vector<bool>> st;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int maxAreaOfIsland(vector<vector<int>> &grid) {
g = grid;
n = g.size();
if (n == 0) return 0;
m = g[0].size();
if (m == 0) return 0;
st = vector<vector<bool>>(n, vector<bool>(m, false));
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!st[i][j] && g[i][j] == 1)
res = max(res, bfs(i, j));
return res;
}
int bfs(int sx, int sy) {
queue<PII> q;
q.push({sx, sy});
st[sx][sy] = true;
int area = 0;
while (q.size()) {
auto t = q.front(); q.pop();
area++;
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 < m && g[a][b] == 1 && !st[a][b]) {
q.push({a, b});
st[a][b] = true;
}
}
}
return area;
}
};
- Java
/**
* 执行用时:5 ms, 在所有 Java 提交中击败了18.82%的用户
* 内存消耗:38.4 MB, 在所有 Java 提交中击败了99.62%的用户
*/
public class Solution {
private int n, m;
private int[][] g;
private final int[][] d = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 上右下左
private boolean[][] st; // 记录节点是否访问过
public int maxAreaOfIsland(int[][] grid) {
n = grid.length;
if (n == 0) return 0;
m = grid[0].length;
if (m == 0) return 0;
this.g = grid;
st = new boolean[n][m]; // 默认为false
int res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!st[i][j] && grid[i][j] == 1)
res = Math.max(res, dfs(i, j));
return res;
}
// 返回某个陆地大小
private int dfs(int sx, int sy) {
st[sx][sy] = true;
int res = 1;
for (int i = 0; i < 4; i++) {
int a = sx + d[i][0], b = sy + d[i][1];
if (a >= 0 && a < n && b >= 0 && b < m && !st[a][b] && g[a][b] == 1)
res += dfs(a, b);
}
return res;
}
}