|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
手机用到的是用j2me所编出来的小程序。算法|成绩//8数码类
classEight{
inte[][]={{2,8,3},{1,6,4},{7,0,5}};//默许的肇端形态
intfaX,faY;//保留父形态中0的地位
intf;//估价函数值
Eightformer;
publicEight(){
faX=-1;
faY=-1;
f=-1;
former=null;
}
publicEight(Eightother){
for(inti=0;i<3;i++)
for(intj=0;j<3;j++){
e[i][j]=other.e[i][j];
}
faX=other.faX;
faY=other.faY;
f=other.f;
former=other.former;
}
publicvoidprint()
{
for(inti1=0;i1<3;i1++)
for(intj1=0;j1<3;j1++){
System.out.print(e[i1][j1]);
if(j1==2)
System.out.println();
}
System.out.println();
}
publicvoidlistAll(Eighte){
while(e.former!=null){
e.former.print();
e=newEight(e.former);
}
return;
}
}
classQueueextendsObject{//行列类
privateintsize=0;
Eightqe[]=newEight[20];
publicvoidprint(){
for(inti=0;i<size;i++)
qe[i].print();
}
publicvoidaddElement(Eighte){
qe[size]=e;
size++;
}
publicbooleancontains(Eighte){
if(size==0)
returnfalse;
else{
for(inti=0;i<size;i++){
if(qe[i].equals(e))
returntrue;
}
}
returnfalse;
}
publicbooleanisEmpty(){
if(size==0){
returntrue;
}
elsereturnfalse;
}
publicEightelementAt(intindex){
returnqe[index];
}
publicvoidsetElementAt(Eighte,intindex){
qe[index]=e;
}
publicintsize(){
returnsize;
}
publicintindexOf(Eighte){
for(inti=0;i<size;i++){
if(qe[i].equals(e))
returni;
}
return-1;
}
publicvoidremoveFirst(){
for(inti=0;i<size;i++){
qe[i]=qe[i+1];
}
size--;
}
publicvoidremove(Eighte){
for(inti=0;i<size;i++){
if(qe[i].equals(e))
qe[i]=null;
}
size--;
}
publicvoidremoveAllElements(){
for(inti=0;i<size;i++){
qe[i]=null;
}
size=0;
}
}
//算法完成类
publicclassAsearch{
staticintdest[][]={{1,2,3},{8,0,4},{7,6,5}};
staticvoidSwap(Eightee,inti,intj,intm,intn){
inttemp;
temp=ee.e[i][j];
ee.e[i][j]=ee.e[m][n];
ee.e[m][n]=temp;
}
staticintcompare(Eighta){
inth=0,i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++){
if(a.e[i][j]!=dest[i][j])
h++;
}
returnh;
}
//天生子形态
staticQueueborn(Eighte){
intm=1,n=1,i=0,j=0;
booleanflag=true;
Queuesons=newQueue();
for(i=0;i<3&&flag;i++)
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.faX){
Swap(e,m,j,i,j);
//e.print();
Eightson1=newEight(e);
son1.faX=i;
son1.faY=j;
son1.former=e;
sons.addElement(son1);
Swap(e,i,j,m,j);
}
}
if(i+1<3){
m=i+1;
if(m!=e.faX){
Swap(e,m,j,i,j);
//e.print();
Eightson2=newEight(e);
son2.faX=i;
son2.faY=j;
son2.former=e;
sons.addElement(son2);
Swap(e,i,j,m,j);
}
}
if(j-1>=0){
n=j-1;
if(n!=e.faY){
Swap(e,i,n,i,j);
//e.print();
Eightson3=newEight(e);
son3.faX=i;
son3.faY=j;
son3.former=e;
sons.addElement(son3);
Swap(e,i,j,i,n);
}
}
if(j+1<3){
n=j+1;
if(n!=e.faY){
Swap(e,i,n,i,j);
//e.print();
Eightson4=newEight(e);
son4.faX=i;
son4.faY=j;
son4.former=e;
sons.addElement(son4);
Swap(e,i,j,i,n);
}
}
returnsons;
}
publicstaticvoidmain(String[]args){
intdepth=0;//深度
Eightn=newEight();
Eighttemp1=newEight(),temp2=newEight();
//open表
Queueopen=newQueue();
//closed表
Queueclosed=newQueue();
//保留子形态的表
Queueson=newQueue();
open.addElement(n);
while(!open.isEmpty()){
n=open.elementAt(0);
open.removeFirst();
if(compare(n)==0){
n.listAll(n);
System.out.println("Success!");
return;
}
son=born(n);
depth++;
intcount=son.size();
if(count==0)
continue;
elsefor(intt=0;t<count;t++){
temp1=son.elementAt(t);
if(!open.contains(temp1)&&!closed.contains(temp1)){
temp1.f=depth+compare(temp1);
open.addElement(temp1);
}
elseif(open.contains(temp1)){
temp1.f=depth+compare(temp1);
intpos=open.indexOf(son.elementAt(t));
temp2=open.elementAt(pos);
if(temp1.f<temp2.f){
open.setElementAt(temp1,pos);
}
}
elseif(closed.contains(temp1)){
temp1.f=depth+compare(temp1);
intpos=closed.indexOf(temp1);
temp2=closed.elementAt(pos);
if(temp1.f<temp2.f){
closed.remove(son.elementAt(t));
open.addElement(temp1);
}
}
}//endfor
closed.addElement(n);
for(inti=open.size()-1;i>0;i--)
for(intj=0;j<i;j++){
temp1=(Eight)open.elementAt(j);
temp2=(Eight)open.elementAt(j+1);
if(temp1.f>temp2.f){
Eighttq=newEight();
tq=open.elementAt(j);
open.setElementAt(open.elementAt(j+1),j);
open.setElementAt(tq,j+1);
}
}
}//endwhile
System.out.println("Fail!");
return;
}//endmain
}
这个程序是完成野生智能中的A*算法,照着书上的算法做的。Queue类是本人写的一个行列类,用来完成open表和closed表。本来用Vector做的,但厥后发明Vector中保留的只是援用,天生子形态后表中的形态也随着变了,只好本人完成一个行列类。如今晓得另有个LinkedList类能够胜任这项事情,不外功课都交了,我也懒得改了!
java主要分三块,j2se:java的基础核心语言。j2me:java的微型模块,专门针对内存小,没有持续电源等小型设备。j2ee:java的企业模块,专门针对企业数据库服务器的连接维护。 |
|