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

来源:互联网 发布:腾讯企业邮箱的域名 编辑:程序博客网 时间:2024/05/03 15:47

题目链接: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
原创粉丝点击