这道题是一道状态空间搜索的题目,思路是用IDA*,哎,主要是没看清题目要求,老是WA
1.无论需不需要移动,都要打印中间的那个东西,害我一直WA
2.如果函数不是int ,一定要写返回值,其返回值不是0,是个随机值,这一点一直害我WA
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000;
int a[maxn];
char ans[maxn];
int line[8][7]=
{
{ 0, 2, 6,11,15,20,22}, // A
{ 1, 3, 8,12,17,21,23}, // B
{10, 9, 8, 7, 6, 5, 4}, // C
{ 19,18,17,16,15,14,13}, // D
{23,21,17,12,8,3,1},//E
{22,20,15,11,6,2,0},//F
{13,14,15,16,17,18,19},//G
{4,5,6,7,8,9,10},//H
};
int center[8]= {6,7,8,11,12,15,16,17};
int initial_check()
{
for(int i=0; i<8; i++)
if(a[center[i]]!=a[center[0]]) return 0;
return 1;
}
int h()//移动一次只能改变一个数,所以统计最少的相同的那个,看至少需要几步
{
int sum=1000,cnt;
for(int i=1; i<=3; i++)
{
cnt=0;
for(int j=0; j<7; j++)
if(a[center[j]]!=i) cnt++;
sum=min(sum,cnt);
}
return sum;
}
inline void move_item(int i) {
int tmp = a[line[i][0]];
for(int j = 0; j < 6; j++) a[line[i][j]] = a[line[i][j+1]];
a[line[i][6]] = tmp;
}
int dfs(int d,int max_depth)
{
if(d==max_depth)
{
if(initial_check())
{
ans[d] = '\0';
printf("%s\n", ans);
return 1;
}
else return 0;
}
if(d+h()>max_depth) return 0;//A*的预期公式
for(int i=0; i<8; i++)
{
ans[d]='A'+i;
move_item(i);
if(dfs(d+1,max_depth)) return 1;
if(i%2==0) move_item((i+5)%8);
else move_item((i+3)%8);
}
return 0;
}
int main()
{
while(scanf("%d",&a[0])==1&&a[0])
{
if(!a[0]) break;
for(int i=1; i<24; i++)
scanf("%d",&a[i]);
if(initial_check())
{
printf("No moves needed\n");
}
else
{
for(int depth=1; 1; depth++) //开始迭代加深搜索
{
if(dfs(0,depth)) break;
}
}
cout<<a[center[0]]<<endl;
}
return 0;
}