UESTC 853 一个简单的走迷宫问题

来源:互联网 发布:江苏破获网络诈骗团伙 编辑:程序博客网 时间:2024/05/17 00:10

这是一个网格迷宫问题,地图的左上角为00,坐标i,j表示ij列。有一个人,他在一个迷宫里,他要去一个目标位置。他站在网格的交叉点上,障碍物在网格内部。他可以用不同的速度前进,每秒可以走1格到3格。也可以花费一秒的时间向左或向右转。同时他不能撞到障碍物,也不能半个身体在地图外面。现在,他最短要多少秒才能走到目标位置。人的体积是一个半径为0.5格的圆。

Input

题目有多组测试数据。每组包含以下内容:

第一行包含两个整数n,m(0n,m50)。表示地图的行数和列数。n,m0 0时表示结束。

接下来n行,每行包含m01的整数。1表示障碍物,0表示没有障碍物。

接下来一行包含5个整数,x1,y1,x2,y2,w。分别表示初始位置和目标位置,以及初始方向。0123分别表示上右下左。

题目保证输入合法。

Output

输出仅包含一个整数,如果无解输出-1,否则输出最短的时间(单位:秒)。

Sample input and output

Sample InputSample Output
9 100 0 0 0 0 0 1 0 0 00 0 0 0 0 0 0 0 1 00 0 0 1 0 0 0 0 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 00 0 0 0 0 1 0 0 0 00 0 0 1 1 0 0 0 0 00 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 1 07 2 2 7 29 100 0 0 0 0 0 1 0 0 00 0 0 0 0 0 0 0 1 00 0 0 1 0 0 0 0 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 00 0 0 0 0 1 0 0 0 00 0 0 1 1 0 0 0 0 00 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 1 07 2 2 7 20 0
1212

Hint

样例如图所示:(蓝色点为出发点,红色点为目标点,红线为路径)

title

状态:

1.向左转 

2.向右转

3.向此时的方向走1步

4.向此时的方向走2步

5.向此时的方向走3步

bfs

#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;int n,m;int map[55][55];bool vis[4][55][55];struct node{    int x,y;    int dr;   //方向    int step;    bool operator<(const node &t)const    {        return t.step<step;    }}S,E;bool judge(int x,int y){    if(x>=0 && x<n-1 && y>=0 && y<m-1)    {        return true;    }    return false;}int bfs(){    priority_queue<node>q;    q.push(S);    node now,next,temp;    memset(vis, false, sizeof(vis));    vis[S.dr][S.x][S.y]=true;    while (!q.empty())    {       next=now=temp=q.top();        q.pop();        if(now.x==E.x && now.y==E.y)        {            return now.step;        }        for (int i=-1; i<4; i++)        {            if(i<=0)            {                now.step=temp.step+1;                if(i==-1)                {                    now.dr=(1+temp.dr)%4;  //右转                }                if(i==0)                {                    now.dr=(3+temp.dr)%4;  //左转                }                if(!vis[now.dr][now.x][now.y])                {                    vis[now.dr][now.x][now.y]=true;                    q.push(now);                }            }            else            {                next.step=temp.step+1;                if(next.dr==0)  //向上走                {                    next.x=temp.x-i;                }                if(next.dr==1)  //向右走                {                    next.y=temp.y+i;                }                if(next.dr==2) //向下走                {                    next.x=temp.x+i;                }                if(next.dr==3) //向左走                {                    next.y=temp.y-i;                }                //走i步过程中有一步走不通就不得行了                if(!judge(next.x, next.y) || map[next.x][next.y]==1)                {                    break;                }                if(!vis[next.dr][next.x][next.y])                {                    q.push(next);                    vis[next.dr][next.x][next.y]=true;                }            }        }    }    return -1;}int main(){    while (scanf("%d%d",&n,&m) && (n||m))    {        for (int i=0; i<n; i++)        {            for (int j=0; j<m; j++)            {                scanf("%d",&map[i][j]);                                //把n*m的矩阵化成(n-1)*(m-1)的网格线矩阵。                if(map[i][j]==1)                {                    map[i][j]=1;                    if(i>=1)                    {                        map[i-1][j]=1;                        if(j>=1)                        {                            map[i-1][j-1]=1;                        }                    }                    if(j>=1)                    {                        map[i][j-1]=1;                        if(i>=1)                        {                            map[i-1][j-1]=1;                        }                    }                }            }        }        scanf("%d%d%d%d%d",&S.x,&S.y,&E.x,&E.y,&S.dr);        S.x--,S.y--,E.x--,E.y--;        S.step=0;        printf("%d\n",bfs());    }    return 0;}




0 0
原创粉丝点击