【刷题记录】 HDU 2102 A计划

本文介绍了一款迷宫救援游戏的算法实现过程,玩家需要在限定时间内通过迷宫营救公主。文章详细阐述了使用BFS算法进行路径搜索的方法,并针对迷宫中的特殊元素进行了预处理以避免无限循环等问题。

目录

一. 问题描述

二. 题解代码


一. 问题描述

Problem Description

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。 
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。

Input

输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。

Output

如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

二. 题解代码

        BFS+预处理。要注意当传送门传过去也是传送门是会产生无限传送的情况,因此这样的传送门是不能走的,我们可以直接把他变成墙去处理。除此之外,当传送门对面是墙时,传过去就死,因此也不能走,所以这里的传送门也变成墙去处理。最后题目要求到达公主位置时间<=t即可 ,因此这里用BFS 求最短时间判断。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = 10000  + 10;
char maps1[11][11];
char maps2[11][11];
bool judge1[11][11];
bool judge2[11][11];
int n,m,t;
int px,py;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
struct Node
{
    int x,y,time,lo;
    Node(int a,int b,int c,int d):x(a),y(b),time(c),lo(d) {}
};
int BFS(int a,int b)
{
    judge1[a][b] = 1;
    queue<Node> que;
    que.push(Node(0,0,0,1));
    while(!que.empty()){
        Node node = que.front();
        que.pop();
        if(node.lo==1&&maps1[node.x][node.y]=='P')return node.time;
        else if(node.lo==2&&maps2[node.x][node.y]=='P')return node.time;
        for(int i = 0;i<4;i++){
           int fx = node.x + dx[i];
           int fy = node.y + dy[i];
           int k = node.lo;
           if(fx>=0&&fx<n&&fy>=0&&fy<m){
                if(k==1&&(maps1[fx][fy]=='.'||maps1[fx][fy]=='P')&&!judge1[fx][fy]){
                    judge1[fx][fy] = 1;
                    que.push(Node(fx,fy,node.time + 1,k));
                }
                else if(k==2&&(maps2[fx][fy]=='.'||maps2[fx][fy]=='P')&&!judge2[fx][fy]){
                    judge2[fx][fy] = 1;
                    que.push(Node(fx,fy,node.time + 1,k));
                }
                else if(k==1&&maps1[fx][fy]=='#'&&!judge2[fx][fy]){
                    judge2[fx][fy] = 1;
                    que.push(Node(fx,fy,node.time+1,2));
                }
                else if(k==2&&maps2[fx][fy]=='#'&&!judge1[fx][fy]){
                    judge1[fx][fy] = 1;
                    que.push(Node(fx,fy,node.time+1,1));
                }
           }
        }
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        px = py = -1;
        scanf("%d%d%d",&n,&m,&t);
        memset(judge1,0,sizeof(judge1));
        memset(judge2,0,sizeof(judge2));
        for(int i = 0;i<n;i++){
            cin>>maps1[i];
        }
        for(int i = 0;i<n;i++){
           for(int j = 0;j<m;j++){
              cin>>maps2[i][j];
              if(maps2[i][j]=='#'&&maps1[i][j]=='#'){
                   maps1[i][j] = maps2[i][j] = '*';
              }
              else if(maps2[i][j]=='#'&&maps1[i][j]=='*')maps2[i][j] = '*';
              else if(maps1[i][j]=='#'&&maps2[i][j]=='*')maps1[i][j] = '*';
           }
        }
        int ans = BFS(0,0);
//        printf("%d\n",ans);
        if(ans!=-1&&ans<=t){
            printf("YES\n");
        }
        else printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿阿阿安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值