hdu A计划
阅读原文时间:2024年06月07日阅读:1

这道题是一道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=&&x=&&y 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"<>c;
while(c--)
{
int i,j,k;
cin>>n>>m>>t;
for(k=;k<=;k++) { for(i=;i>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 ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器