ACM寒假集训DFS/BFS专题
DFS(深度优先搜索)
深度优先搜索大致为(1)递归下去 (2)回溯上来.顾名思义,以深度为准则,先一条路走下去,直到达到目标.无路可走后,便退回到上一步的状态,走其他路.
BFS(广度优先搜索)
广度优先搜索大致为面临路口时,把所有的岔路A,B,C…都记下来,然后选择其中一个岔路(如:A)进入,并将它的岔路a,b,c…记录下来,再返回来进入另外一个岔路(如:B),并重复这样的操作.
部分题解如下 :
Lake Countin
Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (’.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John’s field, determine how many ponds he has.
Input
- Line 1: Two space-separated integers: N and M
- Lines 2…N+1: M characters per line representing one row of Farmer John’s field. Each character is either ‘W’ or ‘.’. The characters do not have spaces between them.
Output
- Line 1: The number of ponds in Farmer John’s field.
大致题意
有一个大小为N*M的园子,雨后部分格子积了水,八连通的积水被认为是在一起的,求园子共有多少个水池.
思路
顺序遍历所有格子,遇到’w’的时候,池子的数量加1,然后使用DFS把自身及邻接的’w’用其他字符代替(一次DFS后,与初始的这个 w 所连接的所有 ‘w’ 都会被替换成其他字符).
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define finc(i,a,b) for(int i=(a);i<(b);i++)
#define fdec(i,a,b) for(int i=(b);i-->(a);)
char m[128][128];
void cls(int i, int j)
{
m[i][j] = 0;
finc(k, -1, 2)
finc(l, -1, 2)
if (m[i + k][j + l] == 'W')
cls(i + k, j + l);
}
int main(int argc, char* argv[])
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int test = 1; //cin >> test;
while (test--)
{
int a, b; cin >> a >> b;
finc(i, 1, a + 1)
finc(j, 1, b + 1)
cin >> m[i][j];
int ans = 0;
finc(i, 1, a + 1)
finc(j, 1, b + 1)
if (m[i][j] == 'W')
++ans, cls(i, j);
cout << ans << endl;
}
return 0;
}
Red and Black
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
大致题意
一个n*m的表格,其中一个格子是起点,其余格子是红色或黑色,人物不能跳到红色的格子,求人物能跳到的格子的数目.
思路
与上题思路差异不大,从起点开始,把自身及邻接的’.'用一个"特殊"的字符(如:1)代替,再顺序遍历,统计该字符的数量.
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define finc(i,a,b) for(int i=(a);i<(b);i++)
#define fdec(i,a,b) for(int i=(b);i-->(a);)
char a[128][128];
void cls(int i, int j)
{
a[i][j] = 1;
finc(k, -1, 2)
finc(l, -1, 2)
if (k * l == 0 && a[i + k][j + l] == '.')
cls(i + k, j + l);
}
int main(int argc, char* argv[])
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m; cin >> m >> n;
//int test = 1; cin >> test;
while (n)
{
finc(i, 1, n + 1)
finc(j, 1, m + 1)
cin >> a[i][j];
finc(i, 1, n + 1)
finc(j, 1, m + 1)
if (a[i][j] == '@')
cls(i, j);
int ans = 0;
finc(i, 1, n + 1)
finc(j, 1, m + 1)
if (a[i][j] == 1)
ans++;
cout << ans << endl;
finc(i, 1, n + 1)
finc(j, 1, m + 1)
a[i][j] = 0;
cin >> m >> n;
}
return 0;
}