码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 816 - Abbott's Revenge(BFS)

时间:2014-10-10 23:16:44      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   on   

UVA 816 - Abbott‘s Revenge

题目链接

题意:一个迷宫,每个点限制了从哪一方向来的,只能往左右前走,然后问起点到终点的最短路径

思路:BFS,每个点拆成4个方向的点,对应能走的方向建图跑一下bfs即可

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int N = 10005;
const int D[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
char name[25];

int n, m;
vector<int> g[15][15][4];

struct State {
	int x, y, dir;
	int pre;
} Q[N], s, e;

char str[25];
int x, y, vis[15][15][4];

int hash(char c) {
	if (c == 'F') return 0;
	if (c == 'R') return 1;
	if (c == 'L') return -1;
	if (c == 'N') return 0;
	if (c == 'E') return 1;
	if (c == 'S') return 2;
	return 3;
}

#define MP(a,b) make_pair(a,b)
typedef pair<int, int> pii;
vector<pii> ans;

void print(int u) {
	if (u == -1) return;
	print(Q[u].pre);
	ans.push_back(MP(Q[u].x, Q[u].y));
}

void bfs() {
	ans.clear();
	memset(vis, 0, sizeof(vis));
	int head = 0, rear = 0; s.pre = -1;
	Q[rear++] = s;
	vis[s.x][s.y][s.dir] = 1;
	while (head < rear) {
		State u = Q[head++];
		if (u.x == e.x && u.y == e.y) {
			print(head - 1);
			int tot = ans.size();
			for (int i = 0; i < tot; i++) {
				if (i % 10 == 0) printf("\n ");
				printf(" (%d,%d)", ans[i].first, ans[i].second);
			}
			printf("\n");
			return;
		}
		for (int i = 0; i < g[u.x][u.y][u.dir].size(); i ++) {
			int di = (g[u.x][u.y][u.dir][i] + u.dir + 4) % 4;
			State v = u;
			v.x += D[di][0]; v.y += D[di][1];
			if (v.x < 0 || v.y < 0) continue;
			v.dir = di;
			if (vis[v.x][v.y][v.dir]) continue;
			vis[v.x][v.y][v.dir] = 1;
			v.pre = head - 1;
			Q[rear++] = v;
		}
	}
	printf("\n  No Solution Possible\n");
}

int main() {
	while (~scanf("%s", name) && strcmp(name, "END")) {
		memset(g, 0, sizeof(g));
		printf("%s", name);
		scanf("%d%d%s", &s.x, &s.y, str);
		s.dir = hash(str[0]);
		scanf("%d%d", &e.x, &e.y);
		g[s.x][s.y][hash(str[0])].push_back(0);
		int x, y;
		while (scanf("%d", &x) && x) {
			scanf("%d", &y);
			while (scanf("%s", str) && str[0] != '*') {
				int len = strlen(str);
				for (int i = 1; i < len; i++)
					g[x][y][hash(str[0])].push_back(hash(str[i]));
			}
		}
		bfs();
	}
	return 0;
}


UVA 816 - Abbott's Revenge(BFS)

标签:style   http   color   io   os   ar   for   sp   on   

原文地址:http://blog.csdn.net/accelerator_/article/details/39967387

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!