跳到正文

个人记录

搜索

AmaoFox约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月11日
篇幅
约 9 分钟

版本

  1. v1当前

搜索

2021/7/28 15:27

类似于递推
关键点:状态和转移
按做法分类可以分为以下四种:

  1. 全排列
  2. 组合
  3. 子集
  4. 网格图

1. 全排列搜索

枚举1∼n1 \sim n的所有全排列

“下一位”可以使用任何数字
只要没被使用过.

复杂度

枚举全排列:O(n!)O(n!)
输出全排列:O(n)O(n)
总时间复杂度:O(n×n!)O(n \times n!)
储存全排列:O(n)O(n)
标记是否用过:O(n)O(n)
总空间复杂度:O(n)O(n)

C++
#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. 组合搜索

枚举从 1∼n1 \sim n 中挑选 mm 个元素的组合.

“下一位”必须比“这一位”大

复杂度

枚举所有组合:O(Cnm)O(C^m_n)
输出一个组合:O(m)O(m)
总时间复杂度:O(m×Cnm)O(m \times C^m_n)
保存组合:O(m)O(m)
总空间复杂度:O(m)O(m)

C++
#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;
}

状态是还没生成完全的组合,转移是往末尾添加一个比末尾大的数.

子序列(以最长上升子序列为例)

本质上还是组合搜索
生成所有子序列:O(2n)O(2^n)
总时间复杂度:O(2n)O(2^n)
递归n次:O(n)O(n)
记录原序列:O(n)O(n)
总空间复杂度:O(n)O(n)

C++
#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;
}

状态是(last,n)(last, n),转移是在末尾添加一个新元素.

3. 子集搜索

枚举所有子集.

一个元素就是选和不选.

以01背包为例.

复杂度

储存原序列:O(n)O(n)
生成所有子集:O(2n)O(2^n)
状态是(x,sumw,sumv)(x, sumw, sumv),转移是选或不选第xx个元素.

4. 网格图搜索

没有固定的时空复杂度,不能进行计算.

C++
#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;
}

状态是一个(x,y)(x,y),转移是走了一步.

2021/7/29 15:41

按问题分类可以分为以下三种:

  1. 遍历问题

遍历问题

遍历每个状态.

复杂度

记录迷宫:O(n×m)O(n\times m)
总时间复杂度:O(n×m)O(n\times m)
每个状态遍历一次:O(n×m)O(n\times m)
每个状态转移四次:O(1)O(1)
总时间复杂度:O(n×m)O(n\times m) (状态数+转移数)

C++
#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;
}

状态是格子坐标(x,y)(x,y), 状态是上下左右走.

连通性问题

遍历每块状态.

复杂度

记录迷宫:O(n×m)O(n\times m)
总时间复杂度:O(n×m)O(n\times m)
每个状态遍历一次:O(n×m)O(n\times m)
每个状态转移四次:O(1)O(1)
总时间复杂度:O(n×m)O(n\times m) (状态数+转移数)

状态是格子坐标(x,y,z)(x,y,z),马走 zz 步到了(x,y)(x,y) 状态是上下左右走.

最优性问题

区别于遍历问题,所有可能的搜索路径都要搜一遍.
状态是马的坐标,转移是马的跳跃方向.
普通剪枝:O(8nm)O(8^{nm}) 高级剪枝:快许多倍(不能预计) 记录答案:O(n,m)O(n,m)
总空间复杂度:O(n,m)O(n,m)

北京市第二算法大队温馨提醒您:代码千万条,仔细第一条;读题不规范,导师两行泪.
原文
存档评论2 条抓取于