资源描述:
《A星算法和深度优先》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、A*算法程序publicclassEight{intg;inte[][]={{2,8,3},{1,6,4},{7,0,5}};intzi,zj;//0的位置Eightformer;publicEight(){g=0;zi=-1;zj=-1;former=null;}publicEight(Eightother){for(inti=0;i<3;i++)for(intj=0;j<3;j++){e[i][j]=other.e[i][j];}zi=other.zi;zj=other.zj;former=other.former;}publicvoidsetFormer(Eighte){
2、this.former=e;}publicvoidlistAll(Eighte){System.out.println("最优路径为:");Listl=newList();l.insertAtFront(e);while(e.former!=null){l.insertAtFront(e.former);e=newEight(e.former);}while(!l.isEmpty()){e=l.getFirstNode();e.print();l.removeFromFront();}return;}publicbooleanequals(Eighta){inti=0;intj
3、=0;if(a==null)returnfalse;else{for(i=0;i<3;i++)for(j=0;j<3;j++){if(a.e[i][j]!=this.e[i][j])returnfalse;}returntrue;}}publicvoidSwap(inti,intj,intm,intn){inttemp;temp=this.e[i][j];this.e[i][j]=this.e[m][n];this.e[m][n]=temp;}publicinth(){intdest[][]={{1,2,3},{8,0,4},{7,6,5}};inth=0,i,j;for(i=
4、0;i<3;i++)for(j=0;j<3;j++){if(this.e[i][j]!=dest[i][j]&&e[i][j]!=0)h++;}returnh;}publicintf(){returng+h();}publicEight[]ex(){Liste=newList();inti=0,j=0,k=0;intm,n;booleanflag=true;for(i=0;i<3&&flag;i++)for(j=0;j<3&&flag;j++){if(this.e[i][j]==0){flag=false;break;}}i=i-1;if(i-1>=0){Eighta=newE
5、ight(this);m=i-1;a.Swap(m,j,i,j);e.insertAtBack(a);++k;}if(i+1<3){Eighta=newEight(this);m=i+1;a.Swap(m,j,i,j);e.insertAtBack(a);++k;}if(j-1>=0){Eighta=newEight(this);n=j-1;a.Swap(i,n,i,j);e.insertAtBack(a);++k;}if(j+1<3){Eighta=newEight(this);n=j+1;a.Swap(i,n,i,j);e.insertAtBack(a);++k;}Eigh
6、tb[]=newEight[k];for(intx=0;xi;j--){if(a[j].f()7、voidlistSort(Listl){Eighta[]=newEight[l.length];for(inti=0;i