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!

一个人,从S到E,‘.'是可以走的,'#'是不可以走的,求S到E的最短路。最短路一定要用bfs!!!用dfs就挂了……因为dfs无法便捷的表示是否访问过这个点,导致回溯过程中不断搜索回去的路,十分费时!然后这个三维的搜索过程和2维没什么区别,就是变成了6个方向搜索,然后通过队列实现。初始化有问题,wa两发,而且这是我到现在写过的最最最最!!最脑残的代码,明天重写,怎么会写的这么糟糕,居然用类似hash的方法来标记数组,卧槽,直接三维不就好了,傻屌。写了3000多字节。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int l, r, c, x0, yo, x2, y2, z0, z2;
char a[33][33][33];
struct Node{
	int x;
	int y;
	int z;
}node[40000];
int leap[40000], dp[40000];
queue<Node>p;
int main()
{
	int i, j, m, n, ans, k;
	Node temp;
	while (scanf("%d%d%d", &l, &r, &c) != EOF)
	{
		getchar();
		if (l == 0 && r == 0 && c == 0)break;
		memset(leap, 0, sizeof(leap));
		for (i = 0; i <= 39999; i++)
			dp[i] = 40000;
		for (i = 1; i <= l; i++)
		for (j = 1; j <= r; j++)
		for (k = 1; k <= c; k++)
		{
			node[i * 961 + j * 31 + k].x = i;
			node[i * 961 + j * 31 + k].y = j;
			node[i * 961 + j * 31 + k].z = k;
		}
		for (i = 1; i <= l; i++)
		{
			for (j = 1; j <= r; j++)
			{
				for (k = 1; k <= c; k++)
				{
					cin >> a[i][j][k];
					if (a[i][j][k] == 'S')
					{
						x0 = i; yo = j; z0 = k;
					}
					if (a[i][j][k] == 'E')
					{
						x2 = i; y2 = j; z2 = k;
					}
				}
				getchar();
			}
			getchar();
		}

		p.push(node[x0 * 961 + yo * 31 + z0]);
		leap[x0 * 961 + yo * 31 + z0] = 1;
		dp[x0 * 961 + yo * 31 + z0] = 0;
		while (p.size())
		{
			temp = p.front();
			if (temp.x + 1 <= l&&!leap[(temp.x + 1) * 961 + temp.y * 31 + temp.z] && a[temp.x + 1][temp.y][temp.z] != '#')
			{
				leap[(temp.x + 1) * 961 + temp.y * 31 + temp.z] = 1;
				dp[(temp.x + 1) * 961 + temp.y * 31 + temp.z] = dp[temp.x * 961 + temp.y * 31 + temp.z] + 1;
				p.push(node[(temp.x + 1) * 961 + temp.y * 31 + temp.z]);
			}
			if (temp.x - 1 >= 1 && !leap[(temp.x - 1) * 961 + temp.y * 31 + temp.z] && a[temp.x - 1][temp.y][temp.z] != '#')
			{
				leap[(temp.x - 1) * 961 + temp.y * 31 + temp.z] = 1;
				dp[(temp.x - 1) * 961 + temp.y * 31 + temp.z] = dp[temp.x * 961 + temp.y * 31 + temp.z] + 1;
				p.push(node[(temp.x - 1) * 961 + temp.y * 31+ temp.z]);
			}
			if (temp.y + 1 <= r&&!leap[temp.x * 961 + (temp.y + 1) * 31 + temp.z] && a[temp.x][temp.y + 1][temp.z] != '#')
			{
				leap[temp.x * 961 + (temp.y + 1) * 31 + temp.z] = 1;
				dp[temp.x * 961 + (temp.y + 1) * 31 + temp.z] = dp[temp.x * 961 + temp.y * 31 + temp.z] + 1;
				p.push(node[temp.x * 961 + (temp.y + 1) * 31 + temp.z]);
			}
			if (temp.y - 1 >= 1 && !leap[temp.x * 961 + (temp.y - 1) * 31 + temp.z] && a[temp.x][temp.y - 1][temp.z] != '#')
			{
				leap[temp.x * 961 + (temp.y - 1) * 31 + temp.z] = 1;
				dp[temp.x * 961 + (temp.y - 1) * 31 + temp.z] = dp[temp.x * 961 + temp.y * 31 + temp.z] + 1;
				p.push(node[temp.x * 961 + (temp.y - 1) * 31 + temp.z]);
			}
			if (temp.z + 1 <= c&&!leap[temp.x * 961 + temp.y * 31 + temp.z + 1] && a[temp.x][temp.y][temp.z + 1] != '#')
			{
				leap[temp.x * 961 + temp.y * 31 + temp.z + 1] = 1;
				dp[temp.x * 961 + temp.y * 31 + temp.z + 1] = dp[temp.x * 961 + temp.y * 31 + temp.z] + 1;
				p.push(node[temp.x * 961 + temp.y * 31 + temp.z + 1]);
			}
			if (temp.z - 1 >= 1 && !leap[temp.x * 961 + temp.y * 31 + temp.z - 1] && a[temp.x][temp.y][temp.z - 1] != '#')
			{
				leap[temp.x * 961 + temp.y * 31 + temp.z - 1] = 1;
				dp[temp.x * 961 + temp.y * 31 + temp.z - 1] = dp[temp.x * 961 + temp.y * 31 + temp.z] + 1;
				p.push(node[temp.x * 961 + temp.y * 31 + temp.z - 1]);
			}
			p.pop();
		}
		if (dp[x2 * 961 + y2 * 31 + z2] != 40000)
			cout << "Escaped in "<<dp[x2 * 961 + y2 * 31 + z2] <<" minute(s)."<< endl;
		else
			cout << "Trapped!" << endl;
	}
	
	return 0;
}

