您的位置:首页 > 其它

hdu 5012 Dice(隐式图搜索)

2014-09-15 19:24 483 查看
题目链接:hdu 5012 Dice

题目大意:给出一个立方体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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: