hdu 5012 Dice(隐式图搜索)
2014-09-15 19:24
483 查看
题目链接:hdu 5012 Dice
题目大意:给出一个立方体6个面的值,问说最少经过次旋转得到目标状态。
解题思路:隐式图搜索,直接将一个状态映射成一个整数,然后对应BFS。
题目大意:给出一个立方体6个面的值,问说最少经过次旋转得到目标状态。
解题思路:隐式图搜索,直接将一个状态映射成一个整数,然后对应BFS。
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; typedef int Mat[10]; const int maxn = 100005; const int INF = 0x3f3f3f3f; const int dir[4][6] = { {3, 2, 0, 1, 4, 5}, {2, 3, 1, 0, 4, 5}, {5, 4, 2, 3, 0, 1}, {4, 5, 2, 3, 1, 0}}; Mat S, E; int d[maxn]; bool init () { for (int i = 0; i < 6; i++) if (scanf("%d", &S[i]) != 1) return false; for (int i = 0; i < 6; i++) if (scanf("%d", &E[i]) != 1) return false; for (int i = 0; i < 6; i++) { S[i]--; E[i]--; } return true; } int get_hash(Mat x) { int ret = 0; for (int i = 0; i < 6; i++) ret = ret * 6 + x[i]; return ret; } void reback(int u, Mat a) { for (int i = 5; i >= 0; i--) { a[i] = u % 6; u /= 6; } } int rotate (int u, int x) { Mat a, b; reback(u, a); for (int i = 0; i < 6; i++) b[i] = a[dir[x][i]]; return get_hash(b); } int bfs (int s, int e) { memset(d, INF, sizeof(d)); queue<int> que; que.push(s); d[s] = 0; if (s == e) return 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int v = rotate(u, i); if (d[v] > d[u] + 1) { d[v] = d[u] + 1; que.push(v); } if (v == e) return d[v]; } } return -1; } int main () { while (init()) { printf("%d\n", bfs(get_hash(S), get_hash(E))); } return 0; }
相关文章推荐
- HDU 5012 Dice 隐式图的搜索
- hud 5012 Dice(隐式图的搜索 )
- hdu 5012 Dice 记忆化搜索
- Dice - HDU 5012 搜索
- 【搜索】 HDU 5012 Dice
- 【搜索】 HDU 5012 Dice
- HDU 5012 Dice
- hdu 5012 Dice 2014 ACM/ICPC Asia Regional Xi'an Online bfs
- HDU - 5012 Dice(BFS)
- HDU5012 Dice
- HDU 5012 Dice (bfs + 记忆化搜索)
- ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)
- HDU 1016 Prime Ring Problem - 隐式图的搜索
- hdu 5012 Dice(bfs)
- HDU 5012 Dice (BFS)
- hdu 5012 Dice
- hdu 5012——Dice
- HDU 5012 Dice (bfs)
- HDU 5012 Dice
- HDU 5012 Dice(西安网络赛F题)