诡异的楼梯 (优化剪枝)

来源:互联网 发布:橱柜效果图制作软件 编辑:程序博客网 时间:2024/06/09 16:32

诡异的楼梯

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 8589    Accepted Submission(s): 2105


Problem Description
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
 

Input
测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
 

Output
只有一行,包含一个数T,表示到达目标的最短时间. 
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
 

Sample Input
5 5**..T**.*...|...*.*.S....
 

Sample Output
7
Hint
Hint
地图如下:
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;char a[30][30];bool vis[30][30];struct node{    int x,y,d;};int xx[4]={0,1,0,-1};int yy[4]={1,0,-1,0};int n,m;bool judge(int x,int y){    if(x<0||x>=n||y<0||y>=m)        return 0;    if(a[x][y]=='*')        return 0;    if(vis[x][y])        return 0;    return 1;}int bfs(int sx,int sy){    memset(vis,0,sizeof(vis));    queue<node> q;    node temp;    temp.x=sx;    temp.y=sy;                 temp.d=0;    vis[sx][sy]=1;    q.push(temp);    while(!q.empty())    {        node cur=q.front();        q.pop();        if(a[cur.x][cur.y]=='T')            return cur.d;        int i;        for(i=0;i<4;i++)        {            int nx=cur.x+xx[i];            int ny=cur.y+yy[i];            if(nx<0||nx>=n||ny<0||ny>=m)                continue;            if(vis[nx][ny]||a[nx][ny]=='*')                continue;            else if(a[nx][ny]=='.'||a[nx][ny]=='T')                      {                node next;                next.x=nx;                next.y=ny;                next.d=cur.d+1;                vis[nx][ny]=1;                q.push(next);            }            else            {                if(i==0||i==2)                {                    if(a[nx][ny]=='-'&&cur.d%2==0||a[nx][ny]=='|'&&cur.d%2==1)                    {                        nx=nx+xx[i];                        ny=ny+yy[i];                        if(judge(nx,ny))                        {                            node next;                            next.x=nx;                            next.y=ny;                            next.d=cur.d+1;                            vis[nx][ny]=1;                            q.push(next);                        }                    }                    else  //等下一分钟                    {                        node next;                        next.x=cur.x;                        next.y=cur.y;                        next.d=cur.d+1;                        q.push(next);                    }                }                else                {                    if(a[nx][ny]=='-'&&cur.d%2==1||a[nx][ny]=='|'&&cur.d%2==0)                    {                        nx=nx+xx[i];                        ny=ny+yy[i];                        if(judge(nx,ny))                        {                            node next;                            next.x=nx;                            next.y=ny;                            next.d=cur.d+1;                            vis[nx][ny]=1;                            q.push(next);                        }                    }                    else                    {                        node next;                        next.x=cur.x;                        next.y=cur.y;                        next.d=cur.d+1;                        q.push(next);                    }                }            }        }    }    return -1;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        int i;        int sx,sy;        for(i=0;i<n;i++)        {            scanf("%s",a[i]);            int j;            for(j=0;j<m;j++)            {                if(a[i][j]=='S')                    {sx=i;sy=j;}            }        }        int res=bfs(sx,sy);        printf("%d\n",res);    }    return 0;}


 
0 0
原创粉丝点击