hdu2102A计划(三维bfs)

题目链接

题意

在一个两层迷宫内,骑士从起点是否能在给定步数之内找到公主,'#'代表传送门,会传送到另一个平面该位置去,'*'代表墙,'P'代表公主

解题思路

水题,用bfs模拟即可,就是有个坑 传送门被传到的位置不能是墙(由题意)和传送门(会造成无限传送)

AC代码

#include<vector>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<set>
#include<cstring>
#include<functional>
#include<map>
#include<cmath>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,pii> PII;
const int maxn = 15;
int N,M,S;
struct Node 
{
    int x,y,z;
    int step;
};
char G[2][maxn][maxn];;
bool vis[2][maxn][maxn];
bool ok = 0;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

void bfs(Node beg)
{
    queue<Node> Q;
    Q.push(beg);
    while(!Q.empty()){
        Node t = Q.front();
        Q.pop();
        if(t.step > S)continue;
        if(G[t.z][t.x][t.y]=='P'){
            ok = 1;
            return ;
        }
        for(int i=0;i<4;i++){
            int nx = t.x + dx[i];
            int ny = t.y + dy[i];
            int nz = t.z;
            if(nx<0||ny<0||ny>=M||nx>=N)continue;
            if(G[nz][nx][ny]=='*')continue;
            if(vis[nz][nx][ny])continue;
            vis[nz][nx][ny] = 1;
            if(G[nz][nx][ny]=='#'&&G[nz^1][nx][ny]!='*'&&G[nz^1][nx][ny]!='#'){
                Q.push((Node){nx,ny,nz^1,t.step+1});
            }else if(G[nz][nx][ny]!='#')
                Q.push((Node){nx,ny,nz,t.step+1});
        }
    }
}

int main(int argc, char const *argv[])
{
    int T = 0;
    cin >> T;
    while(T--){
        ok = 0;
        memset(vis,0,sizeof(vis));
        cin >> N >> M >> S;
        Node beg;
        for (int k = 0; k < 2; k++)
        {
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    cin >> G[k][i][j];
                }
            }
        }
        beg = (Node){0,0,0,0};
        bfs(beg);
        if(ok){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
    
    return 0;
}
posted @ 2018-10-18 16:21  django_lf  阅读(125)  评论(0编辑  收藏  举报