HDU 2102 A计划(两层地图加时间限制加传送门的bfs)

传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=2102

A计划

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28972    Accepted Submission(s): 7274


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”。
 
Sample Input
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
 
Sample Output
YES
 
Source
 
Recommend
xhd   |   We have carefully selected several similar problems for you:  1253 1548 1728 1240 1072 
 
分析:
地图是两层的
所以地图数组改为3维的,结构体四个变量:x,y,typr:当前地图层,step:步数
注意:
1.遇到当前位置是墙 跳过
2.找到了公主,直接return
3.走过的地方变成墙(*)标记
3.下一个位置是墙或者当前时间大于限定时间 跳过
4.如果下一个位置是传送门,传送过去不要时间,如果传送过去是墙或者还是传送门,跳过(是墙的话会撞死)(具体参考代码)
 
小技巧:层数设置,0,1
变换层的时候1减去当前层号就是以后的层号!
 
code:
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
#define max_v 105
struct node
{
    int x,y,step,type;
};
char G[2][12][12];
int n,m,t;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
int bfs(int sx,int sy)
{
    queue<node> q;
    node p,next;

    p.x=sx;
    p.y=sy;
    p.step=0;
    p.type=0;

    q.push(p);

    while(!q.empty())
    {
        p=q.front();
        q.pop();

        if(G[p.type][p.x][p.y]=='*')//当前位置 墙
            continue;

         if(G[p.type][p.x][p.y]=='P')//找到公主
            return 1;

        G[p.type][p.x][p.y]='*';//走过标记

        for(int i=0;i<4;i++)
        {
            next.x=p.x+dir[i][0];
            next.y=p.y+dir[i][1];
            next.step=p.step+1;
            next.type=p.type;

            if(next.step>t||G[next.type][next.x][next.y]=='*')//下一个位置超时 或者是墙
                continue;

            if(G[next.type][next.x][next.y]=='#')//下一个位置是传送门
            {
                G[next.type][next.x][next.y]='*';//标记走过

                next.type=1-next.type;//传送门对应层的位置

                if(G[next.type][next.x][next.y]=='#'||G[next.type][next.x][next.y]=='*')//传送门对应层是传送门或者墙
                {
                    G[next.type][next.x][next.y]=G[1-next.type][next.x][next.y]='*';//两个图对应的位置都标记
                    continue;
                }
            }
            q.push(next);
        }
    }
    return 0;
}
int main()
{
    int T;
   scanf("%d",&T);
    while(T--)
    {
        cin>>n>>m>>t;
        memset(G,'*',sizeof(G));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>G[0][i][j];
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>G[1][i][j];
            }
        }
        if(bfs(1,1))
            printf("YES
");
        else
            printf("NO
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yinbiao/p/9392261.html