OpenJudge 2251:Dungeon Master(BFS)

2251:Dungeon Master
总时间限制: 1000ms 内存限制: 65536kB
描述

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!

样例输入
在这里插入图片描述

样例输出

Escaped in 11 minute(s).
Trapped!

来源

Ulm Local 1997

思路

最短的时间
每个位置向周围移动花费的时间为一个单位时间
可以看做是边权为1的图

BFS(Breadth-First Search)算法的具体实现就是:通过不断取得某个状态能够达到的所有状态并将其加入队列尾, 并且由于队列本身的特性先加入队列的状态总是先得到处理,这样就可以总是先将需要转移次数更少的状态进行处理。换句话说就是总是取得了这个状态的树中更接近根部的节点,又或者是总是让搜索树的广度得到尽可能增加

地图可以使用string类进行储存

string str[35][35];

节点信息

struct Node {
	int x, y, z;
	int step;
};

每次位置的移动可以使用数组打表进行

int dx[] = {0, 0, 0, 0, 1, -1};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {1, -1, 0, 0, 0, 0};

完整代码

#include<bits/stdc++.h>
using namespace std;
int L, R, C;
int sx, sy, sz, ex, ey, ez;
bool vis[35][35][35];
string str[35][35];
struct Node {
	int x, y, z;
	int step;
};
int dx[] = {0, 0, 0, 0, 1, -1};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {1, -1, 0, 0, 0, 0};
int BFS() {
	if(sx==ex && sy==ey && sz==ez)
		return 0;
	memset(vis, false, sizeof(vis));/*初始化*/
	queue<Node> que;
	que.push({sx, sy, sz, 0});
	vis[sx][sy][sz] = true;/*标记起点来过*/
	while (!que.empty()) {
		Node fr = que.front();
		int tx = fr.x;
		int ty = fr.y;
		int tz = fr.z;
		if (tx == ex && ty == ey && tz == ez) {
			return fr.step;
		}
		for (int i = 0; i < 6; ++i) {
			int xx = tx + dx[i];
			int yy = ty + dy[i];
			int zz = tz + dz[i];
			if (xx < 0 || xx >= L || yy < 0 || yy >= R || zz < 0 || zz >= C)/*越界*/
				continue;
			if (vis[xx][yy][zz])/*来过*/
				continue;
			if (str[xx][yy][zz] == '#')/*岩石*/
				continue;
			vis[xx][yy][zz] = true;
			que.push({xx, yy, zz, fr.step + 1});
		}
		que.pop();
	}
	return -1;
}
int main() {

	while (cin >> L >> R >> C) {
		if (L == 0 && R == 0 && C == 0)/*输入结束*/
			break;
		for (int i = 0; i < L; ++i) {
			for (int j = 0; j < R; ++j) {
				cin >> str[i][j];/*使用cin读入 会自动跳过前面的空白*/
			}
		}
		sx = sy = sz = -1;/*初始化*/
		ex = ey = ez = -2;
		for (int i = 0; i < L; ++i) {
			for (int j = 0; j < R; ++j) {
				for (int k = 0; k < C; ++k) {
					if (str[i][j][k] == 'S') {/*获取起点坐标*/
						sx = i;
						sy = j;
						sz = k;
					}
					if (str[i][j][k] == 'E') {/*获取终点坐标*/
						ex = i;
						ey = j;
						ez = k;
					}
				}
			}
		}
		int Time = -1;
		Time = BFS();
		if(Time!=-1)    cout<<"Escaped in "<<Time<<" minute(s)."<<endl;
		else           cout<<"Trapped!"<<endl;

	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值