BFS / DFS breadth / depth-first search


Depth-first search DFS

The so-called depth-first search, popular point is to understand the head went one way - do not hit the brick wall does not look back.

Let's look at a full array issue, now on 123 full-arranged, now holding the hands of small hum 123 three cards, he will give this three cards into three boxes, each filled It is not a fully arranged yet?


But in the end every time the card is first put 1 or 3 card it?

Well think small, I put it in order, and each time put the card in the order 1.2.3. So he went to the former No. 1 box 1 into the card, went to No. 2 before the card 2 into the box, went to No. 3 before the card 3 into the box, but little came fourth box ... hum card has been done and

La. Then arose a full array " 123 " friends.

But there is a good variety of circumstances ah, so have the full array of small hum latter to return immediately, before he returned to the No. 3 box, retrieve the card 3, see also not put other cards, revealing a small hum hands now In addition to the card 3 is not no other external cards, so little hum continue

Go back to the No. 2 box before, 2 small hum recover the card, which is the hand of the little hum has two cards, so he put the card 3 No. 2 box, then put away to take a step back Three box, put the card 2, the top four went to the box, of course not

Fourth box, so he produces a full array " 132 "

Thus according to the simulation, it will in turn generate a full-arrangement "213" "231" "312" "321"

Said a long time, we look at how to use code to achieve it

I number of cards in the first step of the boxes

for(i = 1; i <=n; i++)
    {
        a[step] = i;  //将卡片i放入第step个盒子里
    }

But there's a problem, how a card has been placed in a box in the other, you can not put the current box. So, we need a book [] array to mark this card hand is there

for(i = 1; i <= n; i++)
    {
        if(book[i] == 0) / /如果手上有这张卡片
        {
            a[step] = i; //将卡片i放入第step个盒子里
            book[i] = 1; //此时手上已经没有这张卡了 标记一下
        }              
   }

After processing method of the step boxes we want to go back one step, the first processing step + 1 boxes, apparently the first processing step + 1 boxes and boxes as the first step, then we will just deal with the first step of boxes written as a function method

void dfs(int step)
{
    for(i = 1; i <= n; i++)
        {
            if(book[i] == 0) / /如果手上有这张卡片
            {    
                a[step] = i; //将卡片i放入第step个盒子里
                book[i] = 1; //此时手上已经没有这张卡了 标记一下
            }              
       }
}

Treatment step + 1 boxes only dfs (step + 1) on the line

void dfs(int step)
{
    for(i = 1; i <= n; i++)
        {
            if(book[i] == 0) / /如果手上有这张卡片
            {    
                a[step] = i; //将卡片i放入第step个盒子里
                book[i] = 1; //此时手上已经没有这张卡了 标记一下
            }          
            dfs(step+1);//处理下个盒子
            book[i] = 0; //这步很重要!!往回走时要将之前的卡片收回!
       }
}

book[i] = 0;是将之前的卡片收回,如果不把刚才放入盒子的卡片收回,那么就无法再进行下一次摆放。
还剩最后一个问题,什么时候输出一个满足条件的序列呢?其实当我们走到第n+1个盒子前时,说明前n个盒子都已经放好卡片了,这时候我们就要return返回了


void dfs(int step)
{
    if (step == n + 1) //判断边界
    {
        for (i = 1; i <= n; i++)
            cout << a[i] << " ";
        cout << endl;
        return;//返回上一个盒子前
    }
    else
    {
        for (i = 1; i <= n; i++)
        {
            if (book[i] == 0) / /如果手上有这张卡片
            {
                a[step] = i; //将卡片i放入第step个盒子里
                book[i] = 1; //此时手上已经没有这张卡了 标记一下
            }
            dfs(step + 1);//处理下个盒子
            book[i] = 0; //这步很重要!!往回走时要将之前的卡片收回!
        }
    }
}

讲到这里想必大家已经对深度优先搜索有一定了解了吧,深搜使用就是递归和回溯的思想。

广度优先搜索BFS

讲完了dfs,我们再来看看什么是广度优先搜索。

