池塘计数
农夫约翰有一片 N∗M 的矩形土地。
最近,由于降雨的原因,部分土地被水淹没了。
现在用一个字符矩阵来表示他的土地。
每个单元格内,如果包含雨水,则用”W”表示,如果不含雨水,则用”.”表示。
现在,约翰想知道他的土地中形成了多少片池塘。
每组相连的积水单元格集合可以看作是一片池塘。
每个单元格视为与其上、下、左、右、左上、右上、左下、右下八个邻近单元格相连。
请你输出共有多少片池塘,即矩阵中共有多少片相连的”W”块。
输入格式
第一行包含两个整数 N 和 M。
接下来 N 行,每行包含 M 个字符,字符为”W”或”.”,用以表示矩形土地的积水状况,字符之间没有空格。
输出格式
输出一个整数,表示池塘数目。
数据范围
1 ≤ N,M ≤ 1000
输入样例:
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
输出样例:
3
宽度优先搜索
- 标记池塘的第一个水池位置
- 宽搜遍历邻近全部水池,标记
- 将水池存入队列
- 每次从队列中取出水池坐标,重复2、3,直至将一个池塘的水池遍历完毕,周围不再出现水池,则寻找下一个池塘的第一个水池,重复以上
使用基础课BFS模板
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m, res;
char a[N][N];
bool st[N][N];
void bfs(int sx, int sy)
{
queue<PII> q;
q.push({sx, sy});
st[sx][sy] = true;
int dirx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int diry[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 8; i ++ )
{
int x = t.first + dirx[i];
int y = t.second + diry[i];
if (x >= 0 && x < n && y >= 0 && y < m && !st[x][y] && a[x][y] == 'W')
{
q.push({x, y});
st[x][y] = true;
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
cin >> a[i][j];
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (a[i][j] == 'W' && !st[i][j])
{
res ++ ;
bfs(i, j);
}
cout << res << endl;
return 0;
}
城堡问题
1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(图 1)
# = Wall
| = No wall
- = No wall
方向:上北下南左西右东。
图1是一个城堡的地形图。
请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。
城堡被分割成 m∗n个方格区域,每个方格区域可以有0~4面墙。
注意:墙体厚度忽略不计。
输入格式
第一行包含两个整数 m 和 n,分别表示城堡南北方向的长度和东西方向的长度。
接下来 m 行,每行包含 n 个整数,每个整数都表示平面图对应位置的方块的墙的特征。
每个方块中墙的特征由数字 P 来描述,我们用1表示西墙,2表示北墙,4表示东墙,8表示南墙,P 为该方块包含墙的数字之和。
例如,如果一个方块的 P 为3,则 3 = 1 + 2,该方块包含西墙和北墙。
城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。
输入的数据保证城堡至少有两个房间。
输出格式
共两行,第一行输出房间总数,第二行输出最大房间的面积(方块数)。
数据范围
1 ≤ m,n ≤ 50,
0 ≤ P ≤ 15
输入样例:
4 7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
输出样例:
5
9
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 55;
int n, m, res, cnt;
int g[N][N];
bool st[N][N];
int bfs(int sx, int sy)
{
queue<PII> q;
q.push({sx, sy});
st[sx][sy] = true;
int dirx[4] = {0, -1, 0, 1};
int diry[4] = {-1, 0, 1, 0};
int sum = 1;
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int x = t.first + dirx[i];
int y = t.second + diry[i];
if (x >= 0 && x < n && y >= 0 && y < m && !st[x][y] && !(g[t.first][t.second] >> i & 1))
{
q.push({x, y});
st[x][y] = true;
sum ++ ;
}
}
}
return sum;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
cin >> g[i][j];
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (!st[i][j])
{
res = max(res, bfs(i, j));
cnt ++ ;
}
cout << cnt << endl << res << endl;
return 0;
}
山峰和山谷
FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。
为了能够对旅程有一个安排,他想知道山峰和山谷的数量。
给定一个地图,为FGD想要旅行的区域,地图被分为 n×n 的网格,每个格子 (i,j) 的高度 w(i,j) 是给定的。
若两个格子有公共顶点,那么它们就是相邻的格子,如与 (i,j) 相邻的格子有(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)。
我们定义一个格子的集合 S 为山峰(山谷)当且仅当:
- S 的所有格子都有相同的高度。
- S 的所有格子都连通。
- 对于 s 属于 S,与 s 相邻的 s′ 不属于 S,都有 ws>ws′(山峰),或者 ws<ws′(山谷)。
- 如果周围不存在相邻区域,则同时将其视为山峰和山谷。
你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。
输入格式
第一行包含一个正整数 n,表示地图的大小。
接下来一个 n×n 的矩阵,表示地图上每个格子的高度 w。
输出格式
共一行,包含两个整数,表示山峰和山谷的数量。
数据范围
1 ≤ n ≤ 1000,
0 ≤ w ≤ 109
输入样例1:
5
8 8 8 7 7
7 7 8 8 7
7 7 7 7 7
7 8 8 7 8
7 8 8 8 8
输出样例1:
2 1
输入样例2:
5
5 7 8 3 1
5 5 7 6 6
6 6 6 2 8
5 7 2 5 8
7 1 0 1 7
输出样例2:
3 3
样例解释
样例1:
样例2:
核心:
- 在宽搜时,不需要记录邻近比它高/低的地区数量,只需要记录是否有比它高/低的地区,然后将相同高度的地区存放在队列中,然后重复宽搜,直至没有相同高度。
- 反向判断,没有比它高的地区,就是山峰,没有比它低的地区,就是低谷。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n;
int a[N][N];
bool st[N][N];
void bfs(int sx, int sy, bool& has_higher, bool& has_lower)
{
queue<PII> q;
q.push({sx, sy});
st[sx][sy] = true;
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = t.first - 1; i <= t.first + 1; i ++ )
for (int j = t.second - 1; j <= t.second + 1; j ++ )
{
if (i == t.first && j == t.second) continue;
if (i < 0 || i >= n || j < 0 || j >= n) continue;
if (a[i][j] != a[t.first][t.second])
{
if (a[i][j] > a[t.first][t.second]) has_higher = true;
else has_lower = true;
}
else if (!st[i][j])
{
q.push({i, j});
st[i][j] = true;
}
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
cin >> a[i][j];
int peak = 0; // 山峰
int valley = 0; // 山谷
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
if (!st[i][j])
{
bool has_higher = false;
bool has_lower = false;
bfs(i, j, has_higher, has_lower);
if (!has_higher) peak ++ ;
if (!has_lower) valley ++ ;
}
cout << peak << ' ' << valley << endl;
return 0;
}