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地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
随机推荐
- 【jQuery】总结:筛选器、控制隐藏、操作元素style属性
筛选器 -> http://blog.csdn.net/lijinwei112/article/details/6938134 常用到的: $("tr[id=ac_"+id+ ...
- System.arraycopy方法
数组的复制有多种方法,其中有一种就是System.arraycopy方法,传闻速度也很快. 方法完整签名: public static void arraycopy(Object src, int s ...
- 【转】Github轻松上手1-Git的工作原理与设置
转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzgp.html 作为一个程序猿,如果没有接触过stack overflow和Github,就如同在江湖中 ...
- 【原】cocos2d-x 2.0.4 不支持https协议 CURLE_UNSUPPORTED_PROTOCOL
我们项目组用的cocos2d-x版本还比较老,各种好的功能不能用. 今天就让我遇到一个问题,使用CCHttpClient发送http请求的时候,https协议的不支持,返回失败信息如下 errorco ...
- [Everyday Mathematics]20150109
设 $A$ 是 $n$ 阶复方阵, 其特征多项式为 $$\bex f(\lm)=(\lm-\lm_1)^{n_1}\cdots(x-\lm_s)^{n_s}, \eex$$ 其中 $\lm_i$ 互不 ...
- HDU 4267-A Simple Problem with Integers(多个BIT)
题意: 2种操作 1 a b k c 在区间[a,b]中的(i-a)%k==0的位置i上的数+c 2 a 查询位置a的值 输出每次查询的值 分析: 开始想到多维的线段树,但比较麻烦,看了题解才知道,用 ...
- IOS PUSH 实践操作~~~~
1.推送过程简介 (1)App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远 ...
- 如何从ST网站找到对应的固件库
ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ...
- qt 获取天气的接口
博客来源:http://blog.csdn.net/lzqwebsoft/article/details/7054045 网站api接口:http://smart.weather.com.cn/wzf ...
- 3D 矩阵旋转
如图,需要将点(向量) v(x, y, 0) 绕 z 轴旋转角度 θ,求旋转后的点(向量) v'(x', y', 0). 大概思路: 1. 将 v(x, y, 0) 分解, v(x, y, 0) = ...