Depth-first search and topics

1. Depth-first search

1. Searching refers to an algorithm that traverses the intermediate states of the problem by knowing the initial state and the target state. In layman's terms, searching is a more complicated enumeration.
2. Depth-first search is to search in a depth manner, that is, enumerate all feasible solutions, and keep trying until you find the solution to the problem.
Imagine that you are in a maze. When we are at the starting point, we always have to pass through various forks to find the end of the maze. Then from the starting point, go forward along a road. When you encounter a fork, choose one of the forks to move forward. . If the selected fork is ahead of the fork, then return to the fork and choose another fork. If there is a new fork in the fork, then execute the above method again, so that we can find the end point.

In other words, when you meet a fork in the road, you always use "depth" as the key to progress, and don't look back without touching a dead end .
Well, this method of returning to the last fork after encountering a dead end is called the backtracking method .

2. Depth-first search-backtracking method.

1. The backtracking method refers to starting from a certain angle of the problem, searching for all possible situations, and then using one of them as a new starting point to continue to explore downward, so that a "road" is taken out. When you reach the end, return to the last starting point, and start searching from another situation. And through continuous "backtracking" to find the goal.

Example:

The complete arrangement of numbers: That is, the complete arrangement of 123 is 123,132,213,231,312,321. That is, three numbers of 1, 2, and 3 are used to form a 3-digit number, and no repeated numbers are allowed.

Insert picture description here
As shown in the figure, we artificially stipulate that the selection method of 1, 2, and 3 is to select 1 first, then select 2, and finally select 3. Then if we want to perform a full arrangement, it is as shown.
We stipulate that the first number is the first box ... and so on, the code to put the number in the box:

void  dfs(int step)   
{
  for(i=1;i<=n;i++)   //一共有n个数字,在这里,n=3
  {
    if(book[i]==0)    //等于0表示这个数字还未被放入盒子中
    {
      a[step]=i;      //将数字i放在第step个盒子
      book[i]=1;
      dfs(step+1);      //放下一个数字
      book[i]=0;
     }
  }
  return;
}

So what is the end criterion, that is, when we process the n + 1th small box, it means that the previous n boxes have been put away, then we will output this situation. which is:

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

The complete code is as follows:

#include<stdio.h>
int a[10],book[10],n;

void dfs(int step)
{
     int i;
     if(step==n+1)
     {
     	for(i=1;i<=n;i++)  //将这种情况打印出来
     	    printf("%d ",a[i]);
     	printf("\n");
     	return;
     }

   for(i=1,i<=n;i++)
   {
      if(book[i]==0)
      {
         a[step]=i;
         book[i]=1;
         dfs(step+1);
         book[i]=0;
      }
   }
   return;
}

int main()
{
   scanf("%d",&n);
   dfs(1);
   return 0;
  }

3. Deep search example, go through the maze.

That is, we use a two-dimensional array to store the maze, the size of the maze is n * m, the coordinates of the end point are (p, q), to find the shortest path from the start point to the end point, we specify the order to go right, down, Left, up. In the two-dimensional array storing the maze, 0 represents the path that can be taken and 1 represents the obstacle.
code show as below:

#include<stdio.h>
int n,m,p,q,min=1000000;
int a[20][20],book[20][20];
void dfs(int x,int y,int step)        //x,y为当前点的横纵坐标,step表示已经走过的步数。
{
  int next[4][2]={{0,1),{1,0},{0,-1},{-1,0}};          //分别为向右走,向下,向左,向上
  int tx,ty,k;         //tx,ty为下一个点的横纵坐标
  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]==0&&book[tx][ty]==0)
     {
        book[tx][ty]=1;
        dfs(tx,ty,step+1);
        book[tx][ty]=0;
     }
  }
  return;
  }

int main()
{
 int i,j,x,y;
 scanf("%d %d",&n,&m);   
 for(i=1;i<=n;i++)
   for(j=1;j<=m;j++)
     scanf("%d",&a[i][j]);       //读入迷宫
 scanf("%d %d %d %d",&x,&y,&p,&q);   //读入起点和终点
 book[x][y]=1;    //标记起点已经走过了,防止重复走
 dfs(x,y,0);
 printf("%d ",min);
 return 0;
}
Published 10 original articles · Likes2 · Visits 217

Guess you like

Origin blog.csdn.net/dfwef24t5/article/details/104140246