HDU 2102

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

struct node{
    int index,x,y,time;
    friend bool operator < (node a,node b){
        return a.time>b.time;
    }
};

char graph[2][11][11];
bool vis[2][11][11];
int t,n,m;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

int bfs(){
    priority_queue<node> q;
    node cur,next;
    memset(vis,false,sizeof(vis));
    cur.index = 0;
    cur.time = 0;
    cur.x = 0;
    cur.y = 0;
    vis[0][0][0] = true;
    q.push(cur);
    while(!q.empty()){
        cur = q.top();
        q.pop();
        if(cur.time > t)   //若当前队列中时间最短的都超时,则所有节点均超时
            return -1;
        if(graph[cur.index][cur.x][cur.y]=='P')
            return 1;  //到达目标状态
        for(int i=0;i<4;i++){
            next = cur;
            next.x += dx[i];
            next.y += dy[i];
            int idx = next.index;
            int x = next.x;
            int y =next.y;
            if(!vis[idx][x][y] && x>=0 && x<n && y>=0 && y<m && graph[idx][x][y]!='*'){
                vis[idx][x][y] = 1;
                if(graph[idx][x][y]=='#' && !vis[!idx][x][y]){  //传送机
                    if(graph[!idx][x][y]=='#' || graph[!idx][x][y]=='*')
                        continue;
                    next.index = !idx;
                    vis[next.index][next.x][next.y] = 1;
                    next.time = cur.time+1;
                    q.push(next);
                }
                else{
                    next.time = cur.time+1;
                    q.push(next);
                }
            }
        }
    }
    return -1;
}

int main(){
    int tt;
    scanf("%d",&tt);
    while(tt--){
        scanf("%d%d%d",&n,&m,&t);
        for(int i=0;i<n;i++)
            scanf("%s",graph[0][i]);
        for(int i=0;i<n;i++)
            scanf("%s",graph[1][i]);
        int res = bfs();
        if(res!=-1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值