UVA 10047The Monocycle(BFS)
来源:互联网 发布:数据库推荐书籍 编辑:程序博客网 时间:2024/05/16 00:48
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=988
题意:从S走到T,每走一格车轮转一个颜色,求到T时车轮颜色为绿色(起始也为绿色)的最短路径。每次可向当前车轮方向走一格,或者原地向左转动或向右转动。
思路:几乎也为裸的BFS,多记录一些状态,即需要记录到某点车轮为某个方向某个颜色时的最短路径。
代码:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <math.h>#include <queue>using namespace std;const int N = 40;const int INF = 0x3f3f3f3f;struct Node { int x, y; int color, step, turn;};char _map[N][N];int n, m;int dis[N][N][10][10];int x[4] = {-1, 0, 1, 0};int y[4] = {0, 1, 0, -1};bool pass(Node to) { if (to.x >= 0 && to.x < n && to.y >= 0 && to.y < m && _map[to.x][to.y] != '#' && dis[to.x][to.y][to.color][to.turn] > to.step) return true; return false;}int bfs(Node s) { memset(dis, INF, sizeof(dis)); queue<Node> q; q.push(s); dis[s.x][s.y][s.color][s.turn] = s.step; while (!q.empty()) { Node now = q.front(); q.pop(); if (_map[now.x][now.y] == 'T' && now.color == 0) { return now.step; } Node to; to.x = now.x; to.y = now.y; to.color = now.color; to.step = now.step + 1; to.turn = (now.turn + 1) % 4; if (pass(to)) { q.push(to); dis[to.x][to.y][to.color][to.turn] = to.step; } to.turn = (now.turn + 3) % 4; if (pass(to)) { q.push(to); dis[to.x][to.y][to.color][to.turn] = to.step; } to.x = now.x + x[now.turn]; to.y = now.y + y[now.turn]; to.color = (now.color + 1) % 5; to.turn = now.turn; if (pass(to)) { q.push(to); dis[to.x][to.y][to.color][to.turn] = to.step; } } return -1;}int main() { int i_case = 1; while (scanf("%d%d", &n, &m) != EOF) { if (!n && !m) break; for (int i = 0; i < n; i++) scanf("%s", _map[i]); Node st; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (_map[i][j] == 'S') { st.x = i, st.y = j; st.turn = 0; st.color = 0; st.step = 0; } } } int res = bfs(st); if (i_case > 1) printf("\n"); if (res == -1) printf("Case #%d\ndestination not reachable\n", i_case++); else printf("Case #%d\nminimum time = %d sec\n", i_case++, res); } return 0;}
0 0
- uva 10047 - The Monocycle(Bfs)
- uva 10047 The Monocycle (BFS)
- UVa 10047 The monocycle (BFS)
- UVA - 10047 The Monocycle (BFS)
- UVA 10047 - The Monocycle(BFS)
- UVA 10047The Monocycle(BFS)
- UVA 10047 The Monocycle(BFS)
- UVA 10047 - The Monocycle BFS
- UVA 10047 The Monocycle (BFS)
- 【UVa】10047 The Monocycle BFS
- UVA 10047 The Monocycle (bfs)
- uva 10047 The Monocycle BFS
- UVA-10047The Monocycle- bfs
- UVA 10047 The Monocycle(BFS)
- UVA 10047 The Monocycle(bfs)
- UVa 10047 The Monocycle (BFS + 状态判重)
- uva 10047 The Monocycle(BFS+状态判重)
- UVA 10047 The Monocycle(多状态BFS)
- Markdown语法图
- taglist
- unity实现拖拽
- Android开发中进程和线程
- OOP面向对象——实例——运算
- UVA 10047The Monocycle(BFS)
- chrome跨域访问
- UVA 11582 Colossal Fibonacci Numbers!
- android模拟器与pc,模拟器与模拟之间用socket进行通信和获得模拟器ip地址程序
- 行人检测之HOG——方向梯度直方图(Histogram of Oriented Gradient)
- 大型网站技术架构——架构演化发展历程
- Git冲突解决之P4Merge
- HDU 4514 湫湫系列故事——设计风景线(并查集+树的直径)
- UVA - 10375 Choose and divide (唯一分解定理)