Holiday learn dfs (deep search)

The key depth-first search is that how you're doing , then using a recursive method, the next step in the search, until it reaches the border or other conditions.
Here the introduction of a simple search case:

It requires a number of inputs and outputs of n 1 to full array:

Imagine there are three digits i.e. if (n == 3),

  1. First, for the first position, we can put 1 or 2 or 3. If 1, 2 and 3 and then the remaining hands, and then continue at the second position.

  2. Either put 2, and 3 can be put, then the second position, one can not recapture, so we need an array of tags to record this number with useless, so they need a book array.

  3. Then the first position after done and we need to put the numbers in the second position, and for the first time since this is a reason to put digital, so here uses recursion. But position to a second position, so we called a second time, it is necessary to do a judgment, which used figures, and put it in the book array corresponding to the marked 1, otherwise, only put numbers.

  4. After a recursive completed, it shows for the first time to put all the cards over, then the corresponding license should be revoked, so the back of the book should be called recursively array becomes zero.

  5. Speaking of recursion, then the first thought should be recursive end condition, end condition this question should go to the actual position is greater than that end recursion.

void dfs(int step)
{
    int i;
    if(step==n+1)
    {
        for(i=1;i<=n;i++)
        {
            printf("%d",a[i]);
        }
        printf("\n");

        return ;        //返回之前的一步(最近一次调用dfs函数的地方)

    }
    //在第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;
            //因为有循环操作,所以要将牌收回;
        }
    }
    return ;
}                                                                            

These are part of the code, must pay attention to the end of the recursion will be a return, this function will return back to the place on a recursive call.
From the above, the basic steps are as follows our code dfs

void dfs(int step)
{
	判断边界
	尝试每一种可能 for(i=1;i<=n;i++)
	{
		继续下一步 dfs(step+1)
	}
	返回
}

Build on the progress we continue to look at a rescue problem, when I first saw a lot of grid coordinates to find problems inside a person, when this feeling is wonderful, and today finally have the free time to learn this.
We just have to understand the deep search of the code section, it is the most important thing is to find a recursive border and how to do the moment, and then after each step and the same moment.

Let's take a look at the title

Patricia from the start, over obstacles, to reach the terminal, the minimum number of steps required, take a small step cell count

Here Insert Picture Description

  1. If it wants to go inside the box, then each in one place, he will have up to four direction to go, at least two directions to go.
    So we should try a walk every law, then we need an array of next array direction.

  2. So we can not go back to the turn came, and then on one subject, we need a book array, this time we need to record the coordinates through the site.

  3. Us on the map how to do judgment to be able to continue to take the next step, we need to consider first of all, that there is no point to go out of bounds (ie outside the map), and secondly we need to determine the coordinates of the point is already gone, and whether it is an obstacle.

  4. Next you need to think about the conditions the end, nothing more than the same time point where the coordinates of Patricia and end point, the end of the recursion.

  5. Because every time a recursive, there will be a total number of steps, we need only the minimum value of retained inside the line.

void dfs(int x,int y,int step)
{
    int next[4][2] = {{0,1}//向右走
                     ,{1,0}//向下走
                     ,{0,-1}//向左走
                     ,{-1,0}};//向上走
    int tx,ty,k;
    //判断是否为小哈的位置
    if(x==p && y==q)
    {
        if(step<min)
        {
            //更新步数的最小值
            min=step;
        }
        return;    //一定不能忘记
    }
    //
    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(a[tx][ty]!=1 && book[tx][ty]!=1)
        {
            book[tx][ty]=1;    //尝试该点
            dfs(tx,ty,step+2);
            book[tx][ty]=0;    //尝试结束,收回该点
        }
    }
    return ;
}                                                                

							注意迷宫里面的x和y是这样子定义的

Here Insert Picture Description

Released six original articles · won praise 5 · views 96

Guess you like

Origin blog.csdn.net/weixin_45870050/article/details/104071882