修改后的代码,比原来看起来好看多了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int l, r, c, x0, yo, x2, y2, z0, z2;
char a[33][33][33];
struct Node{
	int x;
	int y;
	int z;
	int ans;
}node[40000];
int leap[33][33][33];
int dir[6][3] = { 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1 };
queue<Node>p;
int main()
{
	int i, j, m, n, ans, k, tempx, tempy, tempz;
	Node temp;
	while (scanf("%d%d%d", &l, &r, &c) != EOF)
	{
		getchar();
		ans = 0;
		if (l == 0 && r == 0 && c == 0)break;
		for (i = 0; i <= 32;i++)
		for (j = 0; j <= 32;j++)
		for (k = 0; k <= 32; k++)
		{
			leap[i][j][k] = 0; 
		}
		for (i = 1; i <= l; i++)
		{
			for (j = 1; j <= r; j++)
			{
				for (k = 1; k <= c; k++)
				{
					cin >> a[i][j][k];
					if (a[i][j][k] == 'S')
					{
						x0 = i; yo = j; z0 = k;
					}
					if (a[i][j][k] == 'E')
					{
						x2 = i; y2 = j; z2 = k;
					}
				}
				getchar();
			}
			getchar();
		}
		temp.x = x0; temp.y = yo; temp.z = z0; temp.ans = 0;
		p.push(temp);
		leap[x0][yo][z0] = 1;
		while (p.size())
		{
			Node now = p.front();
			for (i = 0; i < 6; i++)
			{
				temp.x = now.x + dir[i][0];
				temp.y = now.y + dir[i][1];
				temp.z = now.z + dir[i][2];
				if (temp.x >= 1 && temp.y >= 1 && temp.z >= 1 & temp.x <= l&&temp.y <= r&&temp.z <= c&&!leap[temp.x][temp.y][temp.z] && a[temp.x][temp.y][temp.z] != '#')
				{
					leap[temp.x][temp.y][temp.z] = 1;
					temp.ans = now.ans + 1;
					p.push(temp);
					if (temp.x == x2&&temp.y == y2&&temp.z == z2)
						ans = temp.ans;
				}
			}
			p.pop();
		}
		if (ans)
			cout << "Escaped in " << ans << " minute(s)." << endl;
		else
			cout << "Trapped!" << endl;
	}
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值