資源簡介
用JAVA寫的A*算法實現八數碼問題,能運行。

代碼片段和文件信息
public?class?Eight{
int?g;
????int?e[][]={{283}{164}{705}};
????int?zizj;?????//0的位置
????Eight?former;
????
????public?Eight()
????{
???? g=0;
????????zi=-1;
????????zj=-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];
???? }
???? zi=other.zi;
???? zj=other.zj;
???? former=other.former;
????}
????
????public?void?setFormer(Eight?e){
???? this.former=e;
????}
????public?void?listAll(?Eight?e?){
???? System.out.println(“最優路徑為:“);
???? List?l=new?List();
???? l.insertAtFront(e);
???? while(?e.former?!=?null?){
???? l.insertAtFront(e.former);
???? e?=?new?Eight(e.former);
???? }
???? while(!l.isEmpty()){
???? e=l.getFirstNode();
???? e.print();
???? l.removeFromFront();
???? }
???? return?;
????}
????
????public?boolean?equals(Eight?a)
????{
???? int?i=0;
???? int?j=0;
???? if(a==null)
???? return?false;
???? else?{
???? for(?i?=?0;?i<3;?i++)
???? for(j=0;j<3;?j++){
???? if(a.e[i][j]?!=?this.e[i][j])
???? return?false;
???????? }
????
???? return?true;
???? }
????}
????public?void?Swap(int?iint?jint?mint?n){
int?temp;
temp?=?this.e[i][j];
this.e[i][j]?=?this.e[m][n];
this.e[m][n]?=?temp;
}
??
????public?int?h(){
???? int?dest[][]?=?{{123}{804}{765}};
???? int?h?=0ij;
???? for(i=0;i<3;i++)
???? for(j=0;j<3;j++){
???? if(this.e[i][j]!=dest[i][j]?&&?e[i][j]!=0)
???? h++;
???? }
???? return?h;
????}
????public?int?f(){
???? return?g+h();
????}
????
????public?Eight[]?ex(){
????
???? List?e?=new?List();
???? int?i=0j=0k=0;
???? int?mn;
????
???? boolean?flag?=?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){
???? Eight?a=new?Eight(this);
???? m=i-1;
???? a.Swap(mjij);
???? e.insertAtBack(a);
???? ++k;
???? }
???? if(i+1<3){
???? Eight?a=new?Eight(this);
???? m=i+1;
???? a.Swap(mjij);
???? e.insertAtBack(a);
???? ++k;
???? }
???? if(j-1>=0){
???? Eight?a=new?Eight(this);
???? n=j-1;
???? a.Swap(inij);
???? e.insertAtBack(a);
???? ++k;
???? }
???? if(j+1<3){
???? Eight?a=new?Eight(this);
???? n=j+1;
???? a.Swap(inij);
???? e.insertAtBack(a);
???? ++k;
???? }
???? Eight?b[]=new?Eight[k];
???? for(int?x=0;x ???? b[x]=e.getFirstNode();
???? b[x].setFormer(this);
???? b[x].g=this.g+1;
???? e.removeFromFront();
???? }
???? return?b;
????}
????static?void?sort(Eight?a[]){
???? Eight?temp;
????????for(int?i=0;i ????????????for(int?j=a.length-1;j>i;j--){
??????????????if(a[j].f()???????????? ??temp=a[j-1];
???????????? ??a[j-1]=a[j];
???????????? ??a[j]=temp;
???????????? ??
???????????? ??
??????????????}
????????????}
????????}
????}
????static?void?listSort(List?l){
???? Eight?a[]=new?Eight[l.length];
???? for(int?i=0;i ???? a[i]=l.getFirstNode();
???? l.removeFromFront();
???? }
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4402??2008-12-07?16:56??EightNum\Eight.java
?????文件???????3773??2008-12-07?15:53??EightNum\List.java
?????目錄??????????0??2008-12-15?21:40??EightNum
-----------?---------??----------?-----??----
?????????????????8175????????????????????3
評論
共有 條評論