hdu 2102 A计划

先预处理把所有通过传送门之后不可能的情况都把传送门换成墙

还有就是遇到传送门会到第二层.用异或方便一些。因为0^1 = 1.   1 ^ 1 = 0; 遇到传送门就异或一次,表示到了另外一层迷宫

最后起点到P点的最短距离小于T, 返回真

/***********************************************
 * Author: fisty
 * Created Time: 2015/2/13 15:41:32
 * File Name   : two_I.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 20
int n, m, t;
char G[MAX_N][MAX_N][2];
int  vis[MAX_N][MAX_N][2];
int  d[MAX_N][MAX_N][2];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct Point{
    int first;
    int second;
    int z;
    Point(int _x, int _y, int _z):first(_x), second(_y), z(_z){}
};
bool bfs(){
    Memset(d, 0);
    d[0][0][0] = 0;
    vis[0][0][0] = 1;
    queue<Point> que;
    que.push(Point(0, 0, 0));
    while(que.size()){
        Point p = que.front(); que.pop();
        if(G[p.first][p.second][p.z] == 'P') {
            //如果可以到达
            if(d[p.first][p.second][p.z] <= t)
                return true;
            return false;
        }
        for(int i = 0;i < 4; i++){
            int x = p.first + dx[i];
            int y = p.second + dy[i];
            int z = p.z;
            if(G[x][y][z] == '#'){
                //进入下一层
                vis[x][y][z] = 1;
                z = z^1;
            }
            if(!vis[x][y][z] && G[x][y][z] != '*' && x < n && x >= 0 && y < m && y >=0){
                vis[x][y][z] = 1;
                que.push(Point(x, y, z));
                d[x][y][z] = d[p.first][p.second][p.z] + 1;
            }
        }
    }
    return false;
}
int main() {
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Kcase;
    cin >> Kcase;
    while(Kcase--){
        cin >> n >> m >> t;
        Memset(G, 0);
        FOR(k, 0, 2){
            FOR(i, 0, n){
                FOR(j, 0, m){
                    cin >> G[i][j][k];
                }
            }
        }
        //预处理,把传送门不可以的地方都换成墙
        FOR(i, 0, n){
           FOR(j, 0, m){
                if(G[i][j][0] == '#' && G[i][j][1] == '#'){
                    G[i][j][1] = G[i][j][0] = '*';
                }else if(G[i][j][0] == '#' && G[i][j][1] == '*'){
                    G[i][j][0] = G[i][j][1] = '*';
                }else if(G[i][j][0] == '*' && G[i][j][1] == '#'){
                    G[i][j][0] = G[i][j][1] = '*';
                }
            }
        }
        Memset(vis, 0);
        if(bfs())
            cout << "YES" << endl;
        else 
            cout << "NO" << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值