POJH-ダンジョンマスター(BFS)

自分自身に加えて、誰もあなたに力を与えることはできません。
いいえ唾液なく、汗、成功の無い涙。

POJ-ダンジョンマスター

タイトル説明

あなたが最も簡単な方法を見つけるために、3Dダンジョンや必要性に閉じ込められています!ダンジョンは、又は岩で充填してもしなくてもよい、単位立方体から構成されています。それは西、上または下、東、南、一つの単位北を移動するために1分かかります。あなたは斜めに移動することはできませんし、迷路はすべての側面に硬い岩に囲まれています。
脱出は可能ですか?そうならば、どのくらいの時間がかかりますか?

エントリー

入力は、ダンジョンの数から成ります。各ダンジョンの説明は、三の整数L、R及びC(全てのサイズは30に限定されるもの)を含む行から始まります。
Lは、ダンジョンを構成するレベルの数です。
RおよびCは、各レベルの計画を構成する行と列の数です。
次いで、R線をそれぞれ含むCの文字のLブロックが続きます。各文字はダンジョンの一つのセルを説明しています。岩の完全なセルが「#」で示され、空のセルは、で表されます「」。あなたの開始位置は「S」で示され、文字「E」によって終了しています。各レベルの後に1つの空白行があります。入力はL、RとCのための3つのゼロで終了します

輸出

それぞれの迷路は、出力の1行を生成します。それが出口に到達することが可能である場合、フォームの線印刷
のx分(S)中にエスケープします。
Xは、それが脱出するのにかかる最短時間で置換されます。
それは脱出することができない場合、ライン印刷
追い込まれましたの!

サンプル入力

3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0

サンプル出力

Escaped in 11 minute(s).
Trapped!

効果の対象に
これは、「#」行くされていない、 'は、それぞれの層があなたのスライスを与え、三次元の迷路ですS''''E「行くことが可能です
あなたはS Eからの最短経路を見つけるために行く必要があります

アイデア解析
この質問は、実際に簡単なBFSの拡張であるのみ終了位置の開始点を決定する必要があり、その方向は6つの方向に行くように、あなたが行くことができます。その上

特記事項:空の裁判官機能、およびキューを忘れてはいけません

ACまでの時間

ノートには良いところは、それBFSの直接書き込みはありません

終わり

#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll GCD(ll a, ll b) { return a ? GCD(b, a % b) : a; }
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a)
{
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod; n >>= 1;
	}
	return res;
}
#define read read()
struct node {
	int x, y, z, step;
};
node STA, END;
char save[50][50][50];
bool judge[50][50][50];
int z, x, y;
queue<node>q;
node temp;
bool Judge(node a, node b)
{
	if (a.x == b.x && a.y == b.y && a.z == b.z)return 1;
	return 0;
}
void bfs()
{
	while (1)
	{
		if (q.empty())
		{
			puts("Trapped!");
			return;
		}
		temp = q.front();
		q.pop();
		if (Judge(temp, END))
		{
			printf("Escaped in %d minute(s).\n", temp.step);
			return;
		}
		if (!judge[temp.x][temp.y][temp.z + 1] && save[temp.z + 1][temp.x][temp.y] == '.') {
			q.push(node{ temp.x,temp.y,temp.z + 1,temp.step + 1 });
			judge[temp.x][temp.y][temp.z + 1] = true;
		}
		if (!judge[temp.x][temp.y][temp.z - 1] && save[temp.z - 1][temp.x][temp.y] == '.') {
			q.push(node{ temp.x,temp.y,temp.z - 1 ,temp.step + 1 });
			judge[temp.x][temp.y][temp.z - 1] = true;
		}
		if (!judge[temp.x + 1][temp.y][temp.z] && save[temp.z][temp.x + 1][temp.y] == '.') {
			q.push(node{ temp.x + 1,temp.y,temp.z,temp.step + 1 });
			judge[temp.x + 1][temp.y][temp.z] = true;
		}
		if (!judge[temp.x - 1][temp.y][temp.z] && save[temp.z][temp.x - 1][temp.y] == '.') {
			q.push(node{ temp.x - 1,temp.y,temp.z,temp.step + 1 });
			judge[temp.x - 1][temp.y][temp.z] = true;
		}
		if (!judge[temp.x][temp.y + 1][temp.z] && save[temp.z][temp.x][temp.y + 1] == '.') {
			q.push(node{ temp.x,temp.y + 1,temp.z,temp.step + 1 });
			judge[temp.x][temp.y + 1][temp.z] = true;
		}
		if (!judge[temp.x][temp.y - 1][temp.z] && save[temp.z][temp.x][temp.y - 1] == '.') {
			q.push(node{ temp.x,temp.y - 1,temp.z,temp.step + 1 });
			judge[temp.x][temp.y - 1][temp.z] = true;
		}
	}
}
int main()
{
	while (scanf("%d%d%d", &z, &x, &y))
	{
		if ((!x) && (!y) && (!z))return 0;
		for (int i = 1; i <= z; i++)
			for (int j = 1; j <= x; j++)
				scanf("%s", save[i][j] + 1);
		for (int i = 1; i <= z; i++)
			for (int j = 1; j <= x; j++)
				for (int k = 1; k <= y; k++)
				{
					if (save[i][j][k] == 'S')
						STA.x = j, STA.y = k, STA.z = i;
					else if (save[i][j][k] == 'E')
					{
						END.x = j, END.y = k, END.z = i;
						save[i][j][k] = '.';
					}
				}
		q.push(node{ STA.x,STA.y,STA.z,0 });
		judge[STA.x][STA.y][STA.z] = true;
		bfs();
		memset(save, 0, sizeof(save));
		memset(judge, 0, sizeof(judge));
		while (!q.empty())q.pop();
	}
}

バイ・ホイール月

公開された31元の記事 ウォンの賞賛4 ビュー1158

おすすめ

転載: blog.csdn.net/qq_35339563/article/details/105177232