学习点:

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 三维迷宫的更多相关文章

  1. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  2. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】

    若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...

  4. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  5. ZOJ 1940 Dungeon Master 三维BFS

    Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  6. Dungeon Master(三维bfs)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  7. POJ 2252 Dungeon Master 三维水bfs

    题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...

  8. POJ 2251 Dungeon Master (非三维bfs)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 55224   Accepted: 20493 ...

  9. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

随机推荐

  1. 搭建LAMP测试环境

    LAMP:Linux+Apache+Mysql+Php,组合统称为LAMP,关于其中的独立个体,这里就不多介绍了. 1.首先准备一下软件包,如下: mysql-5.0.22.tar.gz httpd- ...

  2. TCP/IP详解学习笔记(10)-TCP连接的建立与中止

    TCP是一个面向连接的协议,所以在连接双方发送数据之前,都需要首先建立一条连接.这和前面讲到的协议完全不同.前面讲的所有协议都只是发送数据而已,大多数都不关心发送的数据是不是送到,UDP尤其明显,从编 ...

  3. poco网络库分析,教你如何学习使用开源库

    Poco::Net库中有 FTPClient HTML HTTP HTTPClient HTTPServer ICMP Logging Mail Messages NetCore NTP OAuth ...

  4. IOS 弹出式 POPMenuView

    //MenuView.h   // //  MenuView.h //  RockPopMenu // //  Created by zhuang chaoxiao on 14-6-26. //  C ...

  5. 《Python CookBook2》 第四章 Python技巧 对象拷贝 && 通过列表推导构建列表

    (先学第四章) 对象拷贝 任务: Python通常只是使用指向原对象的引用,并不是真正的拷贝. 解决方案: >>> a = [1,2,3] >>> import c ...

  6. web自动化框架之四测试报告的搭建

    现状: 看过前面的文章,楼主用的是python,所以在搭建测试报告这块的时候使用的是unittest+htmlTestRunner:然后发现生成出来的报告,总是有那么不完美的地方,比如想增加图片,比如 ...

  7. 常用SQL语句汇总整理

    1.SQL 插入语句得到自动生成的递增ID 值 insert into Table1(Name,des,num) values (''ltp'',''thisisbest'',10); select ...

  8. DX11&C++

  9. HTML 5:你必须知道的data属性

    原文:All You Need to Know About the HTML5 Data Attribute 译文:你必须知道HTML 5 的Data属性 译者:dwqs HTML 5的Data属性可 ...

  10. kobo boot scripts

    #!/bin/sh pkill nickel eink_enable_autoupdate rm -rf /debian/tmp/* /debian/tmp/.* 2>/dev/null mou ...