A计划 hdu2102(BFS)

A计划 hdu2102

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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,就是可能会跨层,当跨层的时候可通过异或值来跨层,在跨层的时候如果是墙就over了,如果跨层过去还是跨层那么就一直死循环跨层也over了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e5 + 10;
char map[2][12][12];
int vis[2][12][12];
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
int N, M, T;
struct node
{
    int ce;
    int x;
    int y;
    int step;
};
int flag;
void bfs()
{
    memset(vis, 0, sizeof vis);
    queue<node>que;
    node now;
    now.ce = 0;
    now.x = now.y = 0;
    now.step = 0;
    que.push(now);
    vis[0][0][0]=1;
     flag = 0;
    while (!que.empty())
    {
        
        now = que.front(); 
        if (map[now.ce][now.x][now.y] == 'P' && now.step <= T)
        {
            flag = 1;
            return;
        }
        if (map[now.ce][now.x][now.y] == 'P')
            return;

            
        for (int i = 0; i < 4; i++)
        {
            node next;
            next.x = now.x + dx[i];
            next.y = now.y + dy[i];
            next.ce = now.ce;
            int xx = next.x;
            int yy = next.y;
            int cece = next.ce;
            if (xx<0 || xx>=N)
                continue;
            if (yy<0 || yy>=M)
                continue;
            if (map[cece][xx][yy] == '*')
                continue;
            if (map[cece][xx][yy] == '#' && map[cece ^ 1][xx][yy] == '*')
                continue;
            if (map[cece][xx][yy] == '#' && map[cece ^ 1][xx][yy] == '#')
                continue;
            if (!vis[cece][xx][yy] && map[cece][xx][yy] == '#' && vis[cece ^ 1][xx][yy]==0)
            {
                vis[cece][xx][yy] = 1;
                next.ce ^= 1;
                vis[next.ce][next.x][next.y] = 1;
                next.step = now.step + 1;
                que.push(next);
            }
            if (!vis[cece][xx][yy] && map[cece][xx][yy] != '#')
            {
                vis[cece][xx][yy] = 1;
                next.step = now.step + 1;
                que.push(next);
            }

        }
        que.pop();
    }
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        char ch;
        scanf("%d%d%d", &N, &M, &T);

        for (int i = 0; i < 2; i++)
        { 
            for (int j = 0; j < N; j++)
                scanf("%s", map[i][j]);
            getchar();
        }
         bfs();
         if (flag)
             puts("YES");
         else
             puts("NO");
    }
    
}
View Code
原文地址:https://www.cnblogs.com/smallhester/p/9942047.html