所谓广度优先搜索就是一种层层递进的搜索。

我们来看一张图。这是一个迷宫,我们用一个二维数据储存它。

现在小哼站在(1 1)处,它可以向下或向右走,如何才能到达终点呢?已经学会深搜的你因该很快就想到办法了,但这里我们用另一种方法BFS,“一层层”扩展找到终点。扩展时每发现一个点就将这个点放入队列中,知道走到终点位置。和深搜一样,我们也需要一个book数组记录是否走过这个点。

我们每对一个点扩展完毕,这个点就没用了,所以要将这个点出队,对下个点扩展。

理解了这点后代码就很容易实现了^_^。

完整代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x, y;
    int s;//步数
    int f;//父亲再队列中的编号
};

int main()
{
    queue<node> que;
    int a[51][51] = { 0 };
    int book[51][51] = { 0 };
    int next[4][2] =
    {
        {0,1}, //向右走
        {1,0},//向下走
        {0,-1},//向左走
        {-1,0}//向上走
    };
    int n, m, i, j, k, flag,p,q;
    node start, tnode;
    //输入迷宫的行列 起始点
    cin >> n >> m >> start.x >> start.y;
    //输入迷宫图
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
            cin >> a[i][j];
    cin >> p >> q;//输入终点
    book[start.x][start.y] = 1;//将起始点标记为走过
    //如果队列不为空
    while (!que.empty())
    {
        //枚举四个方向
        for (int k = 0; k <= 3; k++)
        {
            //计算下一步坐标
            tnode.x = que.front().x + next[k][0];
            tnode.y = que.front().y + next[k][1];
            //判断是否出界
            if (tnode.x<1 || tnode.x>n || tnode.y<1 || tnode.y>m)
                continue;
            //判断是否是陆地或已经走过
            if (mapp[tnode.x][tnode.y] > 0 && book[tnode.x][tnode.y] == 0)
            {
                sum++;
                //每个点只入队一次,标记走过
                book[tnode.x][tnode.y] = 1;
                int ts = que.back().s;//记录父亲的步数
                //将该点入队
                que.push(tnode);
                que.back().s = ts+1; //步数是父亲步数加1
            }
            if (tnode.x == p && tnode.y == q)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            break;
        que.pop();//队首出队
    }

    cout << que.back().s << endl;
    system("pause");
    return 0;
}

练习

学完DFS和BFS后来练习一下吧。

题目:宝岛探索

小哼通过一种秘密方法得到一张不完整的钓鱼岛航拍图。小哼决定去钓鱼岛探险。

下面的10*10的矩阵就是该航拍图。图中数字表示海拔,0表示海洋,1-9都表示陆地。

小哼的飞机会降落再(6,8),现在需要计算小哼降落岛的面积。此处我们把降落点上

下左右相邻岛视为同一岛屿。

输入

输出

BFS代码如下:

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

struct node
{
    int x;
    int y;
};
int main()
{
    queue<node> que;
    int mapp[51][51];
    int book[51][51];
    int i, j, n, m,sum;
    node start, tnode;
    memset(book, 0, sizeof(book));
    memset(mapp, 0, sizeof(mapp));
    //输入地图的行列,初始坐标
    cin >> n >> m >> start.x >> start.y;
    //输入地图
    for(i=1;i<=n;i++)
        for (j = 1; j <= m; j++)
        {
            cin >> mapp[i][j];
        }
    int next[4][2]=
    {
        {0,1}, //向右走
        {1,0},//向下走
        {0,-1},//向左走
        {-1,0}//向上走
    };
    //向队列插入降落的起始坐标
    que.push(start);
    sum = 1;
    book[start.x][start.y] = 1;
    //当队列不为空时循环
    while (!que.empty())
    {
        //枚举四个方向
        for (int k = 0; k <= 3; k++)
        {
            //计算下一步坐标
            tnode.x = que.front().x + next[k][0];
            tnode.y= que.front().y + next[k][1];
            //判断是否出界
            if (tnode.x<1 || tnode.x>n || tnode.y<1 || tnode.y>m)
                continue;
            //判断是否是陆地或已经走过
            if (mapp[tnode.x][tnode.y] > 0 && book[tnode.x][tnode.y] == 0)
            {
                sum++;
                //每个点只入队一次,标记走过
                book[tnode.x][tnode.y] = 1;
                //将该点入队
                que.push(tnode);
            }
            
        }
        //队首出队,下个点继续搜素
        que.pop();
    }
    cout << sum << endl;
    getchar();
    getchar();
    return 0;
}

DFS代码如下:

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

struct node
{
    int x;
    int y;
};
int mapp[51][51];
int book[51][51];
int n, m,sum;
void dfs(int x,int y)
{
    int next[4][2] =
    {
        {0,1}, //向右走
        {1,0},//向下走
        {0,-1},//向左走
        {-1,0}//向上走
    };
    int tx, ty, k;
    //枚举四个方向
    for (k = 0; k <= 3; k++)
    {
        tx = x + next[k][0];
        ty = y + next[k][1];
        //判断是否出界
        if (tx<1 || tx>n || ty<1|| ty>m)
            continue;
        //判断是否是陆地或已经走过
        if (mapp[tx][ty] > 0 && book[tx][ty] == 0)
        {
            sum++;
            //标记走过
            book[tx][ty] = 1;
            dfs(tx, ty); //执行下一步
        }
    }
    return;
}
int main()
{
    
    int i, j,sx,sy;
    memset(book, 0, sizeof(book));
    memset(mapp, 0, sizeof(mapp));
    cin >> n >> m >> sx >> sy;
    //输入地图
    for(i=1;i<=n;i++)
        for (j = 1; j <= m; j++)
        {
            cin >> mapp[i][j];
        }
    book[sx][sy] = 1;
    sum = 1;
    dfs(sx, sy);
    cout << sum << endl;
    getchar();
    getchar();
    return 0;
}

拓展:如何求有多少个独立岛屿呢?

即求独立子图的个数,这就是Floodfill漫水填充法,又叫种子填充法。Windows下“画图”中的油漆桶工具就是基于这个算法的。
我们将独立岛屿进行染色,所以增加参数color。
代码如下

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

struct node
{
    int x;
    int y;
};
int mapp[51][51];
int book[51][51];
int n, m,sum;
void dfs(int x,int y,int color)
{
    mapp[x][y] = color;//表示小哼来过这个岛
    int next[4][2] =
    {
        {0,1}, //向右走
        {1,0},//向下走
        {0,-1},//向左走
        {-1,0}//向上走
    };
    int tx, ty, k;
    //枚举四个方向
    for (k = 0; k <= 3; k++)
    {
        tx = x + next[k][0];
        ty = y + next[k][1];
        //判断是否出界
        if (tx<1 || tx>n || ty<1|| ty>m)
            continue;
        //判断是否是陆地或已经走过
        if (mapp[tx][ty] > 0 && book[tx][ty] == 0)
        {
            sum++;
            //标记走过
            book[tx][ty] = 1;
            dfs(tx, ty,color); //执行下一步
        }
    }
    return;
}
int main()
{
    
    int i, j,sx,sy,color=0;
    memset(book, 0, sizeof(book));
    memset(mapp, 0, sizeof(mapp));
    cin >> n >> m ;
    //输入地图
    for(i=1;i<=n;i++)
        for (j = 1; j <= m; j++)
        {
            cin >> mapp[i][j];
        }
    //对每个大于0(未被染色)的点尝试dfs染色
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if (mapp[i][j] > 0)//如果没被染色
            {
                color--;//color减一表示一种新的颜色
                book[i][j] = 1; //标记走过
                dfs(i, j, color);
            }
        }
        
    }
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            cout<<mapp[i][j]<<" ";
        }
        cout << endl;
    }
    cout <<"共有独立岛屿:"<< -color<<" 个" << endl;
    getchar();
    getchar();
    return 0;
}

Guess you like

Origin www.cnblogs.com/muyefeiwu/p/11315482.html