HDU 5012 Dice(西安网络赛F题)
2014-09-15 09:57
417 查看
HDU 5012 Dice
题目链接思路:这题也是水水的,直接记忆化广搜一下即可
代码:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int rot[4][6] = { {4, 3, 1, 2, 5, 6},
{3, 4, 2, 1, 5, 6},
{6, 5, 3, 4, 1, 2},
{5, 6, 3, 4, 2, 1}};
struct Dice {
int v[6];
int hash() {
int ans = 0;
for (int i = 0; i < 6; i++)
ans = ans * 6 + v[i];
return ans;
}
};
Dice rotate(const int *type, Dice a) {
Dice b;
for (int i = 0; i < 6; i++)
b.v[type[i] - 1] = a.v[i];
return b;
}
int vis[55555];
int main() {
Dice st, ed;
while (~scanf("%d", &st.v[0])) {
for (int i = 1; i < 6; i++)
scanf("%d", &st.v[i]);
for (int i = 0; i < 6; i++)
scanf("%d", &ed.v[i]);
queue<Dice> Q;
Q.push(st);
memset(vis, 0, sizeof(vis));
vis[st.hash()] = 0;
int flag = 1;
while (!Q.empty()) {
Dice u = Q.front();
Q.pop();
if (u.hash() == ed.hash()) {
printf("%d\n", vis[ed.hash()]);
flag = 0;
break;
}
for (int i = 0; i < 4; i++) {
Dice v = rotate(rot[i], u);
if (vis[v.hash()]) continue;
Q.push(v);
vis[v.hash()] = vis[u.hash()] + 1;
}
}
if (flag) printf("-1\n");
}
return 0;
}
相关文章推荐
- hdu 5012 Dice(西安网络赛F题,BFS)
- hdu 5012 2014西安网络赛
- hdu 5012(广搜,西安网络赛)
- HDU 5012 Dice (2014年西安赛区网络赛F题)
- 2014 西安赛区网络赛 5012Dice
- HDOJ 5012 Dice--2014网络赛西安赛区F题
- HDU 5012 Dice
- HDU 5012 Dice 隐式图的搜索
- hdu 5012 Dice
- HDU 5012 Dice (bfs)
- 【搜索】 HDU 5012 Dice
- HDU5012 Dice
- hdu 5011 Game(2014 西安网络赛)
- HDU 5007 Post Robot(西安网络赛A题)
- HDU-#5012 Dice(BFS)
- hdu 5012 dice
- hdu 2014西安赛区网络赛 5011Game
- HDU 5011 Game(西安网络赛E题)
- HDU 5012 Dice
- HDU 5017 Ellipsoid(西安网络赛K题)