算法笔记 第8章 提高篇(2) --搜索问题 学习笔记

8.1 深度优先搜索(DFS)

深度优先搜索会走遍所有路径,并且每次走到死胡同就代表一条完整路径的形成。这就是说,深度优先搜索是一种枚举所有完整路径以遍历所有情况的搜索方法。

例子(背包问题):

  有n件物品,每件物品的重量w[i],价值为c[i]。现在需要选出若干件物品放入一个容量为V的背包中,使得在选入背包的物品重量和不超过容量V的前提下,让背包中物品的价值之和最大,求最大价值。(1<= n <= 20)

#include<cstdio>
const int maxn = 30;
int n,V,maxValue = 0; //物品件数n,背包容量V,最大价值maxValue
int w[maxn],c[maxn]; //w[i]为每件物品的重量,c[i]为每件物品的价值
//DFS,index为当前处理的物品编号
//sumW和sumC分别为当前总重量和当前总价值
void DFS(int index,int sumW,int sumC){
    if(index == n){ //已经完成对n件物品的选择(死胡同) 
        if(sumW <= V && sumC > maxValue){
            maxValue = sumC; //不超过背包容量时更新最大价值maxValue 
        }
        return;
    }
    //岔道口 
    DFS(index+1,sumW,sumC); //不选第index件物品 
    DFS(index+1,sumW+w[index],sumC+c[index]); //选第index件物品 
} 
int main(){
    scanf("%d%d",&n,&V);
    for(int i = 0;i < n; i++){
        scanf("%d",&w[i]);
    }
    for(int i = 0;i < n; i++){
        scanf("%d",&c[i]);
    }
    DFS(0,0,0);
    printf("%d
",maxValue);
    return 0;
} 

 

上面的代码的复杂度为O(2^n),这看起来不是很优秀。可以改成如下代码:

void DFS(int index,int sumW,int sumC){
    if(index == n){
        return; //已经完成对n件物品的选择 
    }
    DFS(index + 1,sumW,sumC);//不选第index件物品 
    //只有加入第index件物品后未超过容量V,才能继续
    if(sumW + w[index] <= V){
        if(sumC + c[index] > ans){
            ans = sumC + c[index];
        }
        DFS(index+1,sumW + w[index],sumC + c[index]); //选第index件物品 
    } 
}

8.2 广度优先搜索(BFS)

广度优先搜索则是以广度为第一关键词,当碰到岔道口时,总是先依次访问从该岔道口能直接到达的所有结点,然后再按这些结点被访问的顺序去依次访问它们能直接到达的所有结点,以此类推,直到所有结点都被访问为止。

例子:

给出一个m×n的矩阵,矩阵中的元素为0或1.称位置(x,y)与其上下左右四个位置(x,y+1)、(x,y-1)、(x+1,y)、(x-1,y)是相邻的。如果矩阵中有若干个1是相邻的(不必两两相邻),那么称这些1构成了一个“块”。求给定的矩阵中“块”的个数。

0 1 1 1 0 0 1

0 0 1 0 0 0 0

0 0 0 0 1 0 0

0 0 0 1 1 1 0

1 1 1 0 1 0 0

1 1 1 1 0 0 0

上面的6×7的矩阵中,“块”的个数为4.

#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 100;
struct node{
    int x,y;
}Node;

int n,m; //矩阵大小为n*m 
int matrix[maxn][maxn]; //01矩阵 
bool inq[maxn][maxn] = {false}; //记录位置(x,y)是否已入过队 
int X[4] = {0,0,1,-1}; //增量数组 
int Y[4] = {1,-1,0,0};

bool judge(int x,int y){ //判断坐标(x,y)是否需要访问
    //越界访问false 
    if(x >= n || x<0 || y >= m || y < 0)
        return false;
    //当前位置为0,或(x,y)已入过队,返回false 
    if(matrix[x][y]==0 || inq[x][y] == true)
        return false;
    //以上都不满足,返回true 
    return true;
}
//BFS函数访问位置(x,y)所在的块,将该块中所有"1"的inq都设置为true 
void BFS(int x,int y){
    queue<node> Q; //定义队列 
    Node.x = x;    //当前结点的坐标为(x,y) 
    Node.y = y;
    Q.push(Node); //将结点Node入队 
    inq[x][y] = true; //设置(x,y)已入过队 
    while(!Q.empty()){
        node top = Q.front(); //取出队首元素 
        Q.pop();  //队首元素出队 
        for(int i = 0; i < 4;i++){  //循环4次,得到4个相邻位置 
            int newX = top.x + X[i];
            int newY = top.y + Y[i];
            if(judge(newX,newY)){ //如果新位置(newX,newY)需要访问 
                Node.x = newX,Node.y = newY;
                Q.push(Node);  //将结点Node加入队列 
                inq[newX][newY] = true; //设置位置(newX,newY)已入过队 
            }
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int x = 0; x < n; x++){
        for(int y = 0; y < m; y++){
            scanf("%d",&matrix[x][y]); //读入01矩阵 
        }
    }
    int ans = 0; //存放块数 
    for(int x = 0; x < n; x++){  //枚举每一个位置 
        for(int y = 0; y < m;y++){
            if(matrix[x][y] == 1 && inq[x][y] == false){
                ans++; //块数加1 
                BFS(x,y); //访问整个块,将该块所有"1"的inq都标记为true 
            }
        }
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/coderying/p/12262540.html