这道题是一道bfs的题目,因为题目中给的数据很小,所以可以采用优先队列的方式来简化处理。这道题在搜索的过程中要注意map1的不同层次的转换,即对'#'的理解。之前wa了两次是因为我考虑了如果上下两层对应位置都是'#'时输出'NO’,但是这是错误的,可以不考虑这个问题,也可以当上下两层都是‘#’时将它们赋成‘*’;

具体步骤看程序及程序上的注释:

#include"iostream"
#include"stdio.h"
#include"cmath"
#include"string.h"
#include"queue"
#include"algorithm"
#define mx 15
using namespace std;
char map1[mx][mx][];//三维的地图
int dir[][]={{,},{,-},{-,},{,}};//上下左右四个方向
int n,m,t,sx,sy,sz,ex,ey,ez;
struct node
{
int x,y,z,time;
friend bool operator<(node a,node b)//优先队列中的元素按时间的从小到大排序
{
return b.time<a.time;
}
};
int judge(int x,int y,int z)//判断当前位置的情况
{
if(x>=&&x<n&&y>=&&y<m)
{
if(map1[x][y][z]=='.') return ;
else if(map1[x][y][z]=='#') return ;
else return -;
}
else return -;
}
void bfs()
{
priority_queue<node> q;//构造优先队列
node cur,next;
int i;
cur.x=sx;
cur.y=sy;
cur.z=sz;
cur.time=;
q.push(cur);
while(!q.empty())//队列非空就进行循环
{
cur=q.top();
q.pop();//记得出队列
if(cur.x==ex&&cur.y==ey&&cur.z==ez&&cur.time<=t)//找到了目标位置就输出并返回
{cout<<"YES"<<endl;return;}
for(i=;i<;i++)
{
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
next.z=cur.z;
int re=judge(next.x,next.y,next.z);//判断走过一步后的情况
if(re==-) continue;
else
{
if(re==)
{
map1[next.x][next.y][next.z]='*';
next.time=cur.time+;
q.push(next);
}
else if(re==)
{
next.z=(cur.z+)%;
re=judge(next.x,next.y,next.z);//判断时空隧道的对应位置是什么
if(re==)
{ next.time=cur.time+;map1[next.x][next.y][next.z]='*';q.push(next);}
}
}
}
}
cout<<"NO"<<endl;
}
int main()
{
int c;
cin>>c;
while(c--)
{
int i,j,k;
cin>>n>>m>>t;
for(k=;k<=;k++)
{
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
cin>>map1[i][j][k];
if(map1[i][j][k]=='S')
{
sx=i;sy=j;sz=k;
map1[i][j][k]='*';
}
else if(map1[i][j][k]=='P')
{
map1[i][j][k]='.';
ex=i;ey=j;ez=k;
}
}
}
if(k==)getchar();////注意在输入过程中的空格
}
bfs();
}
return ;
}

hdu A计划的更多相关文章

  1. hdu 2102 A计划

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...

  2. [HDU 2102] A计划(搜索题,典型dfs or bfs)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. A计划 HDU - 2102

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU 2102 A计划(BFS/DFS走迷宫)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

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

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others)    Me ...

  6. HDU 2102 A计划 (BFS)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. HDU 6118 度度熊的交易计划 【最小费用最大流】 (2017"百度之星"程序设计大赛 - 初赛(B))

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. I - A计划 HDU - 2102

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. hdu 2102 A计划-bfs

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. host

    #Google Services START209.116.186.241 0.docs.google.com209.116.186.241 0.drive.google.com209.116.186 ...

  2. UVA 10325 The Lottery( 容斥原理)

    The Sports Association of Bangladesh is in great problem with their latest lottery `Jodi laiga Jai'. ...

  3. ios xcode Could not load the "MyImage.png" image referenced from a nib in the bundle with identifier "com.mytest.MyProject"

    出现找不到xib指定的图片,需要指定图片的完整路径,不能只是图片名 详见:http://vocaro.com/trevor/blog/2012/10/21/xcode-groups-vs-folder ...

  4. Popular Cows(codevs 2186)

    题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表 ...

  5. ImageView中XML属性src和background的区别

    background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸. src是图片内容(前景),bg是背景,可以同时使用. 此外:scaleType只对sr ...

  6. php编译报错 configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

    ➜ php- yum install -y curl-devel

  7. MySQL监控

    http://blog.csdn.net/zreodown/article/details/8158469

  8. display : -webkit-box-inline 的理解

    发现: 最近在做移动端的东西,说起移动端弹性盒子布局真是无往不利,用起来特别爽,我也是偶尔间发现的这个属性并且它的用法,在网上基本查不到这个属性的资料(个人看法).如果没有听说过(display:bo ...

  9. phpStorm支持CodeIgniter代码提示/自动完成

    下载这个文件phpstorm-ci-ac 或者去github下载解压里面的三个文件到ci根目录下然后找到这三个文件 system\core\Controller.phpsystem\core\Mode ...

  10. GRE红宝书5-6

    page5 adopt: adoration: adore:   --ore讲话, oration演讲 adorn:   orn表示装饰, ornate adulation:      adulate ...