HDOJ 1180 诡异的楼梯

题不可一日不水。。。。

可以停留在原地不动。。。用奇偶性判断桥的方向。。。

诡异的楼梯

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


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
地图如下:HDOJ 1180 诡异的楼梯 - qhn999 - 码代码的猿猿
 

Source
 

Recommend
JGShining

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

char m[55][55];
int vis[55][55];
int M,N; int ans=99999999;

struct node
{
    int x,y;
    int time;
}s;

queue<node> q;

int main()
{
while(cin>>M>>N)
{
    while(!q.empty())
    {
        q.pop();
    }

    memset(m,'*',sizeof(m));
    memset(vis,-1,sizeof(vis));

    for(int i=1;i<=M;i++)
    {
        for(int j=1;j<=N;j++)
        {
            cin>>m[j];
            if(m[j]=='S')
            {
                s.x=i;
                s.y=j;
                s.time=1;
                q.push(s);
                vis[j]=0;
            }
            else if(m[j]=='.')
            {
                vis[j]=0;
            }
            else if(m[j]=='-')
            {
                vis[j]=4;
            }
            else if(m[j]=='|')
            {
                vis[j]=5;
            }
            else if(m[j]=='T')
            {
                vis[j]=666;
            }
        }
        getchar();
    }
/*
cout<<"_________________\n";
    for(int i=1;i<=M;i++)
    {
        for(int j=1;j<=N;j++)
            cout<<vis[j]<<"  ";
        cout<<endl;
    }
cout<<"__________________\n";
*/
    ans=999999999;

    node ne;
    while(!q.empty())
    {
/*
        cout<<"("<<q.front().x<<","<<q.front().y<<")"<<"=:"<<q.size()<<endl;
        if(q.front().x==1&&q.front().y==5)
        {
            cout<<"wow!"<<endl;
            cout<<"---+++>>"<<vis[q.front().x][q.front().y]<<endl;
        }
*/
        node s=q.front();
        q.pop();
        int X=s.x,Y=s.y,T=s.time;
        if(vis[X][Y]>=600)
        {
//            cout<<"=-=->"<<T<<endl;
            ans=min(ans,T);
            continue;
        }

        int tx,ty,tt;
        ///stay
        tx=X;ty=Y;tt=T+1;
        if(vis[tx][ty]==0||vis[tx][ty]==1)
        {
           vis[tx][ty]++;
           if(vis[tx][ty]==2) vis[tx][ty]=-1;
           ne.x=tx;ne.y=ty;ne.time=tt;
           q.push(ne);
        }
        ///up
        tx=X-1; ty=Y; tt=T+1;
        if(vis[tx][ty]==0||vis[tx][ty]==1||vis[tx][ty]==666)///'.'
        {
            vis[tx][ty]++;
            if(vis[tx][ty]==2) vis[tx][ty]=-1;
            ne.x=tx; ne.y=ty; ne.time=tt;
            q.push(ne);
        }
        else if(vis[tx][ty]==4||vis[tx][ty]==5)
        {
            if(((vis[tx][ty]==4)&&(~T&1))||((vis[tx][ty]==5)&&(T&1)))
               {
                   ne.x=tx-1;ne.y=ty;ne.time=tt;
                   q.push(ne);
               }
        }

        ///down
        tx=X+1;ty=Y;tt=T+1;
        if(vis[tx][ty]==0||vis[tx][ty]==1||vis[tx][ty]==666)///'.'
        {
            vis[tx][ty]++;
            if(vis[tx][ty]==2)  vis[tx][ty]=-1;
            ne.x=tx; ne.y=ty; ne.time=tt;
            q.push(ne);
        }
        else if(vis[tx][ty]==4||vis[tx][ty]==5)
        {
            if(((vis[tx][ty]==4)&&(~T&1))||((vis[tx][ty]==5)&&(T&1)))
               {
                   ne.x=tx+1;ne.y=ty;ne.time=tt;
                   q.push(ne);
               }
        }

        ///left
        tx=X;ty=Y-1;tt=T+1;
        if(vis[tx][ty]==0||vis[tx][ty]==1||vis[tx][ty]==666)///'.'
        {
            vis[tx][ty]++;
            if(vis[tx][ty]==2)  vis[tx][ty]=-1;
            ne.x=tx; ne.y=ty; ne.time=tt;
            q.push(ne);
        }
        else if(vis[tx][ty]==4||vis[tx][ty]==5)
        {
            if(((vis[tx][ty]==4)&&(T&1))||((vis[tx][ty]==5)&&(~T&1)))
            {
                ne.x=tx;ne.y=ty-1;ne.time=tt;
                q.push(ne);
            }
        }

        ///right
          tx=X;ty=Y+1;tt=T+1;
        if(vis[tx][ty]==0||vis[tx][ty]==1||vis[tx][ty]==666)///'.'
        {
            vis[tx][ty]++;
            if(vis[tx][ty]==2)  vis[tx][ty]=-1;
            ne.x=tx; ne.y=ty; ne.time=tt;
            q.push(ne);
        }
        else if(vis[tx][ty]==4||vis[tx][ty]==5)
        {
            if(((vis[tx][ty]==4)&&(T&1))||((vis[tx][ty]==5)&&(~T&1)))
            {
                ne.x=tx;ne.y=ty+1;ne.time=tt;
                q.push(ne);
            }
        }
    }
    cout<<ans-1<<endl;
}
    return 0;
}

posted @ 2013-06-15 06:59  码代码的猿猿  阅读(138)  评论(0编辑  收藏  举报