HDU 2102 A计划 (BFS)

 

Description

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。        现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。      
       

Input

输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。      
       

Output

如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。      
       

Sample Input

1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
              

Sample Output

YES
 
 
 
 
 
 
 
简单BFS,第一天不知为什么WA了一天,今天重新写了一遍,1A。
传送机要注意判断,对面可能是墙,没走过的路,走过的路,终点,传送机。
 
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
using    namespace    std;

int    N,M,TIME;
int    MAP[15][15][2];
int    VIS[15][15][2];
int    UP_DATE[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
struct    Node
{
    int    x,y,z;
    int    time;

    bool    check(void) const
    {
        if(x >= 1 && x <= N && y >= 1&& y <= M && !VIS[x][y][z] && MAP[x][y][z] != '*')
            return    true;
        return    false;
    }
};

bool    bfs(void);
void    check(void);
int    main(void)
{
    int    n;
    scanf("%d",&n);

    while(n --)
    {
        memset(VIS,0,sizeof(VIS));
        scanf("%d%d%d",&N,&M,&TIME);
        for(int i = 0;i < 2;i ++)
            for(int j = 1;j <= N;j ++)
                for(int k = 1;k <= M;k ++)
                    scanf(" %c",&MAP[j][k][i]);
                
        if(bfs())
            printf("YES\n");
        else
            printf("NO\n");
    }

    return    0;
}

void    check(void)
{
    for(int i = 0;i < 2;i ++)
    {
        for(int j = 1;j <= N;j ++)
        {
            for(int k = 1;k <= M;k ++)
                printf("%c",MAP[j][k][i]);
            cout << endl;
        }
        cout << endl << endl;
    }
}

bool    bfs(void)
{
    queue<Node>    que;
    Node    head,next,cur;
    head.x = head.y = 1;
    head.z = 0;
    head.time = 0;

    que.push(head);
    while(!que.empty())
    {
        cur = que.front();
        que.pop();
        for(int i = 0;i < 4;i ++)
        {
            next = cur;
            next.x += UP_DATE[i][0];
            next.y += UP_DATE[i][1];
            next.time += 1;

            if(!next.check())
                continue;
            VIS[next.x][next.y][next.z] = 1;

            if(MAP[next.x][next.y][next.z] == '#')
            {
                if(MAP[next.x][next.y][!next.z] == '.' && !VIS[next.x][next.y][!next.z])
                {
                    next.z = !next.z;
                    VIS[next.x][next.y][!next.z] = 1;
                }
                else    if(MAP[next.x][next.y][!next.z] == 'P' && next.time <= TIME)
                    return    true;
                else
                    continue;
            }
            else    if(MAP[next.x][next.y][next.z] == 'P' && next.time <= TIME)
                return    true;
            que.push(next);
        }
    }

    return    false;
}

 

posted @ 2015-03-25 16:28  Decouple  阅读(247)  评论(0编辑  收藏  举报