HDU 2102A计划(BFS)

A计划

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

struct node {
    int x, y, floor;
    int step;
};

int t, n, m, lim;
int s[3],e[3];
int to[4][2] = {1,0,0,1,-1,0,0,-1};
char map[2][15][15];
int vis[2][15][15];

int check(int floor, int x,int y) {
    if(x < 0 || y < 0 || x >= n || y >=m )
        return 1;

    if(map[floor][x][y] == '*')//墙就没法扩展
        return 1;

    return 0;
}

void bfs() {
    memset(vis, 0, sizeof(vis));
    node a, next;
    queue<node> Q;
    int i, j, k;
    a.floor = s[0];
    a.x = s[1];
    a.y = s[2];
    a.step = 0;

    vis[s[0]][s[1]][s[2]] = 1;
    Q.push(a);

    while(!Q.empty()) {
        a = Q.front();
        Q.pop();

        if(a.floor == e[0] && a.x == e[1] && a.y == e[2]) {
            printf("YES\n");
            return ;
        }

        if(a.step >= lim)
            break;

        for(i = 0; i < 4; i++) {

            next = a;
            next.x += to[i][0];
            next.y += to[i][1];
            next.step++;
            if(check(next.floor, next.x, next.y))
                continue;

            if(vis[next.floor][next.x][next.y])
                continue;

            vis[next.floor][next.x][next.y] = 1;
            if(map[next.floor][next.x][next.y] == '#') {//传输机位置

                next.floor = !next.floor;
                if(check(next.floor, next.x, next.y))
                    continue;

                if(vis[next.floor][next.x][next.y])
                    continue;
                //这里要判断,会不会上下都是传送机,来回传就没有意义
                if(map[next.floor][next.x][next.y] == '*' || map[next.floor][next.x][next.y] == '#')
                    continue;

                vis[next.floor][next.x][next.y] = 1;
            }

            if(next.floor == e[0] && next.x == e[1] && next.y == e[2]) {
                printf("YES\n");
                return ;
            }

            Q.push(next);
        }
    }
    printf("NO\n");
}

int main() {
    //freopen("data.in", "r", stdin);

    int i, j, k;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &n, &m, &lim);
        for(k = 0; k < 2; k++) {
            for(i = 0; i < n; i++) {
                scanf("%s", map[k][i]);
                for(j = 0; j < m; j++) {
                    if(map[k][i][j] == 'S') {
                        s[0] = k, s[1] = i, s[2] = j;
                    } else if(map[k][i][j] == 'P') {
                        e[0] = k, e[1] = i, e[2] = j;
                    }
                }
            }
        }

        bfs();
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值