UVa816 例题 6-14 Abbott的复仇 (Abbott's Revenge,ACM/ICPC World Finals 2000)
2017-07-31 13:47
666 查看
原题链接: UVa-816
题目大意:
模有一个最多包含9*9个交叉点的迷宫。输入起点、离开起点时的朝向和终点,求一条最短路径。(具体题目参考原题和紫书)解题思路:
本题是一道用BFS求最短路径的迷宫题。不过和普通的迷宫不太一样,该题加了一个朝向和转向的概念。可以想象成十字路口修路,限制道路前进方向(不太准确)。这道题看的时候完全没有思路,虽然写过用BFS求解迷宫的问题,但是想了半个多小时也没有思路(你好菜啊),然后就去看紫书上给的思路,之后大概明白了一点,然后就开始按着刘汝佳给的思路和代码写了写。其实还是有些地方不是特别明白,所以有错误的地方,希望大家见谅(你来咬我啊)。整体思路就是在读入的时候构造一个四维的数组has_edge[r][c][d][t]来存储,在交叉点(r,c)能朝向d时向t方向转向。同时还要读取输入起点(r0,c0)、离开起点时的朝向(d)和终点(r2,c2),并将起点(r0,c0),朝d方向走到(r1,r1)。然后使用BFS的框架从寻找最短路径。
刘汝佳给的代码里有很多处理的很巧妙的地方比如朝向和转向的处理,还有就是walk函数。
遇到问题:
参考别人的代码没有自己写着有成就感,所以写到中间有点不太想写了,很懈怠,所以出了很多小问题(比如==写出=),导致后来花了很长时间去调试。感悟:
过一段时间,这个题应该再返工一次。代码:
#include<iostream> #include<string> #include<cstring> #include<queue> using namespace std; struct Node { int x, y, dir; Node() {} Node(int x, int y, int d = -1) :x(x), y(y), dir(d) {} }; const int MAXN = 9 + 5; const char *dircs = "NESW"; const char *turns = "FLR"; const int dr[4] = { -1,0,1,0 }; const int dc[4] = { 0,1,0,-1 }; int d[MAXN][MAXN][4], has_edge[MAXN][MAXN][4][3]; Node p[MAXN][MAXN][4]; string name; int dir_id(char c) { return strchr(dircs, c) - dircs; } //返回c在dircs的下标 int turn_id(char c) { return strchr(turns, c) - turns; } void inti(Node& begin, Node& end); bool inside(int x, int y); void print(Node u, Node begin); void BFS(const Node begin, const Node end); Node walk(const Node& u, int turn); int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); while (cin >> name && name != "END") { Node begin, end; inti(begin, end); BFS(begin, end); } return 0; } void BFS(const Node begin, const Node end) { queue<Node> q; Node u = walk(begin, 0); p[u.x][u.y][u.dir] = begin; d[u.x][u.y][u.dir] = 0; q.push(u); while (!q.empty()) { Node u = q.front(); q.pop(); if (u.x == end.x && u.y == end.y) { print(u, begin); return; } for (int i = 0; i < 3; i++) { Node v = walk(u, i); if (has_edge[u.x][u.y][u.dir][i] && inside(v.x, v.y) && d[v.x][v.y][v.dir] < 0) { d[v.x][v.y][v.dir] = d[u.x][u.y][u.dir] + 1; p[v.x][v.y][v.dir] = u; q.push(v); } } } cout << name << endl << " No Solution Possible\n"; } void print(Node u, Node begin) { vector<Node> v; while (1) { v.push_back(u); if (d[u.x][u.y][u.dir] == 0) break; u = p[u.x][u.y][u.dir]; } v.push_back(begin); cout << name; for (int i = v.size() - 1; i >= 0; i--) { if ((v.size()- 1-i) % 10 == 0) cout << endl << " "; cout << " (" << v[i].x << "," << v[i].y << ")"; } cout << endl; } bool inside(int x, int y) { if (x < 1 || y < 1 || x > 9 || y > 9) return false; return true; } Node walk(const Node& u, int turn) { int dir = u.dir; if (turn == 1) dir = (dir + 3) % 4; if (turn == 2) dir = (dir + 1) % 4; return Node(u.x + dr[dir], u.y + dc[dir], dir); } void inti(Node &begin, Node &end) { int x, y; string s; cin >> x >> y >> s; begin = Node(x, y, dir_id(s[0])); cin >> x >> y; end.x = x; end.y = y; memset(has_edge, 0, sizeof(has_edge)); memset(d, -1, sizeof(d)); while (cin >> x && x) { cin >> y; while (cin >> s && s != "*") { int dir = dir_id(s[0]); for (int i = 1; i < s.length(); i++) { int turn = turn_id(s[i]); has_edge[x][y][dir][turn] = 1; } } } }
相关文章推荐
- 例题6-14 Abbott的复仇(Abbott's Revenge, ACM/ICPC World Finals 2000, UVa 816)
- Abbott的复仇(Abbott's Revenge, ACM/ICPC World Finals 2000, UVa 816)<经典BFS>
- Abbott 的复仇(Abbott's Revenge,ACM|IPPC World Finals 2000, UVa 816)
- Abbott 的复仇(Abbott's Revenge,ACM|IPPC World Finals 2000, UVa 816)
- 【例题 6-14 UVA-816】Abbott's Revenge
- 例题6-1 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991, UVa210)
- 例题6-19 自组合(Self-Assembly, ACM/ICPC World Finals 2013, UVa 1572)
- 习题7-4 切断圆环链(Cutting Chains, ACM/ICPC World Finals 2000, UVa818)
- 【DP 训练】Stamps and Envelope Size, ACM/ICPC World Finals 1995, UVa242
- 信息解码(Message Decoding, ACM/ICPC World Finals 1991, UVa 213)
- 例题7-14 网格动物(Lattice Animals, ACM/ICPC NEERC 2004, UVa1602)
- UVa227 and acm/icpc world finals 1993 谜题
- 习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994, UVa232)
- Crossword Answers, ACM/ICPC World Finals 1994, UVa232
- 算法竞赛入门经典-习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994,UVa232)
- Sqaures, ACM/ICPC World Finals 1990, UVa
- UVa210 Concurrency Simulator (ACM/ICPC World Finals 1991) 双端队列
- 6_14 Abbott的复仇(UVa816)<图的最短路BFS>
- 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
- ACM/ICPC World Finals 1990 && UVA 201 Squares(数正方形)