UVA 589 - Pushing Boxes(BFS+状态判重)
来源:互联网 发布:coap协议java 开发 编辑:程序博客网 时间:2024/06/14 03:20
题目链接:589 - Pushing Boxes
题意:就是模拟推箱子游戏,要求推箱子次数最少,然后是总次数最少的方案。思路:广搜+状态判重,用人的位置和箱子位置和当前步数作为状态。然后由于是要优先推箱子次数少,所以利用优先队列去取状态。
代码:
#include <stdio.h>#include <string.h>#include <iostream>#include <vector>#include <queue>#include <string>#include <map>#define INF 0x3f3f3f3fusing namespace std;const int d[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};const char s1[10] = "NSEW";const char s2[10] = "nsew";const int N = 25;int r, c, push_num;char g[N][N];string ans;map<int, bool> vis[20][20][20][20];typedef pair<int, int> pi;pi s, b, t;struct State {int sx, sy, bx, by, num;string path;State() {}State(int _sx, int _sy, int _bx, int _by, int _num) {sx = _sx; sy = _sy; bx = _bx; by = _by; num = _num;path = "";}friend bool operator < (State a, State b) {if (a.num != b.num)return a.num > b.num;return a.path.length() > b.path.length();}};void init() {ans = "";push_num = INF;for (int i = 0; i < r; i++) {scanf("%s", g[i]);for (int j = 0; j < c; j++) {if (g[i][j] == 'S')s = make_pair(i, j);else if (g[i][j] == 'B')b = make_pair(i, j);else if (g[i][j] == 'T')t = make_pair(i, j);if (g[i][j] != '#')g[i][j] = '.';for (int k = 0; k < r; k++)for (int l = 0; l < c; l++)vis[i][j][k][l].clear();}}}bool check(int x, int y) {if (x < 0 || x >= r || y < 0 || y >= c || g[x][y] != '.') return false;return true;}void solve() {priority_queue<State> Q;vis[s.first][s.second][b.first][b.second][0] = 1;Q.push(State(s.first, s.second, b.first, b.second, 0));while (!Q.empty()) {State p = Q.top();Q.pop();if (p.bx == t.first && p.by == t.second) {if (push_num > p.num) {ans = p.path;push_num = p.num;} else if (push_num == p.num) {if (ans.length() > p.path.length())ans = p.path;}else break;continue;}for (int i = 0; i < 4; i++) {State q = p;q.sx += d[i][0];q.sy += d[i][1];if (!check(q.sx, q.sy)) continue; q.path += s2[i];if (q.sx == q.bx && q.sy == q.by) {q.bx += d[i][0], q.by += d[i][1];if (!check(q.bx, q.by)) continue;q.num++;if (q.num > push_num) continue;q.path[q.path.length() - 1] = s1[i];}if (vis[q.sx][q.sy][q.bx][q.by][ans.length()]) continue;vis[q.sx][q.sy][q.bx][q.by][ans.length()] = true;Q.push(q);}}}int main() {int cas = 0;while (~scanf("%d%d", &r, &c) && r || c) {init();printf("Maze #%d\n", ++cas);solve();if (ans.length())cout << ans << endl;else printf("Impossible.\n");printf("\n");}return 0;}
1 0
- UVA 589 - Pushing Boxes(BFS+状态判重)
- uva 589 - Pushing Boxes(双重bfs)
- UVa 589 - Pushing Boxes
- UVa 10047 The Monocycle (BFS + 状态判重)
- uva 10047 The Monocycle(BFS+状态判重)
- UVA 1377 - Ruler(BFS + 状态判重)
- poj 1475 Pushing Boxes (推箱子游戏 三维数组判重)
- Pushing Boxes (poj 1475 嵌套bfs)
- poj 1475 Pushing Boxes(bfs)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(BFS)
- POJ 1475 Pushing Boxes(嵌套BFS)
- 【POJ1475】Pushing Boxes-A*(或BFS?)
- POJ1475 Pushing Boxes(BFS+BFS)
- UVa 321 The New Villa ( BFS + 状态压缩判重)
- UVa 816 - Abbott's Revenge(BFS+记录状态判重)
- UVa 810 - A Dicey Problem(BFS+记录状态判重)
- poj 1475 Pushing Boxes(推箱子游戏,bfs+bfs)
- maven处理资源文件
- 服务器后台语言选型参考
- Myeclipse中如何更改jsp默认的打开方式
- 设计移动App的十大技巧
- adb devices连接不到设备的解决方式
- UVA 589 - Pushing Boxes(BFS+状态判重)
- VPN技术详解 L2TP VPN
- 数据结构之链表反转
- 一步一步教你做ios推送
- win7 64bit 下mysql5.5.36(x86,64bit) ODBC qt5.2连接
- 在XP下如何安装支持vs2008的visual assist插件
- Struts2笔记---开发环境的搭配
- VS2010安装EF5.0 EntityFramework
- 我们的天空 - BEYOND