8.1 Problem F: [Introduction to Recursion] Walk the maze

Title description


  There is a maze of n*m grids (indicating that there are n rows and m columns), and there are some that can be walked and some are not. If you use 1 to indicate that you can walk, and 0 to indicate that you cannot walk, the file reads these n*m ​​data And the start point and end point (both the start point and the end point are described by two data, which respectively represent the row number and column number of this point). Now you are required to program to find all feasible roads. There are no duplicate points in the road you are walking. You can only go in four directions, up, down, left, and right. If no way is feasible, output the corresponding information (use -l to indicate no way).
  Please expand in the order of upper left and lower right, which is (0,-1),(-1,0),(0,1),(1,0)
 

 

 

enter

The first row is two numbers n, m (1 <n, m <15 ), the next is m rows and n columns consisting of 1 and 0 data, and the last two rows are the starting point and the ending point. 

Output

  For all feasible paths, use the form of (x, y) when describing a point. Except for the starting point, all others must use "->" to indicate the direction. 
  If there is no feasible way, output -1.

Sample input Copy

5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6

Sample output Copy

(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)

prompt

[Algorithm analysis] 
  An array a is used to store the conditions of the maze, and an array b is used to store which points have been walked. Each point is described by two numbers, one for the row number and the other for the column number. For a certain point (x, y), the four possible directions of points are described in the following table: 
   2 
1 x, y 3 
   4 
  corresponds to the position: (x, y-1), (x-1, y), (x, y+1), (x+1, y). Therefore, each point must be tested in four directions. If you have not walked (the value of the corresponding point in the array b is 0) and you can walk (the value of the corresponding point in the array a is 1) and does not cross the boundary, just walk over and see if there is If the end point is not reached, output the path taken when the end point is reached, otherwise continue walking. 
  This search process is described by search as follows: 
procedure search(x, y, b, p); {x, y represents a certain point, b is the situation of a point that has been passed, and p is the road that has been traveled} 
 begin 
   for i :=1 to 4 do{test 4 points separately} 
   begin 
     first remember the position of the current point, the situation and the way traveled; 
     if the i-th point (xl, y1) can go, then go In the past; 
     if it has reached the end, output the path taken and the information that there is a way to go, 
     otherwise continue to search from the new point down search(xl, y1, b1, p1); 
   end; 
 end; 
  In some cases, it is obvious that there is no solution. For example, there is a row or column in the rectangle from the start point to the end point that is 0, and the road is obviously blocked. In this case, you must quickly "cut off" the redundant branches to draw a conclusion. This is what the search refers to as "pruning". The layers of nodes starting from the starting point look like branches, and the "dead branches"-obviously useless nodes can be "cut off" first to increase the search speed.  

#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
int a[30][30],m,n,sx,sy,ex,ey;
int vis[30][30]={0};
int dx[4]={0,-1,0,1};///左上右下四个方向
int dy[4]={-1,0,1,0};
bool flag=false;
struct node
{
    int x;
    int y;
};
node s[300];
void dfs(int cx,int cy,int deep)
{
    s[deep].x=cx;
    s[deep].y=cy;
    if(cx==ex&&cy==ey){
        flag=true;
        for(int i=0;i<deep;i++)
            printf("(%d,%d)->",s[i].x,s[i].y);
        printf("(%d,%d)\n",ex,ey);
        return ;
    }
    for(int i=0;i<4;i++)
    {
       if(a[cx+dx[i]][cy+dy[i]]==1&&vis[cx+dx[i]][cy+dy[i]]==0){
            vis[cx+dx[i]][cy+dy[i]]=1;
            dfs(cx+dx[i],cy+dy[i],deep+1);
            vis[cx+dx[i]][cy+dy[i]]=0;
       }
    }
}
int main()
{
    scanf("%d %d",&m,&n);
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&a[i][j]);
    scanf("%d%d",&sx,&sy);
    scanf("%d%d",&ex,&ey);
    vis[sx][sy]=1;
    dfs(sx,sy,0);
    if(flag==false) printf("-1\n");
    return 0;
}

 

Guess you like

Origin blog.csdn.net/wangws_sb/article/details/114931104