HDU 2102 A计划 (BFS + 预处理)

31 篇文章 0 订阅

题目链接:A计划


解析:三维的搜索,但是只有两层。先将地图预处理:两层对应位置都是‘#’的和一层是‘#’一层是‘*'的,两层都处理成’*‘。再bfs即可。




AC代码:

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

char mz[2][12][12];
bool vis[2][12][12];
int dir[4][2] = {1,0, -1,0, 0,1, 0,-1};
int n, m, T;

struct Node{
    int z, x, y;
    int step;
}P;
queue<Node> q;

bool bfs(){
    while(!q.empty()) q.pop();
    q.push(Node{0, 0, 0, 0});
    vis[0][0][0] = true;
    while(!q.empty()){
        Node now = q.front(); q.pop();
        if(mz[now.z][now.x][now.y] == 'P'){
            return now.step <= T;
        }
        Node tp;
        for(int i=0; i<4; i++){
            tp.x = now.x + dir[i][0];
            tp.y = now.y + dir[i][1];
            tp.z = now.z;
            tp.step = now.step + 1;
            if(tp.step > T) continue;
            if(tp.x<0 || tp.x>=n || tp.y<0 || tp.y>=m) continue;
            if(mz[tp.z][tp.x][tp.y] == '*') continue;
            if(vis[tp.z][tp.x][tp.y]) continue;
            if(mz[tp.z][tp.x][tp.y] == '#') tp.z ^= 1;
            if(mz[tp.z][tp.x][tp.y] != '*' && !vis[tp.z][tp.x][tp.y]){
                vis[tp.z][tp.x][tp.y] = true;
                q.push(tp);
            }
        }
    }
    return false;
}

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif //sxk

    int C;
    scanf("%d", &C);
    while(C--){
        scanf("%d%d%d", &n, &m, &T);
        for(int i=0; i<2; i++)
            for(int j=0; j<n; j++)
                scanf("%s", mz[i][j]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if((mz[0][i][j] == '#' && mz[1][i][j] == '#') || (mz[0][i][j] == '#' && mz[1][i][j] == '*') || (mz[1][i][j] == '#' && mz[0][i][j] == '*'))
                    mz[0][i][j] = mz[1][i][j] = '*';
        memset(vis, false, sizeof(vis));
        puts(bfs() ? "YES" : "NO");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值