您的位置:首页 > 其它

hdu 2102 A计划

2015-07-17 18:09 323 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2102

A计划

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 12340 Accepted Submission(s): 3024



Problem 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


之前一直WA,原来是没有考虑到来回无限穿梭的情况,崩溃啊!






<span style="color:#993399;">#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>

using namespace std;

#define N 210
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8

const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

struct node
{
    int x, y, t;
} S, P;

int n, m, t, vis

;
char g

;

void BFS ();

int main ()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> m >> t;
        for (int i=0; i<2*n; i++)
            for (int j=0; j<m; j++)
                cin >> g[i][j];

        S = (node) {0, 0, 0};
        BFS ();
    }
    return 0;
}

void BFS ()
{
    memset (vis, 0, sizeof (vis));
    node qq = S;
    queue <node> que;
    que.push (qq);
    vis[qq.x][qq.y] = 1;

    while (que.size())
    {
        qq = que.front();
        que.pop();

        if (g[qq.x][qq.y] == 'P' && qq.t <= t )
        {
            puts ("YES");
            return;
        }

        for (int i=0; i<4; i++)
        {
            node q = qq;
            q.x += dir[i][0], q.y += dir[i][1];

            if (q.x>=0 && q.x<2*n && q.y>=0 && q.y<m && !vis[q.x][q.y])
            {
                if (g[q.x][q.y] == '#' && g[q.x+n][q.y] != '*' && </span><span style="color:#3333ff;"><strong>g[q.x+n][q.y] != '#'</strong></span><span style="color:#993399;"> && q.x+n>=0 && q.x+n<2*n && q.x>=0 && q.x<n)
                {
                    vis[q.x][q.y] = 1, vis[q.x+n][q.y] = 1;
                    q.x += n;
                    q.t++;
                    que.push(q);
                }           //在第一层中遇到时空穿梭机
                else if (g[q.x][q.y] == '#' && g[q.x-n][q.y] != '*' && </span><span style="color:#3333ff;"><strong>g[q.x+n][q.y] != '#'</strong></span><span style="color:#993399;"> && q.x-n>=0 && q.x-n<2*n&& q.x>=n && q.x<2*n)
                {
                    vis[q.x][q.y] = 1, vis[q.x-n][q.y] = 1;
                    q.t++;
                    q.x -= n;
                    que.push(q);
                }           //在第二层中遇到时空穿梭机
                else if ((g[q.x][q.y] == '.' || g[q.x][q.y] == 'P') && ((qq.x!=n-1 || q.x!=n) && (q.x!=n-1 || qq.x!=n)))
                {
                    vis[q.x][q.y] = 1;
                    q.t++;
                    que.push(q);
                }//在第一层最下边时没有穿梭机不能进入第二层,在第二层最上边时没有穿梭机也不能进入第一层
            }
        }
    }
    puts ("NO");
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: