码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1180:诡异的楼梯(BFS)

时间:2014-07-29 14:59:08      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:bfs

:

诡异的楼梯


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


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
地图如下: bubuko.com,布布扣


中文题, 这道题主要是在于如何判断楼梯的状态, 很容易可以想到它是每两秒一循环,所以我们可以利用奇偶性来确定。。



#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

char map[25][25];//地图
int vis[25][25];//访问数组
int dir[4][2]= {1, 0, -1, 0, 0, 1, 0, -1};//走法
int n, m;//地图大小
int ax, ay;//起点坐标

struct point//利用结构体记录
{
    int x, y;
    int time;
};

queue<point>q;

bool judge_1(int xx, int yy)
{
    if( xx>=0 && xx<n && yy>=0 && yy<m && !vis[xx][yy] && map[xx][yy]!='*' )
        return true;
    else
        return false;
}

bool judge_2(int a, int b, int c, int d, int step)//楼梯每2秒一循环,利用奇偶性来判断此时楼梯的状态,利用枚举各种情况求解
{
    if( judge_1(c, d) )//当然首先也是要判断它是否满足进行下一步条件
    {
        if( map[a][b] == '|' )
        {
            if(b==d)//这种情况是在|变为-时通过
            {
                if( step%2==0 )
                    return true;
                else
                    return false;
            }
            else  //这种情况当楼梯为初始的|时通过
            {
                if( step%2==0 )
                    return false;
                else
                    return true;
            }
        }

        else if( map[a][b] =='-' )//原理同上
        {
            if( a==c )
            {
                if( step%2==0 )
                    return true;
                else
                    return false;
            }
            else
            {
                if( step%2==0 )
                    return false;
                else
                    return true;
            }
        }
    }
    return false;
}

void bfs()
{
    memset(vis, 0, sizeof(vis));
    vis[ax][ay] = 1;
    while( !q.empty() )
    {
        point temp = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx = temp.x + dir[i][0];
            int yy = temp.y + dir[i][1];
            if( judge_1(xx, yy) )
            {
                point next;
                next.x = xx;
                next.y = yy;
                next.time = temp.time + 1;
                if( map[xx][yy]=='T' )//当到达目的地时输出并退出
                {
                    printf("%d\n", next.time);
                    return ;
                }
                else if( map[xx][yy]=='.' )//遇到空地
                {
                    vis[xx][yy] = 1 ;
                    q.push(next);
                }
                else if( map[xx][yy]=='|' || map[xx][yy]=='-' )//遇到楼梯
                {
                    point lou;
                    lou.x = xx + dir[i][0];
                    lou.y = yy + dir[i][1];
                    if( judge_2(xx, yy, lou.x, lou.y, temp.time ) ) //此时能够过楼梯
                    {
                        lou.time = temp.time + 1;
                        vis[lou.x][lou.y] = 1;
                        if( map[lou.x][lou.y]=='T' )
                        {
                            printf("%d\n", lou.time);
                            return ;
                        }
                        q.push(lou);
                    }
                    else   //不能过楼梯,停下等一秒过。
                    {
                        if( judge_1(lou.x, lou.y) )
                        {
                            lou.x = temp.x;
                            lou.y = temp.y;
                            lou.time = temp.time + 1;
                            vis[lou.x][lou.y] =  1;
                            q.push(lou);
                        }
                    }
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d%d", &n, &m) ==2 )
    {
        while(!q.empty())  q.pop();
        memset(map,0 , sizeof(map));
        point start;
        for(int i=0; i<n; i++)
        {
            getchar();
            for(int j=0; j<m; j++)
            {

                scanf("%c", &map[i][j]);
                if( map[i][j]=='S' )
                {
                    ax = i;
                    ay = j;
                }
            }
        }
        start.x = ax;//找到起点并进入队列
        start.y = ay;
        start.time = 0;
        q.push(start);
        bfs();
    }

    return 0;
}




HDU 1180:诡异的楼梯(BFS),布布扣,bubuko.com

HDU 1180:诡异的楼梯(BFS)

标签:bfs

原文地址:http://blog.csdn.net/u013487051/article/details/38233005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!