zoukankan      html  css  js  c++  java
  • hdu 1180(广搜好题)

    诡异的楼梯

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


    Problem Description
    Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
    比 如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达 目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写 的.
     
    Input
    测试数据有多组,每组的表述如下:
    第 一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位 置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N& lt;=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
     
    Output
    只有一行,包含一个数T,表示到达目标的最短时间.
    注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
     
    Sample Input
    5 5 **..T **.*. ..|.. .*.*. S....
     
    Sample Output
    7
    Hint
    Hint
    地图如下:
     
    Source
     
    当奇数时间到达'|'或'-'时,所以这个时候原图的'|'会变成'-',所以只能横向走了,'-'的话就会变成'|',只能竖着走了。
    当偶数时间到达'|'或'-'时,原图不会变,所以'|'还是可以竖着走,'-'也是横着走。
    最重要的地方来了,这个题目有个隐含条件,我开始没注意到,题目没有不可达的情况!!所以这也暗示了在前面有楼梯的情况下但是楼梯不可达的情况下我们可以在原地等一分钟,这个隐含条件一定要挖掘出来。不然会WA。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<math.h>
    #include<queue>
    using namespace std;
    typedef long long LL;
    char graph[25][25];
    bool vis[25][25];
    struct Node
    {
        int x,y,step;
    };
    Node s,t;
    int dir[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
    int n,m;
    bool check(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m||vis[x][y]||graph[x][y]=='*')
            return false;
        return true;
    }
    int bfs()
    {
        memset(vis,false,sizeof(vis));
        queue<Node> q;
        vis[s.x][s.y]=true;
        q.push(s);
        while(!q.empty())
        {
            Node node = q.front();
            q.pop();
            if(node.x==t.x&&node.y==t.y)
            {
                return node.step;
            }
            Node next;
            for(int i=0; i<4; i++)
            {
                next.x = node.x+dir[i][0];
                next.y = node.y+dir[i][1];
                next.step=node.step+1;
                if(!check(next.x,next.y)) continue;
                if(graph[next.x][next.y]=='.')
                {
                    vis[next.x][next.y]=true;
                    q.push(next);
                }
                if(graph[next.x][next.y]=='|')
                {
                    if((next.step%2==1)&&(i==0||i==1))  ///now this place change to '-'
                    {
                        next.x+=dir[i][0];
                        next.y+=dir[i][1];
                        if(check(next.x,next.y)){
                            vis[next.x][next.y]=true;
                            q.push(next);
                        }
                    }
                    else if((next.step%2==0)&&(i==2||i==3)) ///stay '|'
                    {
                        next.x+=dir[i][0];
                        next.y+=dir[i][1];
                        if(check(next.x,next.y)){
                            vis[next.x][next.y]=true;
                            q.push(next);
                        }
                    }else{  ///隐含条件,等下一分钟
                        next.x= node.x;
                        next.y = node.y;
                        q.push(next);
                    }
                }
                if(graph[next.x][next.y]=='-')
                {
                    if((next.step%2==1)&&(i==2||i==3)) ///now this place change to '|'
                    {
                        next.x+=dir[i][0];
                        next.y+=dir[i][1];
                        if(check(next.x,next.y)){
                            vis[next.x][next.y]=true;
                            q.push(next);
                        }
                    }
                    else if((next.step%2==0)&&(i==0||i==1)) ///stay '-'
                    {
                        next.x+=dir[i][0];
                        next.y+=dir[i][1];
                        if(check(next.x,next.y)){
                            vis[next.x][next.y]=true;
                            q.push(next);
                        }
                    }else{  ///等下一分钟
                        next.x= node.x;
                        next.y = node.y;
                        q.push(next);
                    }
                }
            }
        }
        return -1;
    }
    int main()
    {
    
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0; i<n; i++)
            {
                scanf("%s",graph[i]);
                for(int j=0; j<m; j++)
                {
                    if(graph[i][j]=='S')
                    {
                        s.x = i,s.y = j,s.step=0;
                        graph[i][j]='.';
                    }
                    else if(graph[i][j]=='T')
                    {
                        t.x = i,t.y = j;
                        graph[i][j]='.';
                    }
                }
            }
            int res = bfs();
            printf("%d
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    Google Authentication 机制原理
    ldap日志
    ldap + kerberos 整合
    kerberos
    U盘格式化后的恢复
    初始化脚本(Os_Init_Optimization.sh)
    拿到新机器,进行初始化和部署Nginx的过程
    python 列表生成式
    python 装饰器
    简单总结无线CPE、无线AP、无线网桥的不同之处【转】
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5572059.html
Copyright © 2011-2022 走看看