UVA 816 - Abbott's Revenge(BFS)

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)

时间: 2025-01-24 10:11:48

UVA 816 - Abbott's Revenge(BFS)的相关文章

UVa 816 Abbott的复仇(BFS)

寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同,允许出去的方向也不同.所以在记录迷宫的时候比较麻烦,可以用一个四元组has_edge[10][10][4][4]来记录,前两个元素代表坐标点,第三个元素代表进入该点时的方向,第四个元素代表离开该点时的方向.在BFS遍历时还是和以前的题目差不多的,记录好路径,最后回溯输出就行. 我还犯了个小错误,因为

uva 816 - Abbott&#39;s Revenge(有一点难度的bfs迷宫题目)

就是典型的bfs,但这道题目的难点在于其条件的读取和判断并不简单,需要想办法来读取条件,也需要想办法来判断在每个点处能不能满足向下继续走的条件. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<iostream> #include<algorithm> using namespace std; struct note { int

UVA 816 -- Abbott&#39;s Revenge(BFS求最短路)

 UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉点的方向(用NEWS表示不同方向)不同时, 允许出去的方向也不相同. 例如:1 2 WLF NR ER * 表示如果 进去时朝W(左), 可以 左转(L)或直行(F), 如果 朝N只能右转(R) 如果朝E也只能右转.* 表示这个点的描述结束啦! 输入有: 起点的坐标, 朝向, 终点的坐标.然后是各个

Uva 816 Abbott&#39;s Revenge(BFS)

#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std; struct Node{ int row,col,dir; Node(int row=0,int col=0,int dir=0):row(row),col(col),dir(dir){}}; const char * dirs="NESW";const char * t

UVA 816 Abbott’s Revenge

bfs求最短路,递归打印最短路的具体路径: 难点: 当前状态和转弯方式很复杂,要仔细处理: 递归打印:用一个数组存储路径中结点的前一个节点,递归查找 (bfs无法确定下一个结点,但对于没一个结点,它的上一个结点是确定的!) ps:输出因为太懒不想处理所以按书上打的:递归打印理解有点麻烦... 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue>

Uva 816 Abbott的复仇(三元组BFS + 路径还原)

题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir),然后做好输入处理的细节, 这题的关键在于bfs中的路径还原. 其实bfs的过程就是一棵树,如下图 除了起点外, 每个点都有且只有一个父亲节点, 那么我们只需要开一个pre数组来记录每个点的父亲, 找到终点后从终点往上不断找父亲节点, 直到找到父亲节点, 那么就完成了路径还原的 步骤. 1 #in

UVa 439骑士的移动(BFS)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=380 做了这道题之后对BFS总算是有了点认识了. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int map[10][10]; 6 typedef struct node 7

L - Abbott&#39;s Revenge(比较复杂的bfs)

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 816 Appoint description: Description  Abbott’s Revenge  The 1999 World Finals Contest included a problem based on a “dice maze.” At the time the problem

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all