bfs+优先队列+诡异的梯子 hdu1277

                                             诡异的楼梯

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

这个楼梯确实很诡异,首先看一组数据

..S

..-

..T

这组样例对的答案是2,就是说当你走的方向与楼梯的方向对不上时,这时实际上要停下来等下一秒,这才是到达对面最快的路径;所以vis标记就是先到哪就标记,后到的一定走重或者是慢的走法。

走的顺序是,先到一个点,判断是不是梯子,是梯子沿这个方向再向前走一步,是点直接常规操作。

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<cmath>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=2e6+5;
const int MOD=10007;
bool vis[105][105];
struct node
{
    int x,y;
    int st;
    bool operator < (node b) const
    {
        return st>b.st;
    }
};
int dx[5]={0,0,-1,1};
int dy[5]={1,-1,0,0};
char a[105][105];
int n,m;
int sx,sy,tx,ty;
inline bool check(int x,int y)
{
    if(vis[x][y]==true) return false;
    if(a[x][y]=='*') return false;
    if(x<0||x>=n||y<0||y>=m) return false;
    return true;
}
void bfs()
{
    memset(vis,false,sizeof vis);
    node t;
    t.x=sx;
    t.y=sy;
    t.st=0;
    vis[sx][sy]=true;
    priority_queue<node> q;
    q.push(t);
    while(!q.empty())
    {

        node f=q.top();
        //cout<<f.x<<' '<<f.y<<' '<<f.st<<endl;
        q.pop();
        if(f.x==tx&&f.y==ty)
        {
            printf("%d\n",f.st);
            return ;
        }
        for(int i=0;i<4;++i)
        {
            int xx=f.x+dx[i];
            int yy=f.y+dy[i];
            if(check(xx,yy))//没出界//没来过//不是障碍
            {
                if(a[xx][yy]=='.'||a[xx][yy]=='T')
                {
                    node temp;
                    temp.x=xx;
                    temp.y=yy;
                    temp.st=f.st+1;
                    vis[xx][yy]=true;
                    q.push(temp);
                    continue;
                }
                if(i<2)//横着走
                {
                    if(a[xx][yy]=='|'&&(f.st&1)||(a[xx][yy]=='-'&&f.st%2==0))//横梯子
                    {
                        int xxx=xx+dx[i];
                        int yyy=yy+dy[i];
                        if(check(xxx,yyy))
                        {
                            node temp;
                            temp.st=f.st+1;
                            temp.x=xxx;
                            temp.y=yyy;
                            vis[xxx][yyy]=true;
                            q.push(temp);
                        }
                    }
                    if(a[xx][yy]=='|'&&f.st%2==0||(a[xx][yy]=='-'&&(f.st&1)))//竖梯子//等两秒过去
                    {
                        int xxx=xx+dx[i];
                        int yyy=yy+dy[i];
                        if(check(xxx,yyy))
                        {
                            node temp;
                            temp.st=f.st+2;////////
                            temp.x=xxx;
                            temp.y=yyy;
                            //cout<<xxx<<' '<<yyy<<endl;
                            vis[xxx][yyy]=true;
                            q.push(temp);
                        }
                    }
                }
                else if(i>1)//竖着走
                {
                    if(a[xx][yy]=='|'&&f.st%2==0||(a[xx][yy]=='-'&&(f.st&1)))//竖梯子
                    {
                        int xxx=xx+dx[i];
                        int yyy=yy+dy[i];
                        if(check(xxx,yyy))
                        {
                            node temp;
                            temp.st=f.st+1;
                            temp.x=xxx;
                            temp.y=yyy;
                            vis[xxx][yyy]=true;
                            q.push(temp);
                        }
                    }
                    else if(a[xx][yy]=='|'&&(f.st&1)||(a[xx][yy]=='-'&&f.st%2==0))//横梯子
                    {
                        int xxx=xx+dx[i];
                        int yyy=yy+dy[i];
                        if(check(xxx,yyy))
                        {
                            node temp;
                            temp.st=f.st+2;////////
                            temp.x=xxx;
                            temp.y=yyy;
                            vis[xxx][yyy]=true;
                            q.push(temp);
                        }
                    }
                }

            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;++i)
            scanf("%s",a[i]);
        for(int i=0;i<n;++i)
            for(int j=0;j<m;++j)
        {
            if(a[i][j]=='S')
            {
                sx=i;
                sy=j;
            }
            else if(a[i][j]=='T')
            {
                tx=i;
                ty=j;
            }
        }
        bfs();
    }
}

猜你喜欢

转载自blog.csdn.net/codetypeman/article/details/81382688
今日推荐