Tuesday, June 9, 2015

UVa 532 - Dungeon Master

// UVa 532 - Dungeon Master

#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;

int move[6][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { -1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } };

struct pos {
	int i, j, k;
};

bool operator ==(pos a, pos b) {
	return a.i == b.i && a.j == b.j && a.k == b.k;
}

int main() {
	int l, r, c;
	while ((cin >> l >> r >> c) && (l || r || c)) {
		string dummy;
		pos s, e;
		string dungeon[30][30];
		for (int i = 0; i < l; i++) {
			getline(cin, dummy);
			for (int j = 0; j < r; j++) {
				getline(cin, dungeon[i][j]);
				for (int k = 0; k < c; k++)
					if (dungeon[i][j][k] == 'S') {
						s.i = i;
						s.j = j;
						s.k = k;
					} else if (dungeon[i][j][k] == 'E') {
						e.i = i;
						e.j = j;
						e.k = k;
					}
			}
		}
		int T[30][30][30];
		memset(T, 127, sizeof(T));
		bool found = false;
		queue<pos> q;
		q.push(s);
		T[s.i][s.j][s.k] = 0;
		while (!found && !q.empty()) {
			pos b4 = q.front();
			q.pop();
			for (int m = 0; m < 6; m++) {
				pos now;
				now.i = b4.i + move[m][0];
				now.j = b4.j + move[m][1];
				now.k = b4.k + move[m][2];
				if (now.i >= 0 && now.i < l && now.j >= 0 && now.j < r && now.k >= 0 && now.k < c && dungeon[now.i][now.j][now.k] != '#') {
					T[now.i][now.j][now.k] = T[b4.i][b4.j][b4.k] + 1;
					if (now == e) {
						found = true;
						break;
					}
					q.push(now);
					dungeon[now.i][now.j][now.k] = '#';
				}
			}
		}
		if (found)
			printf("Escaped in %d minute(s).\n", T[e.i][e.j][e.k]);
		else
			printf("Trapped!\n");
	}
	return 0;
}

No comments:

Post a Comment