/* Note:Your choice is C IDE */
#include "stdio.h"
#include "string.h"
#define MAX 370000
typedef struct
{
char string[12];
int state;
int x;
int y;
}string;
typedef struct Queue
{
string queue[MAX];
int low;
int top;
}queue;
queue s1,s2;
char start[12];
char goal[9]={"123456789"};
char flag[MAX];
char step[2][MAX];
int xd[4]={0,-1,0,1};
int yd[4]={1,0,-1,0};
int fact[9]={1,1,2,6,24,120,720,5040,40320};
int cantor(char *s){
int i,j,count;
int sum=0;
for(i=0;i<8;i++)
{
count=0;
for(j=i+1;j<=8;j++)
if(s[i]>s[j])
count++;
sum+=fact[8-i]*count;
}
return sum;
}
int judge(int x,int y)
{
if(x>=0&&x<3&&y>=0&&y<3)
return 1;
return 0;
}
int ckey(char *s){
int i,j;
int sum=0,count;
for(i=8;i>=0;i--)
{
count=0;
if(s[i]=='9')
continue;
for(j=i-1;j>=0;j--)
{
if(s[j]=='9')
continue;
if(s[i]<s[j])
count++;
}
sum+=count;
}
if(sum%2)
return 0;
return 1;
}
int BFS()
{
string t1,t2;
int i;
while(s1.low<s1.top&&s2.low<s2.top)
{
t1=s1.queue[++s1.low];
for(i=0;i<4;i++)
{
if(judge(t1.x+xd[i],t1.y+yd[i]))
{
int currentstate,z,nz;
char t[12];
strcpy(t,t1.string);
z=3*t1.x+t1.y;
nz=3*(t1.x+xd[i])+t1.y+yd[i];
t[z]^=t[nz];
t[nz]^=t[z];
t[z]^=t[nz];
currentstate=cantor(t);
if(!flag[currentstate])
{
flag[currentstate]=1;
step[0][currentstate]=step[0][t1.state]+1;
strcpy(s1.queue[++s1.top].string,t);
s1.queue[s1.top].x=t1.x+xd[i];
s1.queue[s1.top].y=t1.y+yd[i];
s1.queue[s1.top].state=currentstate;
}
else if(flag[currentstate]==2)
return step[0][t1.state]+step[1][currentstate]+1;
}
}
t2=s2.queue[++s2.low];
for(i=0;i<4;i++)
{
if(judge(t2.x+xd[i],t2.y+yd[i]))
{
int currentstate,z,nz;
char t[12];
strcpy(t,t2.string);
z=3*t2.x+t2.y;
nz=3*(t2.x+xd[i])+t2.y+yd[i];
t[z]^=t[nz];
t[nz]^=t[z];
t[z]^=t[nz];
currentstate=cantor(t);
if(!flag[currentstate])
{
flag[currentstate]=2;
strcpy(s2.queue[++s2.top].string,t);
step[1][currentstate]=step[1][t2.state]+1;
s2.queue[s2.top].x=t2.x+xd[i];
s2.queue[s2.top].y=t2.y+yd[i];
s2.queue[s2.top].state=currentstate;
}
if(flag[currentstate]==1)
return step[1][t2.state]+step[0][currentstate]+1;
}
}
}
return -1;
}
void main()
{
int i,result;
char hb[9];
while(scanf("%s",hb)!=EOF)
{
result=0;
start[0]=hb[0];
for(i=1;i<9;i++)
{
scanf("%s",hb);
start[i]=hb[0];
}
for(i=0;i<9;i++)
if(start[i]=='x')
{
start[i]='9';
break;
}
if(!ckey(start))
result=-1;
else if(!strcmp(start,goal))
result=0;
else
{
memset(flag,0,sizeof(flag));
memset(step,0,sizeof(step));
s1.low=s1.top=-1;
s2.low=s2.top=-1;
flag[s1.queue[++s1.top].state=cantor(start)]=1;
flag[s2.queue[++s2.top].state=cantor(goal)]=2;
strcpy(s1.queue[s1.top].string,start);
strcpy(s2.queue[s2.top].string,goal);
s1.queue[s1.top].x=i/3;
s1.queue[s1.top].y=i%3;
s2.queue[s2.top].x=2;
s2.queue[s2.top].y=2;
result=BFS();
}
if(result<0)
printf("unsolvable\n");
else printf("%d\n",result);
}
}
这是经典的八数码问题的求解。。。双向BFS,。。。问题是给出一个初始状态。。求到达目标状态最小需要多少步。。。。其中目标状态固定。。。为1 2 3 4 5 6 7 8 x。。。x代表空格。。这九个数代表3*3的矩阵。。说白了就是小时候玩的拼图游戏。。。我把注释都消了。。。你慢慢看。。。。
追问有没有简单一点的哈?大一水平啊,顺便解释一下大概是什么意思啊
追答我也是大一水平。。。。。。。
简单点的有。。。但是没这么长。。。
这题目是英文的。。你看的懂不??
要是看的懂我就把题目发上来
追问那简单一点的有没有快到100的啊?或者100多的,150的都行
追答好吧
你把QQ邮箱给我。。。
我把题目和程序一起给你。。。
这的字数超过了限制
追问[email protected]
追答发给你了。。给我分吧