import java.util.*;
class Eight
{
int e[][] = {{2,3,1},{4,6,8},{0,5,7}};//初始化的8数码
int eX,eY;//记录父节点的0位置
int value;//估价值
Eight former;

public Eight()
{
eX = -1;
eY = -1;
value = -1;
former = null;
}

//带参数数组的构造
public Eight( Eight father)
{
for(int i = 0; i<3; i++)
{
for(int j=0 ;j<3; j++)
{
e[i][j] = father.e[i][j];
}
eX = father.eX;
eY = father.eY;
value = father.value;
former = father.former;
}
}

//打印数组
public void print()
{
for(int i1 = 0;i1<3;i1++)
{
for(int j1=0;j1<3;j1++)
{
System.out.print(e[i1][j1]);
if(j1==2)
System.out.println();
}
System.out.println();
}
}

//一直返回输出所有的父节点的信息
public void listAll( Eight e )
{
while( e.former != null ){
e.former.print();
//e.print();
e = new Eight(e.former);
}
return ;
}
}

//算法实现类
public class Research
{
static int dest[][] = {{1,2,3},{8,0,4},{7,6,5}};

//交换数组中的两个数的值
static void Swap(Eight ee,int i,int j,int m,int n)
{
int temp;
temp = ee.e[i][j];
ee.e[i][j] = ee.e[m][n];
ee.e[m][n] = temp;
}

//返回和目标数组中不相同的数的个数
static int compare(Eight a)
{
int h =0,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a.e[i][j] != dest[i][j])
h++;
}
}
return h;
}

//生成子状态
static ArrayList born(Eight e)
{
int m=1,n=1,i=0,j=0;
boolean flag = true;
ArrayList sons = new ArrayList();
for(i=0; i<3 && flag; i++)                         //找到0所在的位置
{
for(j=0; j<3 && flag; j++)
{
if(e.e[i][j]==0)
{
flag=false;
break;
}
}
}
i--;
if(i-1>=0)
{
m=i-1;
if( m != e.eX )
{
Swap(e,m,j,i,j);
Eight son1 = new Eight(e);
son1.eX = i;
son1.eY = j;
son1.former = e;
sons.add(son1);
Swap(e,i,j,m,j);
}
}
if(i+1<3)
{
m=i+1;
if(m != e.eX)
{
Swap(e,m,j,i,j);
Eight son2 = new Eight(e);
son2.eX = i;
son2.eY = j;
son2.former = e;
sons.add(son2);
Swap(e,i,j,m,j);
}
}
if(j-1>=0)
{
n=j-1;
if(n!=e.eY)
{
Swap(e,i,n,i,j);
Eight son3 = new Eight(e);
son3.eX = i;
son3.eY = j;
son3.former = e;
sons.add(son3);
Swap(e,i,j,i,n);
}
}
if(j+1<3)
{
n=j+1;
if(n!=e.eY)
{
Swap(e,i,n,i,j);
Eight son4 = new Eight(e);
son4.eX = i;
son4.eY = j;
son4.former = e;
sons.add(son4);
Swap(e,i,j,i,n);
}
}
return sons;
}
//主函数
public static void main(String[] args)
{
int depth=0; //深度
Eight n = new Eight() ;
Eight temp1 = new Eight() , temp2 = new Eight() ;
//open表
ArrayList <Eight> open = new ArrayList <Eight> ();
//closed表
ArrayList <Eight> closed = new ArrayList <Eight> ();
//保存子状态的表
ArrayList <Eight> son = new ArrayList <Eight> ();
open.add(n);
while(!open.isEmpty())
{
n= open.get(0);
open.remove(0);
//open.removeFirst( );
if(compare(n)==0)
{
n.listAll(n);
System.out.println("Success!");
return;
}
  son = born(n);
depth++;
int count = son.size();//记录子节点的数目
if(count==0)
continue;
else for(int t=0;t<count;t++)
{
temp1 = son.get(t);
if( !open.contains(temp1) && !closed.contains(temp1))     //比较的节点不在OPEN也不在colsed表中
{
temp1.value = depth + compare(temp1);
open.add(temp1);
}
else if(open.contains(temp1))                                   //比较的节点在open表中
{
temp1.value = depth + compare(temp1);
int pos = open.indexOf(temp1);
temp2 = open.get(pos);
if(temp1.value < temp2.value)
{
open.set(pos,temp1);
}
}
else if(closed.contains(temp1))                                //比较的节点在closed表中
{
temp1.value = depth + compare(temp1);
int pos = closed.indexOf(temp1);
temp2 = closed.get(pos);
if( temp1.value < temp2.value )
{
closed.remove(temp1);
open.add(temp1);
}
}
}
closed.add(n);
for(int i=open.size()-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
temp1 = open.get(j);
temp2 = open.get(j+1);
if(temp1.value > temp2.value)
{
Eight tq=new Eight();
tq = open.get(j);
open.set(j,open.get(j+1));
open.set(j+1,tq);
}
}
}
}//end while
System.out.println("Fail!");
return;
}//end main
}

编译也通过了,可运行不了
哪位高手能指教下错误在什么地方呀?