hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240
开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向。
第一维代表在第几层。后两维代表行和列。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
int x,y,z,step;
bool operator < (const point a) const
{
return step>a.step;
}
};
point t,e;
int n;
char maze[][][];
int used[][][];
int dir[][]={{-,,},{,,},{,-,},{,,},{,,-},{,,}}; void bfs()
{
memset(used,,sizeof(used));
priority_queue<point>que;
que.push(t);
used[t.z][t.x][t.y]=;
while(!que.empty())
{
point s=que.top(); que.pop();
// printf("%d %d %d %d\n",s.z,s.x,s.y,s.step);
if(s.x==e.x&&s.y==e.y&&s.z==e.z) {printf("%d %d\n",n,s.step);return;}
for(int i=;i<;i++)
{
t.x=s.x+dir[i][],t.y=s.y+dir[i][],t.z=s.z+dir[i][];
if(t.x>=&&t.x<n&&t.y>=&&t.y<n&&t.z>=&&t.z<n&&maze[t.z][t.x][t.y]!='X'&&!used[t.z][t.x][t.y])
{
t.step=s.step+;
used[t.z][t.x][t.y]=;
que.push(t);
}
}
}
printf("NO ROUTE\n");
}
int main()
{
// freopen("a.txt","r",stdin);
char s[];
while(~scanf("%s",s))
{
scanf("%d",&n);
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%s",maze[i][j]);
scanf("%d%d%d",&t.y,&t.x,&t.z);
t.step=;
scanf("%d%d%d",&e.y,&e.x,&e.z);
scanf("%s",s);
bfs();
}
return ;
}
http://acm.hdu.edu.cn/showproblem.php?pid=1253
这题用优先队列是超时的,普通队列可以过。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
int x,y,z,time;
}s,e;
int maze[][][];
int dir[][]= {{-,,},{,,},{,-,},{,,},{,,-},{,,}}; int main()
{
//freopen("a.txt","r",stdin);
int d,flag;
int a,b,c,t;
queue<point>que;
scanf("%d",&d);
while(d--)
{
scanf("%d%d%d%d",&a,&b,&c,&t);
for(int i=; i<a; i++)
for(int j=; j<b; j++)
for(int k=; k<c; k++)
scanf("%d",&maze[i][j][k]);
while(!que.empty()) que.pop();
s.x=,s.y=,s.z=,s.time=;
flag=;
que.push(s);
maze[s.z][s.x][s.y]=;
while(!que.empty())
{
e=que.front();
que.pop();
//printf("%d %d %d %d\n",e.z,e.x,e.y,e.time);
if(e.z==a-&&e.x==b-&&e.y==c-&&e.time<=t)
{
printf("%d\n",e.time);
flag=;
break;
}
for(int i=; i<; i++)
{
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
s.z=e.z+dir[i][];
if(s.z>=&&s.z<a&&s.x>=&&s.x<b&&s.y>=&&s.y<c&&maze[s.z][s.x][s.y]!=)
{
s.time=e.time+;
if(s.time<=t)
{
maze[s.z][s.x][s.y]=;
que.push(s);
}
}
}
}
if(!flag) printf("-1\n");
}
return ;
}
hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)的更多相关文章
- HDU 1253 胜利大逃亡(BFS)
题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 1253 胜利大逃亡 NYOJ 523【BFS】
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 1253 胜利大逃亡 题解
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- [ACM] hdu 1253 胜利大逃亡 (三维BFS)
胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这但是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,能够被表示 ...
- hdu 1253 胜利大逃亡 (代码详解)解题报告
胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示 ...
- hdoj 1253 胜利大逃亡
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDOJ1253 胜利大逃亡 BFS
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
随机推荐
- Application, JDBC, 数据库连接池, Session, 数据库的关系
RT,这几个东东已经困扰我很长一段时间了... 这次争取把她们理清楚了! 参考资料: 1. 数据库连接池:http://www.cnblogs.com/shipengzhi/archive/2011/ ...
- 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...
- add some template for ec-final
二维rmq 离线 init O( n*n*logn*logn ) query O(1) http://www.cnblogs.com/kuangbin/p/3227420.html 求1-n有多少个 ...
- Linux配置防火墙,开启80端口、3306端口(转)
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...
- C++中static的全部作用
要理解static,就必须要先理解另一个与之相对的关键字,很多人可能都还不知道有这个关键字,那就是auto,其实我们通常声明的不用static修饰的变量,都是auto的,因为它是默认的,就象short ...
- 7 天玩转 ASP.NET MVC — 第 7 天
目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 今天是开心的一天.因为我们终于来到了系列学习的最后一节.我相信你喜欢之前的课程,并从中学到了许多. ...
- 用VBS将PPT转为图片
'使用方法:把ppt文件拖放到该文件上. '机器上要安装Powerpoint程序 On Error Resume Next Set ArgObj = WScript.Arguments pptfile ...
- 线性时间常数空间找到数组中数目超过n/5的所有元素
问题描述: Design an algorithm that, given a list of n elements in an array, finds all the elements that ...
- LA 3350
The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM ...
- springmvc web应用程序 java
搭建普通 springmvc 1.如图建立相关文件 建立在WEB-INF下比较安全,不能直接访问资源. 2.建立Controller控制器,如图 3.需要导入的jar包 commons-logging ...