FloodFill
AcWing1097. 池塘计数
农夫约翰有一片 N ∗ M N∗M N∗M 的矩形土地。
最近,由于降雨的原因,部分土地被水淹没了。
现在用一个字符矩阵来表示他的土地。
每个单元格内,如果包含雨水,则用”W”表示,如果不含雨水,则用”.”表示。
现在,约翰想知道他的土地中形成了多少片池塘。
每组相连的积水单元格集合可以看作是一片池塘。
每个单元格视为与其上、下、左、右、左上、右上、左下、右下八个邻近单元格相连。
请你输出共有多少片池塘,即矩阵***有多少片相连的”W”块。
输入格式
第一行包含两个整数 N 和 M。
接下来 N 行,每行包含 M 个字符,字符为”W”或”.”,用以表示矩形土地的积水状况,字符之间没有空格。
输出格式
输出一个整数,表示池塘数目。
数据范围
1 ≤ N , M ≤ 1000 1≤N,M≤1000 1≤N,M≤1000
代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define eps 1e-6
#define mem(n,a) memset(n,a,sizeof(n))
#define rep(i,be,en) for(int i=be;i<=en;++i)
#define pre(i,be,en) for(int i=en;i>=be;--i)
inline int gcd(int a, int b) {
return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
return x & -x; }
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 1010;
char s[N][N];
int vis[N][N];
int n, m, cnt;
void bfs(int x,int y) {
vis[x][y] = 1;
queue<pair<int,int>>q;
q.push(make_pair(x, y));
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 >= m)continue;
if (s[i][j] == '.' || vis[i][j])continue;
vis[i][j] = 1;
q.push({
i,j });
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0;i < n;++i)
scanf("%s", s[i]);
for (int i = 0;i < n;++i) {
for (int j = 0;j < m;++j) {
if (s[i][j] == 'W' && !vis[i][j]) {
bfs(i, j);
cnt++;
}
}
}
cout << cnt << endl;
return 0;
}
AcWing1098. 城堡问题
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表示南墙,PP 为该方块包含墙的数字之和。
例如,如果一个方块的 P 为3,则 3 = 1 + 2,该方块包含西墙和北墙。
城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。
输入的数据保证城堡至少有两个房间。
输出格式
共两行,第一行输出房间总数,第二行输出最大房间的面积(方块数)。
数据范围
1 ≤ m , n ≤ 50 1≤m,n≤50 1≤m,n≤50
0 ≤ P ≤ 15 0≤P≤15 0≤P≤15
代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
inline int gcd(int a, int b) {
return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
return x & -x; }
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100;
int g[N][N];
bool vis[N][N];
int n,m;
int dx[4] = {
0,-1,0,1};
int dy[4] = {
-1,0,1,0};
int bfs(int x,int y){
queue<PII>q;
q.push({
x,y});
vis[x][y] = true;
int area =0 ;
while(q.size()){
auto t = q.front();
q.pop();
++area;
for(int i = 0;i <4;++i){
int xx =t.first + dx[i];
int yy = t.second + dy[i];
if(xx < 0 || xx >= n || yy < 0 || yy >= m)continue;
if(vis[xx][yy])continue;
if(g[t.first][t.second] >> i & 1)continue;
q.push({
xx,yy});
vis[xx][yy] = true;
}
}
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 area,cnt =0;
for(int i = 0 ;i<n;++i)
for(int j = 0;j <m;++j)
if(!vis[i][j]){
area = max(area,bfs(i,j));
cnt++;
}
cout << cnt << endl;
cout << area << endl;
return 0;
}
AcWing1106. 山峰和山谷
F G D FGD FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。
为了能够对旅程有一个安排,他想知道山峰和山谷的数量。
给定一个地图,为 F G D FGD FGD想要旅行的区域,地图被分为 n×n 的网格,每个格子 (i,j) 的高度 w(i,j) 是给定的。
若两个格子有公共顶点,那么它们就是相邻的格子,如与 (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 ) (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) (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,都有 w s > w s ′ ws>ws′ ws>ws′(山峰),或者 w s < w s ′ ws<ws′ ws<ws′(山谷)。
- 如果周围不存在相邻区域,则同时将其视为山峰和山谷。
你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。
输入格式
第一行包含一个正整数 n,表示地图的大小。
接下来一个 n×n 的矩阵,表示地图上每个格子的高度 w。
输出格式
共一行,包含两个整数,表示山峰和山谷的数量。
数据范围
1 ≤ n ≤ 1000 1≤n≤1000 1≤n≤1000
0 ≤ w ≤ 1 0 9 0≤w≤10^{9} 0≤w≤109
代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
inline int gcd(int a, int b) {
return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
return x & -x; }
inline LL read() {
LL f = 1; LL x = 0;char ch = getchar();while (ch > '9' || ch < '0') {
if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f; }
const int N = 1010;
LL h[N][N];
bool vis[N][N];
LL n;
void bfs(int x, int y, bool& has_higher, bool& has_lower) {
queue<PII>q;
q.push({
x,y });
vis[x][y] = 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 < 0 || i >= n || j < 0 || j >= n)continue;
if (h[i][j] != h[t.first][t.second]) {
if (h[i][j] > h[t.first][t.second])has_higher = true;
else has_lower = true;
}
else if (!vis[i][j]) {
vis[i][j] = true;
q.push({
i,j });
}
}
}
}
}
int main() {
n = read();
for (int i = 0;i < n;++i)
for (int j = 0;j < n;++j)
h[i][j] = read();
int peak = 0, valley = 0;
for (int i = 0;i < n;++i) {
for (int j = 0; j < n;++j) {
if (!vis[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;
}
最短路
AcWing1076. 迷宫问题
给定一个 n×n 的二维数组,如下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
数据保证至少存在一条从左上角走到右下角的路径。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含 n 个整数 0 或 1,表示迷宫。
输出格式
输出从左上角到右下角的最短路线,如果答案不唯一,输出任意一条路径均可。
按顺序,每行输出一个路径中经过的单元格的坐标,左上角坐标为 (0,0),右下角坐标为 (n−1,n−1)。
代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
inline int gcd(int a, int b) {
return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
return x & -x; }
inline LL read() {
LL f = 1; LL x = 0;char ch = getchar();while (ch > '9' || ch < '0') {
if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f; }
const int N = 1010;
int g[N][N];
PII pre[N][N];
LL n;
void bfs(int x,int y) {
queue<PII>q;
q.push({
x,y });
pre[x][y] = {
0,0 };
memset(pre, -1, sizeof pre);
pre[x][y] = {
0,0 };
int dx[4] = {
0,-1,0,1 }, dy[4] = {
-1,0,1,0 };
while (q.size()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 4;++i) {
int xx = t.first + dx[i];
int yy = t.second + dy[i];
if (xx < 0 || xx >= n || yy < 0 || yy >= n)continue;
if (pre[xx][yy].first != -1 || g[xx][yy] == 1)continue;
q.push({
xx,yy });
pre[xx][yy] = t;
}
}
}
int main() {
n = read();
for (int i = 0;i < n;++i)
for (int j = 0;j < n;++j)
g[i][j] = read();
bfs(n - 1, n - 1);
PII end = {
0,0 };
while (1) {
printf("%d %d\n", end.first, end.second);
if (end.first == n - 1 && end.second == n - 1)break;
end = pre[end.first][end.second];
}
return 0;
}
AcWing188. 武士风度的牛
农民John有很多牛,他想交易其中一头被Don称为The Knight的牛。
这头牛有一个独一无二的超能力,在农场里像Knight一样地跳(就是我们熟悉的象棋中马的走法)。
虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个x,y的坐标图来表示。
这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了The Knight的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。
现在你的任务是,确定The Knight要想吃到草,至少需要跳多少次。
The Knight的位置用’K’来标记,障碍的位置用’*’来标记,草的位置用’H’来标记。
这里有一个地图的例子:
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . H
5 | * . . . . . . . . .
4 | . . . * . . . * . .
3 | . K . . . . . . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ----------------------
1
0 1 2 3 4 5 6 7 8 9 0
The Knight 可以按照下图中的A,B,C,D…这条路径用5次跳到草的地方(有可能其它路线的长度也是5):
11 | . . . . . . . . . .
10 | . . . . * . . . . .
9 | . . . . . . . . . .
8 | . . . * . * . . . .
7 | . . . . . . . * . .
6 | . . * . . * . . . F<
5 | * . B . . . . . . .
4 | . . . * C . . * E .
3 | .>A . . . . D . . .
2 | . . . * . . . . . *
1 | . . * . . . . * . .
0 ----------------------
1
0 1 2 3 4 5 6 7 8 9 0
注意: 数据保证一定有解。
输入格式
第1行: 两个数,表示农场的列数C(C<=150)和行数R(R<=150)。
第2…R+1行: 每行一个由C个字符组成的字符串,共同描绘出牧场地图。
输出格式
一个整数,表示跳跃的最小次数。
代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
inline int gcd(int a, int b) {
return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
return x & -x; }
inline LL read() {
LL f = 1; LL x = 0;char ch = getchar();while (ch > '9' || ch < '0') {
if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f; }
const int N = 1010;
char g[N][N];
int dis[N][N];
int n, m;
int posx = 0, posy = 0;
int bfs(int posx,int posy) {
queue<PII>q;
q.push({
posx,posy });
memset(dis, -1, sizeof dis);
dis[posx][posy] = 0;
int dx[] = {
-2,-1,1,2,2,1,-1,-2 };
int dy[] = {
1,2,2,1,-1,-2,-2,-1 };
while (q.size()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 8;++i) {
int xx = t.first + dx[i];
int yy = t.second + dy[i];
if (xx < 0 || xx >= n || yy < 0 || yy >= m)continue;
if (g[xx][yy] == '*' || dis[xx][yy] != -1)continue;
if (g[xx][yy] == 'H')return dis[t.first][t.second] + 1;
q.push({
xx,yy });
dis[xx][yy] = dis[t.first][t.second] + 1;
}
}
}
int main() {
m = read();
n = read();
for (int i = 0;i < n;++i)
cin >> g[i];
for (int i = 0;i < n;++i) {
for (int j = 0;j < m;++j) {
if (g[i][j] == 'K')posx = i, posy = j;
}
}
cout << bfs(posx, posy) << endl;
return 0;
}
AcWing1100. 抓住那头牛
农夫知道一头牛的位置,想要抓住它。
农夫和牛都位于数轴上,农夫起始位于点 N,牛位于点 K。
农夫有两种移动方式:
- 从 X 移动到 X−1 或 X+1,每次移动花费一分钟
- 从 X 移动到 2∗X,每次移动花费一分钟
假设牛没有意识到农夫的行动,站在原地不动。
农夫最少要花多少时间才能抓住牛?
输入格式
共一行,包含两个整数N和K。
输出格式
输出一个整数,表示抓到牛所花费的最少时间。
数据范围
0 ≤ N , K ≤ 105 0≤N,K≤105 0≤N,K≤105
代码
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
inline int gcd(int a, int b) {
return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
return x & -x; }
inline LL read() {
LL f = 1; LL x = 0;char ch = getchar();while (ch > '9' || ch < '0') {
if (ch == '-') f = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();return x * f; }
const int N = 100100;
int n, k;
int dis[N];
int solve() {
queue<int>q;
q.push(n);
memset(dis, -1, sizeof dis);
dis[n] = 0;
while(q.size()) {
int t = q.front();
q.pop();
if (t == k)return dis[k];
if (t - 1 >= 0 && dis[t - 1] == -1) {
q.push(t - 1);
dis[t - 1] = dis[t] + 1;
}
if (t + 1 < N && dis[t + 1] == -1) {
q.push(t + 1);
dis[t + 1] = dis[t] + 1;
}
if (t * 2 < N && dis[t * 2] == -1) {
q.push(t * 2);
dis[t * 2] = dis[t] + 1;
}
}
return -1;
}
int main() {
n = read();
k = read();
cout << solve() << endl;
return 0;
}