zoukankan      html  css  js  c++  java
  • HDU 1180 诡异的楼梯 (广搜)

    题目链接

    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

    分析:

    这里有一个比较难得地方就是对于走到楼梯的地方时他能不能走,如何走花费的时间比较短,这里对于楼梯的判断要写一个具体的函数来判断。

    代码:

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<iostream>
    using namespace std;
    char tu[21][21];
    int bj[21][21];
    int sx,sy,ex,ey,n,m;
    int nex1[4][2]= {{1,0},{-1,0},{0,1},{0,-1} };
    struct node
    {
        int x;
        int y;
        int step;
    } now,nex;
    bool operator <(node a,node b)
    {
        return a.step>b.step;
    }
    int solve(int x,int y,int time,char ch)
    {
        if(ch == '|' && x == 0)//如果楼梯是竖着的,而且我在水平方向上
        {
            if(time%2==0)//偶数步,则楼梯还是竖着的,等一秒楼梯变成平的,在过去
                time+=2;
            else
                time++;
        }
        if(ch == '|' && y == 0)//如果楼梯是竖着的,处于竖直方向上
        {
            if(time%2==0)//偶数步,楼梯为竖直的,这时候直接过去
                time++;
            else
                time+=2;//否则等一秒,楼梯变成竖着的过去
        }
        if(ch == '-' && x == 0)//如果楼梯是横着的,现在在水平方向上
        {
            if(time%2 == 0)//偶数步,到达楼梯前一个位置楼梯还是横的,直接过去
                time++;
            else
                time+=2;//否则,等一分钟楼梯变成横的在过去
        }
        if(ch == '-' && y == 0)//如果楼梯是横着的,现在在竖直方向上
        {
            if(time%2==0)//偶数步,楼梯是当前是横着的,过不去,再等一秒
                time+=2;
            else
                time++;//否则直接过去。
        }
        return time;
    }
    void bfs(int x,int y)
    {
        priority_queue<node>q;
        now.x=x;
        now.y=y;
        now.step=0;
        q.push(now);
        while(!q.empty())
        {
            now=q.top();
            q.pop();
            if(now.x==ex&&now.y==ey)
            {
                cout<<now.step<<endl;
                return ;
            }
            for(int i=0; i<4; i++)
            {
                nex.x=now.x+nex1[i][0];
                nex.y=now.y+nex1[i][1];
                if(tu[nex.x][nex.y]!='*'&&nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&bj[nex.x][nex.y]==0)
                {
                    if(tu[nex.x][nex.y]=='.'||tu[nex.x][nex.y]=='T')
                    {
                        nex.step=now.step+1;
                        bj[nex.x][nex.y]=1;
                        q.push(nex);
                    }
                    else if(tu[nex.x][nex.y]=='|'||tu[nex.x][nex.y]=='-')
                    {
                        int tx=nex.x+nex1[i][0];
                        int ty=nex.y+nex1[i][1];
                        if(tx>=0&&tx<n&&ty>=0&&ty<m&&bj[tx][ty]==0&&tu[tx][ty]!='*')
                        {
                            nex.step = solve(nex1[i][0],nex1[i][1],now.step,tu[nex.x][nex.y]);
                            nex.x=tx;
                            nex.y=ty;
                            bj[nex.x][nex.y]=1;
                            q.push(nex);
                        }
                    }
                }
            }
        }
        return ;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(bj,0,sizeof(bj));
            for(int i=0; i<n; i++)
                for(int j=0; j<m; j++)
                {
                    cin>>tu[i][j];
                    if(tu[i][j]=='S')
                    {
                        sx=i;
                        sy=j;
                    }
                    if(tu[i][j]=='T')
                    {
                        ex=i;
                        ey=j;
                    }
                }
            bj[sx][sy]=1;
            bfs(sx,sy);
        }
        return 0;
    }
    
    
  • 相关阅读:
    php实现求链表中倒数第k个节点
    在python正在使用mysql
    1002. 写这个号码 (20)(数学啊 ZJU_PAT)
    Lua 环境结构 --Linux
    Java程序猿JavaScript学习笔记(4——关闭/getter/setter)
    C/C++数据对齐汇总
    多线程
    11gRAC CHM 管理
    hdu 4059 The Boss on Mars(纳入和排除)
    模板方法模式分析、图表和基本代码
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6767350.html
Copyright © 2011-2022 走看看