HDU - 2102 A计划 【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102

有两个要注意的地方

1,走到传送门是强制传送的。

2,如果传送到另一层又是一个传送门,会死循环,直接排除掉这个点。

#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
const int Maxn = 4e5+10;

struct Node {
    int i, j, k, step;
};

int N, M, T, dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char G[5][15][15];
bool vis[5][15][15];

void bfs() {
    memset(vis, false, sizeof(vis));
    queue<Node> qu;
    Node tmp;
    bool ok = false;
    for(int i = 0; i < 2; ++i) {
        for(int j = 0; j < N; ++j) {
            for(int k = 0; k < M; ++k) {
                if(G[i][j][k] == 'S') {
                    tmp.i = i; tmp.j = j; tmp.k = k;
                    tmp.step = 0;
                    ok = true; break;
                }
            }
            if(ok) break;
        }
        if(ok) break;
    }

    vis[tmp.i][tmp.j][tmp.k] = true;
    qu.push(tmp);

    while (!qu.empty()) {
        tmp = qu.front(); qu.pop();
        if(G[tmp.i][tmp.j][tmp.k] == '#') tmp.i = (tmp.i+1)%2;
        if(G[tmp.i][tmp.j][tmp.k] == '*' || G[tmp.i][tmp.j][tmp.k] == '#') continue;

        if(tmp.step > T) {
            printf("NO\n"); return;
        }
        if(G[tmp.i][tmp.j][tmp.k] == 'P') {
            printf("YES\n"); return;
        }

        for(int i = 0; i < 4; ++i) {
            Node now = tmp;
            now.j += dir[i][0]; now.k += dir[i][1];
            if(now.j < 0 || now.j > N-1 || now.k < 0 || now.k > M-1
               || vis[now.i][now.j][now.k] || G[now.i][now.j][now.k] == '*') continue;
            now.step++;
            vis[now.i][now.j][now.k] = true;
            qu.push(now);
        }
    }
    printf("NO\n");
}

int main(void)
{
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d", &N, &M, &T);
        for(int i = 0; i < N; ++i) scanf("%s", G[0][i]);
        for(int i = 0; i < N; ++i) scanf("%s", G[1][i]);

        bfs();
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值