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)的更多相关文章

  1. HDU 1253 胜利大逃亡(BFS)

    题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A ...

  2. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. HDU 1253 胜利大逃亡 NYOJ 523【BFS】

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  4. HDU 1253 胜利大逃亡 题解

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. [ACM] hdu 1253 胜利大逃亡 (三维BFS)

    胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这但是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,能够被表示 ...

  7. hdu 1253 胜利大逃亡 (代码详解)解题报告

    胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示 ...

  8. hdoj 1253 胜利大逃亡

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. HDOJ1253 胜利大逃亡 BFS

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

随机推荐

  1. 快速幂取模 分类: ACM TYPE 2014-08-29 22:01 95人阅读 评论(0) 收藏

    #include<stdio.h> #include<stdlib.h> //快速幂算法,数论二分 long long powermod(int a,int b, int c) ...

  2. UML类图(转载)

    概述: 类图是静态图.它代表了一个应用程序的静态视图.类图不仅用于可视化描述和记录系统的不同方面,但也为构建可执行代码的软件应用程序. 类图描述一类的属性和操作,也对系统的约束.被广泛应用于类图的建模 ...

  3. 用fabric部署维护kle日志收集系统

    最近搞了一个logstash kafka elasticsearch kibana 整合部署的日志收集系统.部署参考lagstash + elasticsearch + kibana 3 + kafk ...

  4. 正确使用HTML title属性

    如果你想对使用手机,平板电脑和辅助技术的用户隐藏某些内容,而只对键盘用户显示,那么请使用title属性. 细节 HTML的title属性本身有问题.之所以有问题是因为它在一些重要的方面表现的不够好,尽 ...

  5. MySQL查看表占用空间大小(转)

    MySQL查看表占用空间大小(转) //先进去MySQL自带管理库:information_schema //自己的数据库:dbwww58com_kuchecarlib //自己的表:t_carmod ...

  6. yum install 与 yum groupinstall 的区别

    原文:http://blog.51yip.com/linux/1171.html yum 提供二种安装软件的方式 1,yum install 它安装单个软件,以及这个软件的依赖关系 2,yum gro ...

  7. Ruby中的语句中断和返回

    李哲 - APRIL 28, 2015 return,break,next 这几个关键字的使用都涉及到跳出作用域的问题,而他们的不同 则在于不同的关键字跳出去的目的作用域的不同,因为有代码块则导致有一 ...

  8. iOS16进制设置颜色

    UIColor+Hex.h // // UIColor+Hex.h // 16进制颜色类别 // // Created by apple on 15-4-3. // Copyright (c) 201 ...

  9. springmvc web应用程序 java

    搭建普通 springmvc 1.如图建立相关文件 建立在WEB-INF下比较安全,不能直接访问资源. 2.建立Controller控制器,如图 3.需要导入的jar包 commons-logging ...

  10. ExtJs之进度条实现

    慢慢按书上的作. <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta h ...