NYOJ353 3D dungeon 【BFS】

3D dungeon

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述
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?

输入
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.

输出
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!

样例输入
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!

#include <stdio.h>
#include <string.h>
#include <queue>
using std::queue;
char map[32][32][32], vis[32][32][32];
int a, b, c, X, Y, Z;
const int mov[][3] = {0, 0, 1, 0, 0, -1, 0, 1,
		0, 0, -1, 0, 1, 0, 0, -1, 0, 0};
struct Node{
	int x, y, z, steps;
};
queue<Node> Q;

bool check(Node t){
	if(t.x < 0 || t.y < 0 || t.z < 0) return 0;
	if(t.x >= a || t.y >= b || t.z >= c) return 0;
	if(vis[t.x][t.y][t.z] || map[t.x][t.y][t.z] == '#') return 0;
	return 1;
}

void BFS(){
	while(!Q.empty()) Q.pop();

	Node t, n = {0};
	n.x = X; n.y = Y; n.z = Z;
	vis[X][Y][Z] = 1;
	Q.push(n);

	while(!Q.empty()){
		n = Q.front(); Q.pop();

		for(int i = 0; i < 6; ++i){
			t = n; ++t.steps;
			t.x += mov[i][0];
			t.y += mov[i][1];
			t.z += mov[i][2];

			if(check(t)){
				if(map[t.x][t.y][t.z] == 'E'){
					printf("Escaped in %d minute(s).\n", t.steps);
					return;
				}
				vis[t.x][t.y][t.z] = 1;
				Q.push(t);
			}
		}
	}
	printf("Trapped!\n");
}

int main(){
	while(scanf("%d%d%d", &a, &b, &c), a || b || c){

		memset(vis, 0, sizeof(vis));

		for(int i = 0; i < a; ++i){
			for(int j = 0; j < b; ++j){
				scanf("%s", map[i][j]);
				for(int k = 0; k < c; ++k)
					if(map[i][j][k] == 'S'){
						X = i; Y = j; Z = k;
					}
			}
		}
		BFS();
	}
	return 0;
}

NYOJ353 3D dungeon 【BFS】

时间: 2024-10-30 21:42:21

NYOJ353 3D dungeon 【BFS】的相关文章

NYOJ 353 3D dungeon 【bfs】

题意:给你一个高L长R宽C的图形.每个坐标都能够视为一个方格.你一次能够向上.下.左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动. 问:从S出发能不能到达E,假设能请输出最少的移动次数. 策略:简单的深搜. 注意:由于是求最少的移动次数.所以要从全部能到达的中选出最少的. 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs

【bfs】hdu 1104 Remainder

[bfs]hdu 1104 Remainder 题目链接:hdu 1104 Remainder 很不错的一道搜索题目,但是有几个关键问题要注意. 最短路径,果断bfs+Queue 路径的存储问题,之前只想把每一步的计算结果存储到queue(int)Q中,后来发现路径无法记录,就选择存储节点的方式并用string保存路径,queue(node)Q,开一个临时的节点node p,每进行一次运算就更新它的路径string+'op',最终输出的一定是完整路径!! 但最关键的是取模!!!!! discus