简单的BFS, 3D迷宫

题意如下

描述
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!
来源 很简单的题意,3D迷宫,给你起始点,和终止点 ,让你求最短多长时间可以走到地方,如果走不到,输出Trapped 写了好久,这个题的坑在于,如何合理地读入数据,其实会发现。scanf读入数据会更快,然后就是状态节点的判定,一定要一致,即层数,行数,列数的顺序要一致,还有其实读入的时候,处理空行的情况也比较特殊,这里不用考虑?? 代码如下
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<memory.h>
#include<queue>
#include<string>
using namespace std;
int l, r, c, sx, sy,sz,ex,ey,ez;
char map1[32][32][32];
int bo[32][32][32];
char ch;
int dz[6] = { 0,0,0,0,1,-1 };
int dx[6] = { 0,0,1,-1,0,0 };
int dy[6] = { 1,-1,0,0,0,0 };
struct node
{
	int x, y, z, step;
	node(int a, int b, int c, int d)
	{
		x = a; y = b; z = c; step = d;
	}
};
bool check(int xx, int yy, int zz)
{
	if ((xx < 0) || (xx >= r) || (yy < 0) || (yy >= c) || (zz < 0) || (zz >= l)) return false;
	if (bo[zz][xx][yy] == false) return false;
	if (map1[zz][xx][yy] == '#') return false;
	return true;
}
void bfs1()
{
	memset(bo, true, sizeof(bo));
	queue<node> q;
	int x1, y1, z1;
	while (!q.empty()) q.pop();
	q.push(node(sx, sy, sz, 0));
	bo[sz][sx][sy] = false;
	bool flag = true;
	while (!q.empty())
	{
		node a = q.front();
		q.pop();
		if ((a.x == ex) && (a.y == ey) && (a.z == ez))
		{
			printf("Escaped in %d minute(s).\n", a.step);
			flag = false;
			break;
		}
		for (int i = 0; i < 6; i++)
		{
			x1 = a.x + dx[i];
			y1 = a.y + dy[i];
			z1 = a.z + dz[i];
			if (!check(x1, y1, z1)) continue;
			
		//	cout << "i will be here" << endl;
			q.push(node(x1, y1, z1, a.step+1));
			bo[z1][x1][y1] = false;
		}
	}
	if (flag == true) printf("Trapped!\n");
}

int main()
{
	while (1)
	{
		scanf("%d %d %d", &l, &r, &c);
		if ((l + r + c) == 0) break;
		for (int ll = 0; ll < l; ll++)
		{
			for (int rr = 0; rr < r; rr++)
			{
				scanf("%s", map1[ll][rr]);
				for (int cc = 0; cc < c; cc++)
				{
					if (map1[ll][rr][cc] == 'S')
					{
						sx = rr; sy = cc; sz = ll;
						continue;
					}
					if (map1[ll][rr][cc] == 'E') 
					{
						ex = rr; ey = cc; ez = ll;
						continue;
					}
				}
			}
		}
		//for (int ll = 0; ll < l; ll++)
		//	for (int rr = 0; rr < r; rr++)
		//		printf("%s", map1[ll][rr]);
		//printf("sx:%d sy:%d sz:%d\n", sx, sy, sz);
		//printf("ex:%d ey:%d ez:%d\n", ex, ey, ez);

		bfs1();
	}
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值