搜索的过程最后会生成一颗搜索树
剪枝,顾名思义,就是通过一些判断,砍掉搜索树上不必要的子树,有时候,我们会发现某个结点对应的子树的状态都不是我们要的结果,那么我们没有必要对这个分支进行搜索,砍掉这个子树,就是剪枝
最优性剪枝
对于求最优解的一类问题,通常可以用最优性剪枝,比如在求解迷宫最短路的时候,如果发现当前的步数已经超过了当前最优解,那么当前状态的搜索都是多余的,因为这样搜索下去永远都搜不到更优的解,通过这样的剪枝,可以省去大量多余的计算
此外,在搜索是否有可行性解的过程中,一旦找到了一组可行解,后面所以的搜索都不必再进行了,这算是最优性剪枝的一个特例
迷宫问题2
n,m分别为迷宫的行数和列数,maze记录迷宫的地形,vis记录dfs过程中当前位置是否被访问过,dir表示每次枚举的四个方向,ans记录当前到达终点的最小的步数,初始值为一个较大的数
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int n, m;
string maze[110];
bool vis[100][100];
int dir[4][2] = { {-1,0},{0,-1},{1,0},{0,1} };
int ans = 10000;
bool in(int x, int y)
{
return 0 <= x && x < n && 0 <= y && y < m;
}
void dfs(int x, int y, int step)
{
if (step >= ans)
{
return;
}
if (maze[x][y] == 'T')
{
ans = step;
return;
}
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty])
{
dfs(tx, ty, step + 1);
}
}
vis[x][y] = 0;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> maze[i];
}
int x, y;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (maze[i][j] == 'S')
{
x = i;
y = j;
}
}
}
dfs(x, y, 0);
cout << ans << endl;
}
重复性剪枝
对于某一些特定的搜索方式,一个方案可能会被搜索很多次,这样是没必要的
比如:
给定n个整数,要求选出K个数,使得选出来的k个数的和为sum
如果搜索方式是每次从剩下的数里选一个数,一个搜到第k层,那么1,2,3这个选取方法能被搜索到6次,这是没必要的,我们只需要关注选出来的数的顺序,所以这里可以用重复性剪枝
#include <iostream>
using namespace std;
int n, k, sum, ans;
int a[40];
bool xuan[40];
void dfs(int s, int cnt,int pos)
{
if (s > sum || cnt > k)
{
return;
}
if (s == sum && cnt == k)
{
ans++;
}
for (int i = pos; i < n; i++)
{
if (!xuan[i])
{
xuan[i] = 1;
dfs(s + a[i], cnt + 1,i+1);
xuan[i] = 0;
}
}
}
int main()
{
n = 30;
k = 8;
sum = 200;
for (int i = 0; i < 30; i++)
{
a[i] = i + 1;
}
ans = 0;
dfs(0, 0,0);
cout << ans << endl;
return 0;
}
奇偶性剪枝
有一个nm大小的迷宫,S表示起点,D表示出口,X表示墙壁,. 表示平地。你需要从S出发走到D。每次只能上下左右相邻的位置移动,并且不能走出地图,也不能走出墙壁
每次移动消耗1时间,走过路都会崩塌,因此不能回头或者原地不动。现在已知出口的大门会在T时间打开,判断在0时间从起点出发能否逃离迷宫
可将nm的网络黑白两色,我们计每个格子的行数和列数之和为x,如果x为偶数,那么格子就是白色,反之为黑色。容易发现相邻的两个格子的颜色肯定不相同,也就是说每走一步颜色都不相同。更普遍的结论为:走奇数步会改变颜色,走偶数步颜色不变
那么如果起点和终点的颜色相同,而T是奇数的话,就不可能逃离迷宫,如果起点和终点的颜色不一样,而T是偶数的话,也不能逃离迷宫,遇到这两种情况,就不用DFS了,直接输入NO
这样的剪枝就是奇偶性剪枝,本质也属于可行性剪枝
#include <iostream>
using namespace std;
const int N = 10;
int n, m, T;
char mat[N][N];
bool vis[N][N];
int dx[4] = { 0,0,-1,1 };
int dy[4] = { 1,-1,0,0 };
bool ok;
void dfs(int x, int y, int t)
{
if (ok)
{
return;
}
if (t == 'T')
{
if (mat[x][y] == 'D')
{
ok = true;
}
return;
}
vis[x][y] = true;
for (int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= m || mat[tx][ty] == 'x' || vis[tx][ty])
{
continue;
}
dfs(tx, ty, t + 1);
}
vis[x][y] = false;
}
int main()
{
cin >> n >> m >> T;
for (int i = 0; i < n; i++)
{
cin >> mat[i];
}
int sx, sy, ex, ey;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mat[i][j] == 'S')
{
sx = i;
sy = j;
}
if (mat[i][j] = 'D')
{
ex = i;
ey = j;
}
}
}
if ((sx + sy + ex + ey + T) % 2 != 0)
{
cout << "NO" << endl;
}
else
{
ok = false;
dfs(sx, sy, 0);
if (ok)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
}