| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 543 人关注过本帖
标题:[分享]八数码
只看楼主 加入收藏
魔鬼之子
Rank: 1
来 自:地狱之都
等 级:新手上路
帖 子:100
专家分:0
注 册:2007-9-22
结帖率:71.43%
收藏
 问题点数:0 回复次数:0 
[分享]八数码

import java.io.*;
import java.util.*;
public class Eight_figureses{
static int opennumber=0;
static int closenumber=0;
static nodal_point begin_point;
static nodal_point end_point;
static nodal_point[] open_point=new nodal_point[10000];
static nodal_point[] close_point=new nodal_point[10000];
public Eight_figureses(){
}
public void expend(nodal_point point){
nodal_point best=new nodal_point();
int k=-1,g=0;
if(point.enqual(end_point)){
System.out.println("目标已经找到");
this.read(point);
System.out.println("深度为:"+point.hight);
return;
}
else {
System.out.println("------------------------待扩展的节点-----------------");
this.read(point);
point.zero();
System.out.print("空格的位置为:");
if(point.location[0]==0&&point.location[1]==0)
System.out.println("左上角");
else if(point.location[0]==0&&point.location[1]==1)
System.out.println("第一行中间");
else if(point.location[0]==0&&point.location[1]==2)
System.out.println("右上角");
else if(point.location[0]==1&&point.location[1]==0)
System.out.println("第一列中间");
else if(point.location[0]==1&&point.location[1]==1)
System.out.println("正中");
else if(point.location[0]==1&&point.location[1]==2)
System.out.println("第三列中间");
else if(point.location[0]==2&&point.location[1]==0)
System.out.println("左下角");
else if(point.location[0]==2&&point.location[1]==1)
System.out.println("第三行中间");
else if(point.location[0]==2&&point.location[1]==2)
System.out.println("右下角");
point.nosame(point);
System.out.println("扩展节点的深度为:"+point.hight);
System.out.println("扩展节点中的不同点的个数为:"+point.different);
System.out.println("扩展节点的耗散值为:"+(point.hight+point.different));
for(int i=0;i<this.opennumber;i++)
if(open_point[i]==point)
k=i;
if(k!=-1){
for(int i=k;i<this.opennumber-1;i++)
open_point[i]=open_point[i+1];
opennumber--;
close_point[closenumber]=point;
closenumber++;
}
if(point.location[0]==0&&point.location[1]==0)
{
this.zhankai(point,2,0,1,point.location);
this.zhankai(point,3,1,0,point.location);
}
else if(point.location[0]==0&&point.location[1]==1)
{
this.zhankai(point,0,0,0,point.location);
this.zhankai(point,2,0,2,point.location);
this.zhankai(point,3,1,1,point.location);
}
else if(point.location[0]==0&&point.location[1]==2)
{
this.zhankai(point,0,0,1,point.location);
this.zhankai(point,3,1,2,point.location);
}
else if(point.location[0]==1&&point.location[1]==0)
{
this.zhankai(point,1,0,0,point.location);
this.zhankai(point,2,1,1,point.location);
this.zhankai(point,3,2,0,point.location);
}
else if(point.location[0]==1&&point.location[1]==1)
{
this.zhankai(point,0,1,0,point.location);
this.zhankai(point,1,0,1,point.location);
this.zhankai(point,2,1,2,point.location);
this.zhankai(point,3,2,1,point.location);
}
else if(point.location[0]==1&&point.location[1]==2)
{
this.zhankai(point,0,1,1,point.location);
this.zhankai(point,1,0,2,point.location);
this.zhankai(point,3,2,2,point.location);
}
else if(point.location[0]==2&&point.location[1]==0)
{
this.zhankai(point,1,1,0,point.location);
this.zhankai(point,2,2,1,point.location);
}
else if(point.location[0]==2&&point.location[1]==1)
{
this.zhankai(point,0,2,0,point.location);
this.zhankai(point,1,1,1,point.location);
this.zhankai(point,2,2,2,point.location);
}
else if(point.location[0]==2&&point.location[1]==2)
{
this.zhankai(point,0,2,1,point.location);
this.zhankai(point,1,1,2,point.location);
}
best=this.open_point[0];
best.nosame(best);
for(int i=1;i<this.opennumber;i++){
open_point[i].nosame(open_point[i]);
if((best.hight+best.different)>(open_point[i].hight+open_point[i].different))
best=open_point[i];
}
this.expend(best);
}
}
public void zhankai(nodal_point point,int derection,int x,int y,int[] zerolocation){
boolean flag1=true,flag2=true;
nodal_point temp=new nodal_point();
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
temp.figur[i][j]=point.figur[i][j];
int change;
change=temp.figur[zerolocation[0]][zerolocation[1]];
temp.figur[zerolocation[0]][zerolocation[1]]=temp.figur[x][y];
temp.figur[x][y]=change;
for(int i=0;i<this.opennumber;i++)
if(open_point[i].enqual(temp)){
flag1=false;
break;
}
for(int i=0;i<this.closenumber;i++)
if(close_point[i].enqual(temp)){
flag2=false;
break;
}
if(flag1&&flag2) {
if(derection==0){
System.out.println("向左扩展为:");
this.read(temp);
}
else if(derection==1){
System.out.println("向上扩展为:");
this.read(temp);
}
else if(derection==2){
System.out.println("向右扩展为:");
this.read(temp);
}
else if(derection==3){
System.out.println("向下扩展为:");
this.read(temp);
}
temp.hight=point.hight+1;
open_point[opennumber]=temp;
opennumber++;
}
}
public static void read(nodal_point readpoint){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
System.out.print(" "+readpoint.figur[i][j]);
System.out.println();
}
}

public int[] input(){
int[] middle=new int[9];
try{
for(int i=0;i<9;i++){
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
middle[i]=Integer.parseInt(in.readLine());
}
}
catch(IOException e){}
return middle;
}
public static class nodal_point{
int[] location=new int[2];
int[][] figur=new int[3][3];
int hight=0;
int different=0;
public nodal_point(){}
public nodal_point(int[] data){
int k=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
figur[i][j]=data[k];
k++;
}
}
public boolean enqual(nodal_point aim){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(this.figur[i][j]!=aim.figur[i][j])
return false;
return true;
}
public void nosame(nodal_point point){
different=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if((end_point.figur[i][j]!=point.figur[i][j])&&end_point.figur[i][j]!=0) this.different++;

}
public void zero(){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(this.figur[i][j]==0){
this.location[0]=i;
this.location[1]=j;
}
}
}
public static void main(String[] args){
int[] begin=new int[9];
int[] end=new int[9];
Eight_figureses eight=new Eight_figureses();

System.out.println("请输入开始队列:");
begin=eight.input();
System.out.println("请输入目标队列:");
end=eight.input();
eight.begin_point=new nodal_point(begin);
eight.end_point=new nodal_point(end);
eight.open_point[eight.opennumber]=eight.begin_point;
eight.opennumber++;
eight.expend(eight.begin_point);
}
}

搜索更多相关主题的帖子: 数码 分享 
2007-11-17 11:45
快速回复:[分享]八数码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053070 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved