POJ2251(BFS)
Dungeon Master
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
题意
你被困在一个3D地牢,需要找到最快的出路!地牢是由单位立方体组成,可以或不可以用岩石填充。将一个单位向北,南,东,西,上或下移动需要一分钟。你不能对角地移动,迷宫被四周的坚实岩石包围。
是逃生可能吗?如果是,需要多长时间?
思路
用BFS是搜索迷宫的三维版本。
代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DungeonMaster {
int l;//层数
int r;//每一层的行
int c;//每一层的列
char[][][] map;
int[][][] vis;
int[][] dir = {{0, 0, -1}, {0, 0, 1}, {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}};//搜索的6个方位
List<Node> queue = new ArrayList<Node>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DungeonMaster dungeonMaster;
while (sc.hasNext()) {
dungeonMaster = new DungeonMaster();
dungeonMaster.l = sc.nextInt();//层数
dungeonMaster.r = sc.nextInt();//每一层的行
dungeonMaster.c = sc.nextInt();//每一层的列
int startL = 0;
int startR = 0;
int startC = 0;
int endL = 0;
int endR = 0;
int endC = 0;
if (dungeonMaster.l == 0 && dungeonMaster.r == 0 && dungeonMaster.c == 0) {
break;
}
dungeonMaster.map = new char[dungeonMaster.l][dungeonMaster.r][dungeonMaster.c];
dungeonMaster.vis = new int[dungeonMaster.l][dungeonMaster.r][dungeonMaster.c];
for (int i = 0; i < dungeonMaster.l; i++) {//层数
for (int j = 0; j < dungeonMaster.r; j++) {//每一层的行
String str = sc.next();
for (int k = 0; k < dungeonMaster.c; k++) {//每一层的列
dungeonMaster.map[i][j][k] = str.charAt(k);
dungeonMaster.vis[i][j][k] = 0;
if (str.charAt(k) == 'S') {
startL = i;
startR = j;
startC = k;
}
if (str.charAt(k) == 'E') {
endL = i;
endR = j;
endC = k;
}
}
}
}
int result = dungeonMaster.bfs(startL, startR, startC, endL, endR, endC);
if (result != -1) {
System.out.println("Escaped in " + result + " minute(s).");
} else {
System.out.println("Trapped!");
}
}
}
public int bfs(int startL, int startR, int startC, int endL, int endR, int endC) {
Node start_node = new Node();
start_node.setL(startL);
start_node.setR(startR);
start_node.setC(startC);
start_node.setDis(0);
vis[startL][startR][startC] = 1;
queue.add(start_node);
int front = 0;
int rear = 1;
while (front < rear) {
Node now_node = queue.get(front);
front++;
for (int i = 0; i < 6; i++) {
Node next = new Node();
next.setL(now_node.getL() + dir[i][0]);
next.setR(now_node.getR() + dir[i][1]);
next.setC(now_node.getC() + dir[i][2]);
next.setDis(now_node.getDis() + 1);
if (next.getR() >= 0 && next.getL() >= 0 && next.getC() >= 0 && next.getC() < c && next.getL() < l && next.getR() < r//在迷宫内
&& map[next.getL()][next.getR()][next.getC()] != '#' && vis[next.getL()][next.getR()][next.getC()] == 0) {//还没有访问过且不是墙壁
if (map[next.getL()][next.getR()][next.getC()] == 'E') {
return next.getDis();
}
queue.add(next);
rear++;
}
}
}
return -1;
}
static class Node {
private int l;
private int r;
private int c;
private int dis;
public void setDis(int dis) {
this.dis = dis;
}
public int getDis() {
return dis;
}
public int getR() {
return r;
}
public int getL() {
return l;
}
public int getC() {
return c;
}
public void setL(int l) {
this.l = l;
}
public void setR(int r) {
this.r = r;
}
public void setC(int c) {
this.c = c;
}
}
}
Java代码超出内存限制。。。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int m, n, h;
const int maxn=33;
char s[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int dis[maxn][maxn][maxn];
struct node{
int x, y, z;
node(int xx, int yy, int zz)
{
x=xx;
y=yy;
z=zz;
}
};
int cm[6]={1,0,-1,0,0,0};
int cn[6]={0,1,0,-1,0,0};
int ch[6]={0,0,0,0,1,-1};
node S=node(0, 0, 0), E=node(0, 0, 0);
int bfs()
{
queue<node>que;
que.push(S);
vis[S.x][S.y][S.z]=1;
while(!que.empty())
{
node no=que.front();
que.pop();
int x=no.x, y=no.y, z=no.z;
for(int i=0; i<6; i++)
{
int nx=x+cm[i], ny=y+cn[i], nz=z+ch[i];
if(nx==E.x&&ny==E.y&&nz==E.z)
{
return dis[x][y][z]+1;
}
if(nx>=0&&nx<m&&ny>=0&&ny<n&&nz>=0&&nz<h&&s[nx][ny][nz]=='.'&&!vis[nx][ny][nz])
{
vis[nx][ny][nz]=1;
que.push(node(nx, ny, nz));
dis[nx][ny][nz]=dis[x][y][z]+1;
}
}
}
return -1;
}
int main(void)
{
while(scanf("%d%d%d", &m, &n, &h)&&m&&n&&h)
{
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
scanf("%s", s[i][j]);
}
}
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
for(int k=0; k<h; k++)
{
if(s[i][j][k]=='S')
S=node(i, j, k);
if(s[i][j][k]=='E')
E=node(i, j, k);
}
}
}
int res=bfs();
if(res==-1)printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", res);
}
return 0;
}
613

被折叠的 条评论
为什么被折叠?



