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. Posix线程编程指南(1) 线程创建与取消

    线程创建 1.1 线程与进程 相对进程而言,线程是一个更加接近于执行体的概念,它可以与同进程中的其他线程共享数据,但拥有自己的栈空间,拥有独立的执行序列.在串行程序基础上引入线程和进程是为了提高程序的 ...

  2. idea maven添加jar包

    在“项目结构“里设置 选择libaray,添加jar包

  3. Brush Mode --- Nyoj 236 分类: Brush Mode 2014-04-02 06:56 116人阅读 评论(0) 收藏

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间 ...

  4. JavaScript 文件上传类型判断

    文件上传时用到一个功能,使用html元素的input标签实现, <input id="imageFile" name="imageFile1" accep ...

  5. mybatis insert 如何返回主键

    在使用ibatis插入数据进数据库的时候,会用到一些sequence的数据,有些情况下,在插入完成之后还需要将sequence的值返回,然后才能进行下一步的操作.       使用ibatis的sel ...

  6. Unity3D研究院之LZMA压缩文件与解压文件

    原地址:http://www.xuanyusong.com/archives/3095 前两天有朋友告诉我Unity的Assetbundle是LZMA压缩的,刚好今天有时间那么就研究研究LZMA.它是 ...

  7. POJ 2041

    #include <iostream> #include <string> #include <algorithm> using namespace std; st ...

  8. BZOJ 1015: [JSOI2008]星球大战starwar 并查集

    1015: [JSOI2008]星球大战starwar Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝 ...

  9. java如何追加写入txt文件

    java中,对文件进行追加内容操作的三种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.BufferedWriter; import  ...

  10. JMeter监控服务器CPU, 内存,网络数据

    http://wenku.baidu.com/link?url=un5QtWHa-A9kCTeVN0PnU3gDEMri38hYqjc8-skNXTD-v65FMObdq1LxfQDb1I6oIK9k ...