Maze walking (breadth-first algorithm)

Posted by jaypotter on Sat, 01 Jun 2019 22:42:34 +0200

Title Description:

Description

There is a N*M grid labyrinth, 1 represents that the grid is a wall, can not pass, 0 represents that can pass, in addition, in the labyrinth.
There are some ports, which will be automatically transmitted to the exit of the portal when it comes to the entrance of the portal (one step at a time). Man can try in a maze.
Move up and down in four directions. Now given a labyrinth and access to all portals, as well as starting and ending points,
Ask at least how many steps you can take to get out of the maze. If you can't get out of the maze and output "die".

Input format

The program is multiple CASE, the number of CASE of the first act
For each CASE, the first act has two numbers N (rows) and M (columns)
Then the number of M rows per line in N rows
Then there is a number W, for the number of ports.
Then one portal entry coordinate C1 (row), R1 (column) and exit coordinate c2,r2 for each row.
Then the starting and ending coordinates SC (row) SR (column) EC (row) Er (column)

Note: The entrance, exit and starting and ending coordinates of the portal will not appear on the wall.
All figures do not exceed 100

Output format
For example

sample input

2
4 3
011
011
110
110
1
1 0 2 2
0 0 3 2
2 2
01
10
0
0 0 1 1

sample output

3
die

Ways of thinking on the topic:

Establish multiple two-dimensional arrays to store maps, entrances and exits, and portal entrances and exits respectively, and need to set up a marker variable to determine whether to reach the destination.

Using breadth-first traversal to simulate the state of maze walking, it is necessary to use a structure to save the coordinates of the current position in the map and the number of steps taken from the starting point.

In the process of walking a maze, the lattice with "1" is not traversed; for the lattice with portal entrance coordinates, the current state needs to be adjusted to the coordinate of portal exit position and the number of steps is added to 1; for the lattice with terminal point, the marker variable to reach the terminal point is set, and the case is terminated.

Points for attention:

  1. For many case
  2. Set the grid to "1"
  3. The coincidence of starting point and ending point

Understanding of breadth-first traversal algorithm

The breadth-first traversal of maze walking can be understood as the ripple of water, like the surrounding swing, but it can not reflect the relationship between nodes. So on this basis, I would like to compare it to a spider web. When an object strikes a spider web, the impact force is connected with the initial node. Spider silk passes to the next node and repeats this step without returning. That is to say, in the breadth traversal, the associated and non-traversing points around the nodes are traversed first, and then repeat this step until all nodes are traversed. Since there are many nodes associated with a node that cannot traverse at the same time, we can use queues to simulate this "simultaneous" traversal.

Specific examples:

  1. Traverse the starting point and find four directions around it.

  2. Push four directions into the queue

  3. Ergodic origin

  4. At the end of the traversal of the starting point, traverse the points in the first direction of the starting point, and press the points associated with them into a queue. Let the set of points associated with the first direction be T1, the second is T2, and so on.

    Current queue: the second direction of starting point, the third direction of starting point, the fourth direction of starting point, T1

  5. Cycle 4 steps until the queue state is:

    Fourth Direction of Starting Point, T1, T2, T3

  6. When the fourth direction of the starting point is traversed and ejected from the queue, the queue completes the simulation of "simultaneous" traversal of the four directions of the starting point.

Code (with comments)

#include <iostream>
#include <cstdio>
#include <malloc.h>
#include <queue>
using namespace std;
int d[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; //Four directions
char s[110][110];       //Map size
int sr,sc;      //Initial location
int er,ec;      //Target location
int In[110][2],Out[110][2]; //Portal entrance and exit coordinates

typedef struct{
int row;
int col;        //position
int step;       //Step number
}node;      //Records of each trip


int main()
{
    int k;
    scanf("%d",&k); //Multi case
    while(k--)
    {
        int m,n,flag=0,jump=0;
        scanf("%d%d",&m,&n);        //Number of ranks
        int i;
        for(i=0;i<m;i++)
        {
            scanf("%s",s[i]);       //Each row of labyrinth
        }
        int num;
        scanf("%d",&num);       //Number of ports
        for(i=0;i<num;i++)
        {
            scanf("%d%d%d%d",&In[i][0],&In[i][1],&Out[i][0],&Out[i][1]);        //Portal entry and exit coordinates
        }
        scanf("%d%d%d%d",&sr,&sc,&er,&ec);      //Starting and ending coordinates

         node first;
         first.row=sr;
         first.col=sc;
         first.step=0;      //Setting the starting point node

         queue <node> Q;
         Q.push(first);

         while(!Q.empty())
         {
             node cur;
             cur=Q.front();
             Q.pop();
             jump=0;

             if(cur.row==er&&cur.col==ec)   //Is it the end?
             {
                 printf("%d\n",cur.step);
                 flag=1;        //Endpoint sign arrived
                 break;
             }

             for(i=0;i<num;i++)     //Is the location of the portal detected?
             {
                 if(cur.row==In[i][0]&&cur.col==In[i][1])
                 {
                     node newnode;
                     newnode.row=Out[i][0];
                     newnode.col=Out[i][1];         //Transferred coordinates
                     newnode.step=cur.step+1;       //Step +1
                     Q.push(newnode);       //Push in this coordinate
                     jump=1;        //Port Use Logo
                     break;

                 }
             }
             if(!jump)      //Unused portal
             {

                 for(i=0;i<4;i++)       //Four directions
                 {
                     int k=0;
                     node now;
                     now.row=cur.row+d[i][0];
                     now.col=cur.col+d[i][1];
                     now.step=cur.step+1;       //Step +1
                     if(now.row<0||now.row>=m||now.col<0||now.col>=n)  continue; //boundary detection
                     if(s[now.row][now.col]=='0')
                     {
                         s[now.row][now.col]='1';   //Passing Sign Bit
                         Q.push(now);
                     }
                 }
             }

         }//while
            if(!flag)  //flag is not set to end mark, die
            {
                printf("die\n");
            }
    }//while
}

Topics: PHP