HDU 2102 A计划 (BFS)

中文题面,题意略

坑点:两个传送门之间不能传,WA到天荒地老~~~~

代码:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 10000000007
#define mem(a) memset(a,0,sizeof(a))

using namespace std;

typedef pair <int,int> pii;
const int maxn = 1000+5 , inf = 0x3f3f3f3f;

char G[2][maxn][maxn];
bool vis[2][maxn][maxn];

int n,m,t;

const int dx[] = {1,-1,0,0};
const int dy[] = {0,0,-1,1};

bool can(int x,int y,int pos,int d){
    if(1<=x&&x<=n&&1<=y&&y<=m&&!vis[pos][x][y]&&d<=t&&G[pos][x][y]!='*')
        return true;
    return false;
}

struct Node{
    int x,y,d,pos;
    Node(int xx,int yy,int dd,int ee):x(xx),y(yy),d(dd),pos(ee){};
};

void bfs(){
    queue<Node>q;
    q.push(Node(1,1,0,0));
    vis[0][1][1] = 1;
    while(!q.empty()){
        Node now = q.front();q.pop();
        if(G[now.pos][now.x][now.y]=='P'){
            cout<<"YES"<<endl;
            return;
        }
        for(int i=0;i<4;i++){
            int nx = now.x+dx[i];
            int ny = now.y+dy[i];
            if(G[now.pos][nx][ny]=='#'){
                if(G[!now.pos][nx][ny]!='*'&&G[!now.pos][nx][ny]!='#'&&can(nx,ny,now.pos,now.d+1)&&!vis[!now.pos][nx][ny]){
                    vis[!now.pos][nx][ny] = 1;
                    vis[now.pos][nx][ny] = 1;
                    q.push(Node(nx,ny,now.d+1,!now.pos));
                }
            }else{
                if(can(nx,ny,now.pos,now.d+1)){
                    vis[now.pos][nx][ny] = 1;
                    q.push(Node(nx,ny,now.d+1,now.pos));
                }
            }
        }
    }
    cout<<"NO"<<endl;
}

int main(){
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        mem(G);mem(vis);
        scanf("%d%d%d",&n,&m,&t);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            scanf(" %c",&G[0][i][j]);
        }
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            scanf(" %c",&G[1][i][j]);
        }
        bfs();
    }
}

猜你喜欢

转载自blog.csdn.net/Insist_77/article/details/81480096