其实这个才是我做 kuangbin 带你飞专题训练的第一个专题,只不过是在做完了 最短路 之后才想起来可以写个博客记录一下。写完最短路之后,就顺便把 简单搜索补上了…
简单搜索专题,基本上只用到了 DFS 和 BFS,因此以下主要介绍 DFS 和 BFS,然后针对具体的题目给出代码。
DFS
深度优先搜索是一种常用的搜索方法,从开始状态出发,不断的转移至下一个状态直到无法转移,然后回退到前一步的状态,继续转移至其他状态,如此不断重复,直到得到最终的解。根据 DFS 搜索的特点,常用递归函数来实现。
DFS 一般来求符合某个条件的状态集合,回溯法就是使用深度优先搜索的一种算法。
经典例题 HDU 1241 Oil Deposits
解题思路:这道题就是用 DFS 遍历以某一个标记为 ‘@’ 位置为起点的周围所有 ‘@’ 标记的区域,遍历过后将 ‘@’ 标记变为 ‘#’,’@’ 区域的个数就是起点的个数。
#include <cstdio>
#include <cstring>
using namespace std;
int const maxn = 110;
int mov[][2] = {{1, 0},{-1, 0},{0, 1},{0, -1},{1, 1},{1, -1},{-1, 1},{-1, -1}};
int M, N;
char grid[maxn][maxn];
void dfs(int , int);
int main()
{
while (scanf("%d%d", &M, &N), M)
{
int t = 0;
memset(grid, 0, sizeof(grid));
for (int i = 0;i < M;i++)
scanf("%s", grid[i]);
for (int i = 0;i < M;i++)
{
for (int j = 0;j < N;j++)
if (grid[i][j] == '@')
{
dfs(i, j);
t += 1;
}
}
printf("%d\n", t);
}
return 0;
}
void dfs(int x, int y)
{
if (x>=M || x<0 || y>=N || y<0)
return ;
if (grid[x][y] != '@')
return ;
grid[x][y] = '#';
for (int i = 0; i < 8;i++)
{
int nx, ny;
nx = x + mov[i][0];
ny = y + mov[i][1];
dfs(nx, ny);
}
}
BFS
广度优先搜索也是搜索的手段之一,它与深度优先搜索类似,从某个状态出发搜索所有可以到达的状态。
与深度优先搜索不同的是搜索的顺序,广度优先搜索总是先搜索距离初始状态近的状态。也就是说,他按照 开始状态 -> 只需一次转移就可以到达的所有状态 -> 只需两次转移就可以到达的所有状态 -> …
对于同一个状态,广度优先搜索之经过一次,因此复杂度为 O(状态数 × 转移的方式)。
经典例题 POJ 2251 : Dungeon Master
解题思路: 这道题就是用普通的 BFS,只不过是将节点拓展的方式变成了 6 个方向:上、下、东、西、南、北
#include <cstdio>
#include <climits>
#include <algorithm>
#include <queue>
#define MAX_SZ 32
#define MOVE 6
using namespace std;
struct Node{
int l, r, c;
Node(int l = 0,int r = 0,int c = 0) :l(l),r(r),c(c) {};
};
int bfs();
char dungeon[MAX_SZ][MAX_SZ][MAX_SZ];
int d[MAX_SZ][MAX_SZ][MAX_SZ];
int dl[] = {0, 0, 0, 0, 1, -1};
int dr[] = {0, 0, 1, -1, 0, 0};
int dc[] = {1, -1, 0, 0, 0, 0};
int L, R, C;
int stat_l, stat_r, stat_c;
int end_l, end_r, end_c;
int main()
{
while (scanf("%d %d %d", &L, &R, &C), L+R+C)
{
for (int i = 0;i < L;i++)
{
for (int j = 0;j < R;j++)
{
scanf("%s", dungeon[i][j]);
for (int k = 0;k < C;k++)
{
if (dungeon[i][j][k] == 'S')
{
stat_l = i;
stat_r = j;
stat_c = k;
}
if (dungeon[i][j][k] == 'E')
{
end_l = i;
end_r = j;
end_c = k;
}
d[i][j][k] = INT_MAX;
}
}
}
int result = bfs();
if (result == 0)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", result);
}
return 0;
}
int bfs()
{
queue<Node> que;
d[stat_l][stat_r][stat_c] = 0;
que.push(Node(stat_l, stat_r, stat_c));
while (!que.empty())
{
Node n = que.front(); que.pop();
if (n.l == end_l && n.r == end_r && n.c == end_c)
return d[end_l][end_r][end_c];
for (int i = 0;i < MOVE;i++)
{
int nl = n.l + dl[i], nr = n.r + dr[i], nc = n.c + dc[i];
if (nc >= 0 && nc < C && nr >= 0 && nr < R && nl >= 0 && nl < L
&& dungeon[nl][nr][nc] != '#' && d[nl][nr][nc] == INT_MAX)
{
que.push(Node(nl, nr, nc));
d[nl][nr][nc] = d[n.l][n.r][n.c] + 1;
}
}
}
return 0;
}
搜索与路径记录
有的题目会要求输出路径,这个时候一般需要用一个结构体数组来存储状态信息,比如说可以存储到初始状态的距离和当前状态的前一个状态。最后再用 DFS 来遍历一遍这个数组,将路径输出。
经典例题 POJ 3984 迷宫问题
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> P;
struct node{
int px, py, l;
}path[5][5];
int maze[5][5];
int route[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void dfs(int x, int y)
{
if (x == 0 && y == 0)
{
printf("(0, 0)\n");
return ;
}
dfs(path[x][y].px, path[x][y].py);
printf("(%d, %d)\n", x, y);
}
void bfs()
{
queue<P> Q;
Q.push(P(0, 0));
path[0][0].l = 0;
while (!Q.empty())
{
P c = Q.front(); Q.pop();
int x = c.first;
int y = c.second;
if (x == 4 && y == 4)
dfs(x, y);
for (int i = 0; i < 4;i++)
{
int nx = x + route[i][0];
int ny = y + route[i][1];
if (nx<0 || ny<0 || nx>=5 || ny >= 5)
continue;
if (path[nx][ny].l != -1)
continue;
if (maze[nx][ny] == 1)
continue;
path[nx][ny].px = x;
path[nx][ny].py = y;
path[nx][ny].l = path[x][y].l + 1;
Q.push(P(nx, ny));
}
}
}
int main()
{
for (int i = 0;i < 5;i++)
{
for (int j = 0;j < 5;j++)
scanf("%d", &maze[i][j]);
}
memset(path, -1, sizeof(path));
bfs();
return 0;
}
285

被折叠的 条评论
为什么被折叠?



