JAVA编程:用java完成野生智能中的A*算法求8数码问...
手机用到的是用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=other.e;
}
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);
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;
publicvoidprint(){
for(inti=0;i<size;i++)
qe.print();
}
publicvoidaddElement(Eighte){
qe=e;
size++;
}
publicbooleancontains(Eighte){
if(size==0)
returnfalse;
else{
for(inti=0;i<size;i++){
if(qe.equals(e))
returntrue;
}
}
returnfalse;
}
publicbooleanisEmpty(){
if(size==0){
returntrue;
}
elsereturnfalse;
}
publicEightelementAt(intindex){
returnqe;
}
publicvoidsetElementAt(Eighte,intindex){
qe=e;
}
publicintsize(){
returnsize;
}
publicintindexOf(Eighte){
for(inti=0;i<size;i++){
if(qe.equals(e))
returni;
}
return-1;
}
publicvoidremoveFirst(){
for(inti=0;i<size;i++){
qe=qe;
}
size--;
}
publicvoidremove(Eighte){
for(inti=0;i<size;i++){
if(qe.equals(e))
qe=null;
}
size--;
}
publicvoidremoveAllElements(){
for(inti=0;i<size;i++){
qe=null;
}
size=0;
}
}
//算法完成类
publicclassAsearch{
staticintdest[][]={{1,2,3},{8,0,4},{7,6,5}};
staticvoidSwap(Eightee,inti,intj,intm,intn){
inttemp;
temp=ee.e;
ee.e=ee.e;
ee.e=temp;
}
staticintcompare(Eighta){
inth=0,i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++){
if(a.e!=dest)
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==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的企业模块,专门针对企业数据库服务器的连接维护。 如果要向java web方向发展也要吧看看《Java web从入门到精通》学完再到《Struts2.0入门到精通》这样你差不多就把代码给学完了。有兴趣可以看一些设计模块和框架的包等等。 是一种使用者不需花费很多时间学习的语言 象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。 另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。 是一种使网页(Web Page)产生生动活泼画面的语言 有时间再研究一下MVC结构(把Model-View-Control分离开的设计思想) 一直感觉JAVA很大,很杂,找不到学习方向,前两天在网上找到了这篇文章,感觉不错,给没有方向的我指了一个方向,先不管对不对,做下来再说。 另外编写和运行Java程序需要JDK(包括JRE),在sun的官方网站上有下载,thinking in java第三版用的JDK版本是1.4,现在流行的版本1.5(sun称作J2SE 5.0,汗),不过听说Bruce的TIJ第四版国外已经出来了,是专门为J2SE 5.0而写的。 象、泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发。 Pet Store.(宠物店)是SUN公司为了演示其J2EE编程规范而推出的开放源码的程序,应该很具有权威性,想学J2EE和EJB的朋友不要 错过了。 是一种使用者不需花费很多时间学习的语言 多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。 不过,每次的执行编译后的字节码需要消耗一定的时间,这同时也在一定程度上降低了 Java 程序的运行效率。 在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。 J2SE开发桌面应用软件比起 VC,VB,DEPHI这些传统开发语言来说,优势好象并不明显。J2ME对于初学者来说,好象又有点深奥,而且一般开发者很难有开发环境。 科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。 还好,SUN提供了Javabean可以把你的JSP中的 Java代码封装起来,便于调用也便于重用。 《JAVA语言程序设计》或《JAVA从入门到精通》这两本书开始学,等你编程有感觉的时候也可以回看一下。《JAVA读书笔记》这本书,因为讲的代码很多,也很容易看懂,涉及到面也到位。是你学习技术巩固的好书,学完后就看看《JAVA编程思想》这本书,找找一个自己写的代码跟书上的代码有什么不一样。
页:
[1]