龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

用java实现人工智能中的A*算法求8数码问题

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
//8数码类 class Eight{ int e[][] = {{2,8,3},{1,6,4},{7,0,5}}; //默认的起始状态 int faX ,faY; //保存父状态中0的位置 int f; //估价函数值 Eight former ; public Eight(){ faX = -1; faY=-1; f=-1; former = null; } public Ei

  //8数码类
  class Eight{
   int e[][] = {{2,8,3},{1,6,4},{7,0,5}}; //默认的起始状态
   int faX ,faY; //保存父状态中0的位置
   int f; //估价函数值
   Eight former ;
  
   public Eight(){
   faX = -1;
   faY=-1;
   f=-1;
   former = null;
   }
  
   public Eight(Eight other){
   for(int i = 0; i<3; i++)
   for(int j=0 ;j<3; j++){
   e[i][j] = other.e[i][j];
   }
   faX = other.faX;
   faY = other.faY;
   f = other.f;
   former = other.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 = new Eight(e.former);
   }
   return ;
   }
  
  }
  
  class Queue extends Object{ //队列类
   private int size = 0;
   Eight qe[] = new Eight[20];
  
   public void print(){
   for(int i=0;i<size;i++)
   qe[i].print();
   }
  
   public void addElement(Eight e){
   qe[size] = e;
   size++;
   }
  
   public boolean contains(Eight e){
   if( size == 0 )
   return false;
   else{
   for(int i=0;i<size;i++){
   if(qe[i].equals(e))
   return true;
   }
   }
   return false;
   }
  
   public boolean isEmpty(){
   if (size == 0) {
   return true;
   }
   else return false;
   }
  
   public Eight elementAt(int index){
  
   return qe[index];
   }
  
   public void setElementAt( Eight e,int index ){
  
   qe[index] = e;
   }
  
   public int size(){
   return size;
   }
  
   public int indexOf (Eight e) {
   for (int i = 0; i < size; i++){
   if (qe[i].equals( e ))
   return i;
   }
   return -1;
   }
  
   public void removeFirst( ){
   for(int i=0;i<size;i++){
   qe[i] = qe[i+1];
   }
   size--;
   }
  
   public void remove( Eight e ){
   for( int i = 0; i < size; i++ ){
   if( qe[i].equals( e ))
   qe[i] = null;
   }
   size--;
   }
  
  
   public void removeAllElements(){
   for (int i = 0; i < size; i++){
   qe[i] = null;
  
精彩图集

赞助商链接