题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5012

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6
0 3 -1
先贴一发队友写的。等会再补上本弱的!
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct saizi
{
int b1,b2,b3,b4,b5,b6;
int size;
saizi() {}
saizi(int a1,int a2,int a3,int a4,int a5,int a6,int si):b1(a1),b2(a2),b3(a3),b4(a4),b5(a5),b6(a6),size(si) {};
bool operator == (const saizi& b) const
{
if(b.b1==b1&&b.b2==b2&&b.b3==b3&&b.b4==b4&&b.b5==b5&&b.b6==b6)
return 1;
return 0;
}
} sai,tem,tel;
int dice[10][10][10][10][10][10];
int a[10];
int bfs()
{
queue<saizi> q;
sai.size=0;
q.push(sai);
while(!q.empty())
{
sai=q.front();
q.pop();
if(sai==tel)
return sai.size;
if(dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]==1)
continue;
dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]=1;
tem=saizi(sai.b6,sai.b5,sai.b3,sai.b4,sai.b1,sai.b2,sai.size+1);
q.push(tem);
tem=saizi(sai.b5,sai.b6,sai.b3,sai.b4,sai.b2,sai.b1,sai.size+1);
q.push(tem);
tem=saizi(sai.b3,sai.b4,sai.b2,sai.b1,sai.b5,sai.b6,sai.size+1);
q.push(tem);
tem=saizi(sai.b4,sai.b3,sai.b1,sai.b2,sai.b5,sai.b6,sai.size+1);
q.push(tem);
}
return -1;
}
int main()
{
int ans;
while(scanf("%d",&sai.b1)!=EOF)
{
memset(dice,0,sizeof(dice));
scanf("%d",&sai.b2);
scanf("%d",&sai.b3);
scanf("%d",&sai.b4);
scanf("%d",&sai.b5);
scanf("%d",&sai.b6);
scanf("%d",&tel.b1);
scanf("%d",&tel.b2);
scanf("%d",&tel.b3);
scanf("%d",&tel.b4);
scanf("%d",&tel.b5);
scanf("%d",&tel.b6);
ans=bfs();
printf("%d\n",ans);
}
return 0;
}
代码如下:
#include <cstdio>
#include <cstring>
struct Dice
{
int top, bottom;
int left, right;
int front_f, back_b;
int step;
} a[100017];
int goal[6];
int bfs()
{
int tp = 1, tl = 0;
Dice t, f;
a[0].step = 0;
while(1)
{
t = a[tl++];
if(t.top==goal[0]&&t.bottom==goal[1]&&t.left==goal[2]&&t.right==goal[3]&&t.front_f==goal[4]&&t.back_b==goal[5])
{
return t.step;
break;
}
else if(t.step > 4)
{
return t.step = -1;
break;
}
else
{
f.top = t.front_f;//向后
f.bottom = t.back_b;
f.left = t.left;
f.right = t.right;
f.front_f = t.bottom;
f.back_b = t.top;
f.step = t.step+1;
a[tp++] = f;
f.top = t.back_b;//向前
f.bottom = t.front_f;
f.left = t.left;
f.right = t.right;
f.front_f = t.top;
f.back_b = t.bottom;
f.step = t.step+1;
a[tp++] = f;
f.top = t.right;//向左
f.bottom = t.left;
f.left = t.top;
f.right = t.bottom;
f.front_f = t.front_f;
f.back_b = t.back_b;
f.step = t.step+1;
a[tp++] = f;
f.top = t.left;//向右
f.bottom = t.right;
f.left = t.bottom;
f.right = t.top;
f.front_f = t.front_f;
f.back_b = t.back_b;
f.step = t.step+1;
a[tp++] = f;
}
}
}
int main()
{
while(~scanf("%d",&a[0].top))
{
scanf("%d",&a[0].bottom);
scanf("%d",&a[0].left);
scanf("%d",&a[0].right);
scanf("%d",&a[0].front_f);
scanf("%d",&a[0].back_b);
for(int i = 0; i < 6; i++)
{
scanf("%d",&goal[i]);
}
int ans = bfs();
printf("%d\n",ans);
}
return 0;
}
原文地址:http://blog.csdn.net/u012860063/article/details/39272269