Maze (breadth-first algorithm)

Subject description:

Description

N * M has a labyrinth grid, a grid is representative of the wall, can not be, representative of 0 by addition, in the maze
some portal, i.e., go to the entrance portal will be automatically transferred to the outlet portal (primary transfer count step 1). People in the maze can try to
move up and down four directions. Given a maze and now all portal entrances, as well as start and end points,
ask how many steps can at least out of the maze. If it is not out of the maze output "die".

Input Format

The program for the multi-CASE, the number of the first CASE behavior of
each of CASE, the behavior of the first two numbers N (row) and M (columns)
and the number of N lines of M
followed by a number W, the number of transfer gate
after each line a portal of entry coordinates C1 (line), r1 (columns) and an outlet coordinates c2, r2
followed starting point and end point SC (OK) SR (column) EC (OK) ER (column)

Note: The portal entrance and the start point and end point it does not appear in the location of the wall
all the numbers does not exceed 100

Output formats
such as title

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
, the

Do title ideas:

Create multiple two-dimensional array, respectively hold the map, entrances and exits, entrances and exits portal, and you need to set a flag variable to determine whether to reach the end

Can take advantage of the breadth-first traversal simulation Maze state, it would need a structure to save the coordinates of the current position in the map, a few steps away from the starting point of the walk.

In the Maze procedure, a lattice of "1", not its traverse; portal of entry for the coordinates of the grid, the current state of the transfer gate needs to be adjusted to an outlet position coordinates, and step number plus 1; for as the end point of the grid, is set to reach the end of the flag variable, and ends the case

Need points to note:

  1. Multi-case
  2. Walked on to the grid is set to "1"
  3. Start and end points coincide case

To understand the breadth-first traversal algorithm

Maze with a breadth-first traversal, introduced with examples, can be understood as the water ripples, like slid around, but is unlikely to reflect the relationship between nodes and node, so on that basis, I would like to compare it to the spider network, when an object strikes spider web, the position (the initial node) of the impact object, the impact force in the spider has the initial node is connected is transmitted to the next node, and repeat this step, and will not back to. That is when traversing the breadth, around and not be associated with the node point first traversed to traverse, and then repeat this step until all nodes have been traversed. Since a node associated with a plurality of nodes and can not be traversed, we can simulate this queue "simultaneous" traversal.

Specific examples:

  1. Traverse to the starting point, to be found in four directions around the traverse

  2. The state of the queue is pressed into four directions

  3. Traversal start

  4. The starting point of the iteration is complete, the first point of a point on a traversal direction of the start point, and its associated queue is pressed, the set of dots arranged in a first direction associated is T1, the second at T2, and so on .

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

  5. Step 4 cycles until the queue status:

    Starting a fourth direction, T1, T2, T3

  6. When the starting point of the fourth direction and ejected from the queue is traversed in this case, queue to complete the "simultaneous" traversal starting simulation of four directions.

The code (including 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}}; //四个方向
char s[110][110];       //地图大小
int sr,sc;      //初始地点
int er,ec;      //目标地点
int In[110][2],Out[110][2]; //传送门入口和出口坐标

typedef struct{
int row;
int col;        //位置
int step;       //步数
}node;      //每次走的记录


int main()
{
    int k;
    scanf("%d",&k); //多case
    while(k--)
    {
        int m,n,flag=0,jump=0;
        scanf("%d%d",&m,&n);        //行列数
        int i;
        for(i=0;i<m;i++)
        {
            scanf("%s",s[i]);       //每行迷宫
        }
        int num;
        scanf("%d",&num);       //传送门数量
        for(i=0;i<num;i++)
        {
            scanf("%d%d%d%d",&In[i][0],&In[i][1],&Out[i][0],&Out[i][1]);        //传送门入口坐标和出口坐标
        }
        scanf("%d%d%d%d",&sr,&sc,&er,&ec);      //起点和终点坐标

         node first;
         first.row=sr;
         first.col=sc;
         first.step=0;      //设置起点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)   //是否是终点
             {
                 printf("%d\n",cur.step);
                 flag=1;        //已到达终点标志
                 break;
             }

             for(i=0;i<num;i++)     //检测是不是传送门位置
             {
                 if(cur.row==In[i][0]&&cur.col==In[i][1])
                 {
                     node newnode;
                     newnode.row=Out[i][0];
                     newnode.col=Out[i][1];         //传送后的坐标
                     newnode.step=cur.step+1;       //步数+1
                     Q.push(newnode);       //推入该坐标
                     jump=1;        //传送门使用标志
                     break;

                 }
             }
             if(!jump)      //未使用传送门
             {

                 for(i=0;i<4;i++)       //四个方向
                 {
                     int k=0;
                     node now;
                     now.row=cur.row+d[i][0];
                     now.col=cur.col+d[i][1];
                     now.step=cur.step+1;       //步数+1
                     if(now.row<0||now.row>=m||now.col<0||now.col>=n)  continue; //边界检测
                     if(s[now.row][now.col]=='0')
                     {
                         s[now.row][now.col]='1';   //走过的标志位
                         Q.push(now);
                     }
                 }
             }

         }//while
            if(!flag)  //flag未设置成终点标志,die
            {
                printf("die\n");
            }
    }//while
}

Guess you like

Origin www.cnblogs.com/Dozeer/p/10961769.html