浅谈 dfs 深搜

· · 算法·理论

普及组 DFS (胖头鱼笔记)

大致分 4 类

1、排列问题

2、组合问题

3、连通块问题

4、爆搜

DFS 一般写法

1、设计状态

2、先判合法性

3、找目标状态

3、处理当前状态

排列问题 全排列

本质上就是将 n 个数填到 n 个空中。

#include <bits/stdc++.h>
using namespace std;

const int N = 12;
int n, a[N], b[N];

void dfs(int l) {
    if (l == n) {
        for (int i = 0; i < n; i++) {
            cout << fixed << setw(5) << a[i];
        }
        cout << '\n';
        return;
    }
    for (int i = 1; i <= n; i++) {
        if (!b[i]) {
            b[i] = 1;
            a[l] = i;
            dfs(l + 1);
            b[i] = 0;
        }
    }
}

int main() {
    cin >> n;
    dfs(0);
    return 0;
}

组合问题 选数

从 n 个数中选 k 个数。

有两种写法:

  1. 只考虑选的元素
    • 需要记录上一次选的元素
    • 从上一个元素转移到下一个要选的元素
    • 需要写循环
  2. 考虑每个元素选还是不选
    • 不需要写循环
    • i+1 是由 i 转移来的,可以考虑滚动优化

【写法1】

#include<bits/stdc++.h>
using namespace std;

const int N = 12;
int n, k, ans, a[N]; 

int isP(int n) {
    for (int i = 2; i *i <= n; i++) {
        if (n % i == 0) {
            return 0;
        }
    }
    return n > 1;
}

void dfs(int cnt, int starti, int s) {   // cnt:当前选了几个数, starti:上一次选的数 
    if (cnt == k) {                      // 已经选了 k 个数 
        ans += isP(s);  
        return;
    }
    for (int i = starti; i <= n; i++) {
        dfs(cnt + 1, i + 1, s + a[i]);
    }
}

int main(){
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    dfs(0, 1, 0);
    cout << ans;
    return 0;
}

【写法2】

#include<bits/stdc++.h>
using namespace std;

const int N = 12;
int n, k, ans, a[N]; 

int isP(int n) {
    for (int i = 2; i *i <= n; i++) {
        if (n % i == 0) {
            return 0;
        }
    }
    return n > 1;
}

void dfs(int l, int s, int cnt) {   // l:当前处理了几个元素, s表示当前的和 ,cnt 表示选了几个元素 
    if (l == n) {                   // 所有元素都被处理了 
        if (cnt == k && isP(s)) {   
            ans++;
        }
        return; 
    }
    dfs(l + 1, s + a[l + 1], cnt + 1);  // 选
    dfs(l + 1, s, cnt);                 // 不选
}

int main(){
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    dfs(0, 0, 0);
    cout << ans;
    return 0;
}

爆搜 迷宫游戏

每一步都有几种不同的操作,每一步都是独立的,将不同的操作都搜一遍。

#include<bits/stdc++.h>
using namespace std;

const int N = 105;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, ans, v[N][N], g[N][N];
char ch ;

void dfs(int x, int y) {
    if (g[x][y] != '.' || v[x][y]) {
        return;
    }
    ans++;
    v[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++) {
            cin >> g[i][j];
        }
    }
    dfs(1, 1);
    cout << ans;
    return 0;
}

连通块问题 水坑数量

从一个点出发,搜出一整个连通块。

#include<bits/stdc++.h>
using namespace std;

const int N = 105;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int n, m, ans, v[N][N];
char g[N][N];

void dfs(int x, int y) {
    if (g[x][y] != 'W' || v[x][y]) {
        return;
    }
    v[x][y] = 1;
    for (int i = 0; i < 8; 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++) {
            cin >> g[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (g[i][j] == 'W' && !v[i][j]) {
                ans++;
                dfs(i, j);
            }
        }
    }
    cout << ans;
    return 0;
}