UVa532 Dungeon Master 三维迷宫
学习点:
scanf可以自动过滤空行
搜索时要先判断是否越界(L R C),再判断其他条件是否满足
bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<queue>
using namespace std;
const int N=31;
const int dl[]={-1, 1, 0, 0, 0, 0};
const int dr[]={0, 0, -1, 1, 0, 0};
const int dc[]={0, 0, 0, 0, -1, 1}; struct State {
State(int l, int r, int c, int d): l(l), r(r), c(c), d(d) {}
int l, r, c;
int d;
}; int L, R, C;
int SL, SR, SC;
int EL, ER, EC;
typedef char A[N][N][N];
A dungeon, vis; int bfs() {
if(SL==EL && SR==EL && SC==EC) return 0;
State s(SL, SR, SC, 0); vis[SL][SR][SC]=1;
queue<State> q;
q.push(s);
while(!q.empty()) {
State t=q.front(); q.pop();
//可以在出口判断
//if(t.l==EL && t.r==ER && t.c==EC) return t.d;
for(int i=0;i<6;i++) {
int nl=t.l+dl[i];
int nr=t.r+dr[i];
int nc=t.c+dc[i];
if(nl>=0 && nl<L && nr>=0 && nr<R && nc>=0 && nc<C && !vis[nl][nr][nc] && dungeon[nl][nr][nc]!='#') {
//也可以在入口判断
if(nl==EL && nr==ER && nc==EC) return t.d+1;
q.push(State(nl, nr, nc, t.d+1));
vis[nl][nr][nc]=1;
}
}
}
return -1;
} int main() {
#ifndef ONLINE_JUDGE
freopen("./uva532.in", "r", stdin);
#endif
while(scanf("%d%d%d", &L, &R, &C)==3 && L && R && C) {
for(int i=0;i<L;i++) {
for(int j=0;j<R;j++) {
scanf("%s", dungeon[i][j]);
for(int k=0;k<C;k++)
if(dungeon[i][j][k]=='S') SL=i, SR=j, SC=k;
else if(dungeon[i][j][k]=='E') EL=i, ER=j, EC=k;
}
}
memset(vis, 0, sizeof vis);
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
// UVa532 Dungeon Master
// Rujia Liu
// 题意:三维迷宫中给定起点(字符S)和终点(字符E),墙是'#',空格是'.',求最短路长度
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; struct State {
int l, r, c;
State(int l, int r, int c):l(l),r(r),c(c) {}
}; const int maxn = 30 + 5;
const int dl[] = {1,-1,0,0,0,0};
const int dr[] = {0,0,1,-1,0,0};
const int dc[] = {0,0,0,0,1,-1};
int L, R, C, d[maxn][maxn][maxn], vis[maxn][maxn][maxn];
char maze[maxn][maxn][maxn]; int bfs(int l1, int r1, int c1) {
queue<State> Q;
d[l1][r1][c1] = 0;
vis[l1][r1][c1] = 1;
Q.push(State(l1, r1, c1));
while(!Q.empty()) {
State s = Q.front(); Q.pop();
for(int i = 0; i < 6; i++) {
int newl = s.l + dl[i];
int newr = s.r + dr[i];
int newc = s.c + dc[i];
if(newl >= 0 && newl < L && newr >= 0 && newr < R && newc >= 0 && newc < C && maze[newl][newr][newc] != '#' && !vis[newl][newr][newc]) {
Q.push(State(newl, newr, newc));
vis[newl][newr][newc] = 1;
d[newl][newr][newc] = d[s.l][s.r][s.c] + 1;
if(maze[newl][newr][newc] == 'E') return d[newl][newr][newc];
}
}
}
return -1;
} int main() {
while(scanf("%d%d%d", &L, &R, &C) == 3 && L) {
int l1, r1, c1;
for(int i = 0; i < L; i++)
for(int j = 0; j < R; j++) {
scanf("%s", maze[i][j]);
for(int k = 0; k < C; k++)
if(maze[i][j][k] == 'S') { l1 = i; r1 = j; c1 = k; }
}
memset(vis, 0, sizeof(vis));
int ans = bfs(l1, r1, c1);
if(ans >= 0) printf("Escaped in %d minute(s).\n", ans);
else printf("Trapped!\n");
}
return 0;
}
UVa532 Dungeon Master 三维迷宫的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】
若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ 2252 Dungeon Master 三维水bfs
题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
随机推荐
- Android中利用画图类和线程画出闪烁的心形
本文讲解主要涉及的知识点: 1.线程控制 2.画图类 3.心形函数 大家先看图片: <ig ...
- codeforces 680E Bear and Square Grid 巧妙暴力
这个题是个想法题 先预处理连通块,然后需要用到一种巧妙暴力,即0变1,1变0,一列列添加删除 复杂度O(n^3) #include <cstdio> #include <iostre ...
- Cadence原理图与Allegro交互
1:激活orCAD与Allegro的交互程序 打开原理图,Options->Preference在Miscellaneous里勾选 2:打开用到的工程 原理图,还有Allegro PCB Des ...
- DOM笔记(三):Element接口和HTMLElement接口
一.Element接口 Element接口表示一个元素,该接口扩展自Node接口,自然继承了Node接口的属性和方法,也有一套针对元素的属性和方法. Element接口常见的属性比较少,常用的就是一个 ...
- JavaScript相关图书推荐
JavaScript语言精粹(修订版) 作 者 Douglas Crockford(道格拉斯·克罗克福德) 著:赵泽欣 等 译 出 版 社 电子工业出版社 出版时间 2012-09-01 版 ...
- Android应用性能优化笔记(java代码优化)
Java代码优化 缓存结果: 如果计算代价过高,最好把过去的结果缓存起来. 伪代码如下: result=cache.get(n); //输入参数n作为键 if(result==nul ...
- jar,war,ear区别及java基础杂七八
jar,war,earqu区别 这三种文件都可以看作是java的压缩格式,其实质是实现了不同的封装: jar--封装类war--封装web站点ear--封装ejb.它们的关系具体为:jar: ...
- 最全面的 MySQL 索引详解
什么是索引? 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 2.索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构 ...
- 第二百八十五天 how can I 坚持
今天好平凡啊. 晚上给徐斌打电话说忘带钥匙了,一块吃了个饭. 回到家,什么都不想做,好消沉. 玩了几局象棋,很多东西只是玩玩,但还是会认真,认真就会输,好惨. 最近在关注万科幸福里,可是.首付付不起啊 ...
- 第二百五十三、四、五天 how can I 坚持
出去玩了几天,好累啊. 周五,坐了半天车.到了西柏坡,下午撕名牌,好疯狂啊,最终还是以有人受伤为代价结束了战斗.晚上吃蛋糕.水饺,还有面条,就是我的奖品没拿到.哎.. 周六,上午滑雪,两年没滑了,都忘 ...