18276 走迷宫(c语言或c++)(用队列的形式)

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

18276 走迷宫

Description

有一个N*M的格子迷宫,1代表该格子为墙,不能通过,0代表可以通过,另外,在迷宫中
有一些传送门,走到传送门的入口即会自动被传送到传送门的出口(一次传送算1步)。人在迷宫中可以尝试
上下左右四个方向移动。现在给定一个迷宫和所有传送门的出入口,以及起点和终点,
问最少多少步可以走出迷宫。如果不能走出迷宫输出“die”。

输入格式

该程序为多CASE,第1行为CASE的数量
每一个CASE,第1行为两个数N(行)和M(列)
然后N行每行M个数
之后是一个数W,为传送门的数量
之后每行一个传送门的入口坐标c1(行),r1(列)和出口坐标c2,r2
之后是起点坐标和终点坐标sc(行) sr(列) ec(行) er(列)
注:传送门出入口和起点坐标和终点坐标不会出现在墙的位置
所有数字不超过100

输出格式

如题

输入样例

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

输出样例

3
die

源码

#include<iostream>
#include<algorithm>
#include<queue>         //队列的头文件
#include<cstdio>        //scanf、printf
using namespace std;
char d[105][105];       //迷宫体,1为墙、0为可通过
int por[105][4];        //传送门,一个传送门由传送起点的行、列和传送终点的行、列
int sr,sc,dr,dc,r,c;    //sr、sc为起点行列,dr、dc为终点行列,r、c为迷宫体的行列大小
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};      //以一个二维数组定义4个方向
typedef struct
{
    int step;
    int row;
    int col;
}LOC;          //定义一个结构体,包含该点所在行、列和已经走的步数
int main()
{
    int n;
    cin>>n;
    while(n--)          //多case
    {
        cin>>r>>c;      
        int flag=0;
        for(int i=0;i<r;i++)
            scanf("%s",d[i]);   //因为是字符型,所以每行可以直接以字符串形式输入
        int portal;             //定义传送门数量
        cin>>portal;
        for(int i=0;i<portal;i++)
            scanf("%d%d%d%d",&por[i][0],&por[i][1],&por[i][2],&por[i][3]);      //输入每个传送门的起点终点
        cin>>sr>>sc>>dr>>dc;                                //输入人的起点和需要达到的终点
        LOC first;first.row=sr;first.col=sc;first.step=0;   //定义一个初始结构体
        queue <LOC>qu;
        qu.push(first);         //入队
        while(!qu.empty())      //队列不空则继续循环
        {
            LOC cur;            
            int jump=0;         //标志是否进入传送门
            cur=qu.front();qu.pop();
            if(cur.row==dr&&cur.col==dc)
            {
                printf("%d\n",cur.step);
                flag=1;         //标志是否到达终点
            }
            for(int i=0;i<portal;i++)       
            {
                if(cur.row==por[i][0]&&cur.col==por[i][1])      //判断现在所在的点是否有传送门
                {
                    LOC newcur;
                    newcur.row=por[i][2];
                    newcur.col=por[i][3];
                    newcur.step=cur.step+1;
                    qu.push(newcur);                        //入队
                    jump=1;                                 //标记,待下次出队时再进行上下左右操作
                    d[por[i][2]][por[i][3]]='1';            //将该位置置'1',走过的不能再走,否则会超时,又题目说传送门终点坐标不会在墙的位置,故不用判断是否为'0'
                }
            }
            if(!jump)
                for(int i=0;i<4;i++)        //对玩家进行上下左右操作
                {
                    int new_r=cur.row+dir[i][0];
                    int new_c=cur.col+dir[i][1];
                    if(new_r>=r||new_r<0) continue;     //走出迷宫范围,无效操作,进行其他方向的操作
                    if(new_c>=c||new_c<0) continue;
                    if(d[new_r][new_c]=='0')            //如果该点不是墙且还未走过该点,则有效
                    {
                        LOC newcur;
                        newcur.row=new_r;newcur.col=new_c;newcur.step=cur.step+1;
                        qu.push(newcur);                //该操作有效,入队
                        d[new_r][new_c]='1';            //置'1'
                    }
                }
        }
        if(!flag)
            cout<<"die"<<endl;
    }
    return 0;
}


/*样例
2 2
00
00
0
0 0 0 0

4 4
0011
1101
1111
0000
2
1 2 3 0
0 1 1 2
0 0 3 3


5 5
00000
01110
01000
01011
00000
7
0 1 0 2
0 2 0 3
0 3 0 4
0 4 1 4
1 4 2 4
2 4 2 3
2 3 2 2
0 0 4 4


0
6
8
*/

猜你喜欢

转载自blog.csdn.net/weixin_43692948/article/details/91995526