uva10047

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 30, INF = 1e9 + 7, dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
int f[maxn][maxn][4][5],n,m,sx,sy,ex,ey,ans;
char ch[maxn][maxn];

struct Node {
	int x, y, face, color, cnt;
	Node(int _x, int _y) :x(_x), y(_y) {
		cnt = 0;
		face = 0;
		color = 0;
	}
};

int bfs() {
	int cnt = 0;
	queue<Node> q;
	Node node(sx, sy);
	q.push(node);
	f[sx][sy][0][0] = 0;

	while (!q.empty()) {
		int len = q.size();
		for (int i = 0; i < len; i++) {
			node = q.front();
			q.pop();
			if (node.x == ex && node.y == ey && node.color == 0) {
				return cnt;
			}
			Node tmp = node;
			tmp.x = node.x + dir[node.face][0];
			tmp.y = node.y + dir[node.face][1];
			tmp.color = (node.color + 1) % 5;
			if (tmp.x >= 0 && tmp.x < n && tmp.y >= 0 && tmp.y < m && ch[tmp.x][tmp.y] != '#' && f[tmp.x][tmp.y][tmp.face][tmp.color] == -1) {
				f[tmp.x][tmp.y][tmp.face][tmp.color] = f[node.x][node.y][node.face][node.color] + 1;
				q.push(tmp);
			}
			tmp = node;
			tmp.face = (node.face + 1) % 4;
			if (tmp.x >= 0 && tmp.x < n && tmp.y >= 0 && tmp.y < m && f[tmp.x][tmp.y][tmp.face][tmp.color] == -1) {
				f[tmp.x][tmp.y][tmp.face][tmp.color] = f[node.x][node.y][node.face][node.color] + 1;
				q.push(tmp);
			}
			tmp.face = (node.face + 3) % 4;
			if (tmp.x >= 0 && tmp.x < n && tmp.y >= 0 && tmp.y < m && f[tmp.x][tmp.y][tmp.face][tmp.color] == -1) {
				f[tmp.x][tmp.y][tmp.face][tmp.color] = f[node.x][node.y][node.face][node.color] + 1;
				q.push(tmp);
			}
		}
		cnt++;
	}
	return INF;
}
int main()
{
	int kCase = 0;
	while (cin >> n >> m && m && n) {
		ans = INF;
		memset(f, -1, sizeof(f));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> ch[i][j];
				if (ch[i][j] == 'S') {
					sx = i;
					sy = j;
				}
				else if (ch[i][j] == 'T') {
					ex = i;
					ey = j;
				}
			}
		}
		int ans = bfs();
		if (kCase)cout << endl;
		cout << "Case #" << ++kCase << endl;
		if (ans == INF) cout << "destination not reachable" << endl;
		else cout << "minimum time = "<< ans<<" sec" << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值