搜索
2021/7/28 15:27
类似于递推
关键点:状态和转移
按做法分类可以分为以下四种:
- 全排列
- 组合
- 子集
- 网格图
1. 全排列搜索
枚举的所有全排列
“下一位”可以使用任何数字
只要没被使用过.
复杂度
枚举全排列:
输出全排列:
总时间复杂度:
储存全排列:
标记是否用过:
总空间复杂度:
#include <iostream>
using namespace std;
int p[MAXN];//保存全排列
bool used[MAXN];//标记数字是否使用过.
int n;
void dfs(int x) {
if(x == n + 1) {//有一个全排列
for(int i = 1; i <= n; i++) {//输出
cout << a[i] << ' ';
}
cout << endl;
return;
}
for(int i = 1; i <= n; i++) {
if(!used[i]) {//没被使用过
used[i] = 1;//搜索
p[x] = i;//放入
dfs(x + 1);//递归
used[i] = 0;//回溯
}
}
}
int main() {
cin >> n;
dfs(1);
return 0;
}状态是还未生成完全的排列,转移是在该序列末尾添加一个数字.
2. 组合搜索
枚举从 中挑选 个元素的组合.
“下一位”必须比“这一位”大
复杂度
枚举所有组合:
输出一个组合:
总时间复杂度:
保存组合:
总空间复杂度:
#include <iostream>
using namespace std;
int p[MAXM]/*用于保存排列*/, n, m;
void dfs(int x, int last) {
if(x == m + 1) {//找到一个组合
for(int i = 1; i <= m; i++) {//输出
cout << p[i] << ' ';
}
cout << endl;
}
for(int i = last + 1; i <= n; i++) {
//每次都添加一个比最后一项大的数
a[x] = i;
dfs(x + 1, i);
}
}
int main() {
cin >> n >> m;
dfs(1, 0);
return 0;
}状态是还没生成完全的组合,转移是往末尾添加一个比末尾大的数.
子序列(以最长上升子序列为例)
本质上还是组合搜索
生成所有子序列:
总时间复杂度:
递归n次:
记录原序列:
总空间复杂度:
#include <iostream>
using namespace std;
int a[MAXN], n, ans;
void dfs(int x, int last) {
ans = max(ans, x);
for(int i = last + 1; i <= n; i++) {
//转移到新的序列
if(a[i] > a[last]) {
dfs(x + 1, i);
}
}
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
a[0] = -1;
dfs(1, 0);
cout << ans;
}状态是,转移是在末尾添加一个新元素.
3. 子集搜索
枚举所有子集.
一个元素就是选和不选.
以01背包为例.
复杂度
储存原序列:
生成所有子集:
状态是,转移是选或不选第个元素.
4. 网格图搜索
没有固定的时空复杂度,不能进行计算.
#include <iostream>
using namespace std;
const int N = 20;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};
struct Node{
int x, y;
}ans[N * N];
bool a[N][N], vis[N][N];
bool f;
int sx, sy, fx, fy;
int n, m;
void dfs(int x, int y, int c) {
if(x < 1 || y < 1 || x > n || y > m || vis[x][y] || !a[x][y]) {
return;
}
ans[c].x = x, ans[c].y = y;
if(x == fx && y == fy) {
f = 1;
for(int i = 0; i < c; i++) {
cout << '(' << ans[i].x << ',' << ans[i].y << ")->";
}
cout << '(' << ans[c].x << ',' << ans[c].y << ')' << '\n';
return;
}
vis[x][y] = 1;
for(int i = 0; i < 4; i++) {
dfs(x + dx[i], y + dy[i], c + 1);
}
vis[x][y] = 0;
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
cin >> sx >> sy >> fx >> fy;
dfs(sx, sy, 0);
if(!f) {
cout << -1;
}
return 0;
}状态是一个,转移是走了一步.
2021/7/29 15:41
按问题分类可以分为以下三种:
- 遍历问题
遍历问题
遍历每个状态.
复杂度
记录迷宫:
总时间复杂度:
每个状态遍历一次:
每个状态转移四次:
总时间复杂度: (状态数+转移数)
#include <iostream>
using namespace std;
const int N = 105;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m, ans;
bool a[N][N], vis[N][N];
void dfs(int x, int y) {
if(x < 1 || y < 1 || x > n || y > m || vis[x][y] || a[x][y]) {
return;
}
ans++;
vis[x][y] = 1;
for(int i = 0; i < 4; i++) {
dfs(x + dx[i], y + dy[i]);
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
char c;
cin >> c;
if(c == '#') {
a[i][j] = 1;
} else {
a[i][j] = 0;
}
}
}
dfs(1, 1);
cout << ans;
return 0;
}状态是格子坐标, 状态是上下左右走.
连通性问题
遍历每块状态.
复杂度
记录迷宫:
总时间复杂度:
每个状态遍历一次:
每个状态转移四次:
总时间复杂度: (状态数+转移数)
状态是格子坐标,马走 步到了 状态是上下左右走.
最优性问题
区别于遍历问题,所有可能的搜索路径都要搜一遍.
状态是马的坐标,转移是马的跳跃方向.
普通剪枝:
高级剪枝:快许多倍(不能预计)
记录答案:
总空间复杂度: