hdu A计划
这道题是一道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计划的更多相关文章
- hdu 2102 A计划
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- A计划 HDU - 2102
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2102 A计划(BFS/DFS走迷宫)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 2102 A计划(两层地图加时间限制加传送门的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others) Me ...
- HDU 2102 A计划 (BFS)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 6118 度度熊的交易计划 【最小费用最大流】 (2017"百度之星"程序设计大赛 - 初赛(B))
度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- I - A计划 HDU - 2102
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
随机推荐
- 32.C++不能被继承的类[C++ Final Class]
[题目] 用C++设计一个不能被继承的类. [分析] 这是Adobe公司2007年校园招聘的最新笔试题.这道题除了考察应聘者的C++基本功底外,还能考察反应能力,是一道很好的题目. 在Java中定义了 ...
- TinyMCE textarea 输入框外部程序动态修改方法
TinyMCE textarea 输入框外部程序动态修改方法 Public Function C2IE_TINYMCE(ByVal id As String, ByVal value As Strin ...
- Java for LeetCode 033 Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- AJAX,JSON用户名校验
效果 开发结构 1,src部分有两个包dao和servlet 1.1dao包下有两个数据库工具类文件 SqlHelper.java package org.guangsoft.dao; import ...
- GLSL Entry point not found
解决方案: 在引用OpenGL的头文件 #include <GL/glew.h>#include <GL/glut.h> 前添加 #define GLUT_DISABLE_AT ...
- Git自动部署
Git自动部署文件位于repository下面的hooks里的post-receive #!/bin/sh set -e git-update-server-info gitosis-run-hook ...
- 使用ASP.NET Web API自带的类库实现对CORS的支持(在开发中使用这种方式)(转载)
在<通过扩展让ASP.NET Web API支持W3C的CORS规范>中我们通过自定义的HttpMessageHandler为ASP.NET Web API赋予了跨域资源共享的能力,具体来 ...
- 使用HttpClient操作ASP.NET Web API 2.1增删改查
使用NuGet包安装Microsoft ASP.NET Web API 2.1 Client Libraries, 调用方式代码如下: HttpClient client = new HttpClie ...
- hdu 5115 区间dp ***
题意:有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么我们受到的伤害值为这只狼的攻击值与它旁边的两只狼的附加值的和,求把所有狼都杀光受到的最小的伤害值. 枚举中间k作为最后杀死的 ...