诡异的楼梯

              https://blog.csdn.net/laojiu_/article/details/51953256
 

诡异的楼梯

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


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
地图如下:
 

Source
 

Recommend
JGShining
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

const int maxn=50;
const int inf=0x3f3f3f3f;
int nex[][2]={{0,1},{1,0},{0,-1},{-1,0}};
int map[maxn][maxn],book[maxn][maxn];
int n,m,stx,sty;
char c;

struct node{
    int a,b,step;
    friend bool operator < (const node &x,const node &y){
        return x.step>y.step;//定义小的优先级高; //用于定义优先队列 ,step小的优先 
    }
};

bool check(int a,int b){
    if(a>=1&&a<=m&&b>=1&&b<=n&&!book[a][b]&&map[a][b]!='*') return true;//判断该位置是否可以走 
    else return false;
}

int dfs(int a,int b,int step)
{
    priority_queue<node>q;
    node temp,temp1,temp2;
    temp.a=a,temp.b=b,temp.step=step;
    q.push(temp);
    while(!q.empty()){//empty()==1表示为空,empty()==0表示不为空 
        temp1=q.top();//top()是取出栈顶元素,不会删掉栈里边的元素
        q.pop();//pop()是删除栈顶元素。
        for(int i=0;i<4;i++){
            temp2.a=temp1.a+nex[i][0];
            temp2.b=temp1.b+nex[i][1];
            temp2.step=temp1.step+1;
            if(check(temp2.a,temp2.b)&&(map[temp2.a][temp2.b]=='-'||map[temp2.a][temp2.b]=='|')){//判断下一步是否是楼梯 
                if(temp2.step%2==1){//步数为奇数,与初符号不同 ;步数为偶数, 与初符号相同 ;
                    if(map[temp2.a][temp2.b]=='-') c='|'; //c表示当前的符号,map记录的是初值; 
                    else c='-';
                }else{
                    c=map[temp2.a][temp2.b];
                }
                temp2.a=temp2.a+nex[i][0];//如果是则要跳过一步 
                temp2.b=temp2.b+nex[i][1];
                if( (c=='|'&&(i==1||i==3)) || (c=='-'&&(i==0||i==2)) ) temp2.step+=1;//如果楼梯过不去,则要多等一下; 
            }
            if(check(temp2.a,temp2.b)){
                if(map[temp2.a][temp2.b]=='T'){
                    return temp2.step;
                } 
                else{
                    book[temp2.a][temp2.b]=1;
                    q.push(temp2);
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d",&m,&n)){
        getchar();
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                scanf("%c",&map[i][j]);
                if(map[i][j]=='S') stx=i,sty=j;
            }
            getchar();
        }
        memset(book,0,sizeof(book));
        book[stx][sty]=1;
        int ans=dfs(stx,sty,0);
        printf("%d
",ans);
    }
    return 0;
}


 

原文地址:https://www.cnblogs.com/qqshiacm/p/10693378.html