java

package com.cj;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;



class ba_num implements Comparable {
	ba_num parent;
	int gCost;
	int hCost;
	int[] nums = new int[9];
	static int[] tt = new int[]{1,10,100,1000,10000,100000,1000000,10000000,1000000000};
	
	public ba_num(int[] src)
	{
		for(int i = 0; i<9;i++)
		{
			nums[i] = src[i];
		}
	}
	
	public int getFCost() {
		return gCost + hCost;
	}
	
	public int hashCode()
	{
		int sum = 0;
		for(int i = 0; i<9;i++)
		{
			sum+=nums[i]*tt[i];
		}
		return sum;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ba_num) {
			ba_num n = (ba_num) obj;
			for(int i =0 ;i<9;i++)
			{
				if(this.nums[i] !=n.nums[i])
					return false;
			}
			return true;
		} else {
			return false;
		}
	}
	
	public int getHCost(ba_num node) {

		int num = 0;
		for(int i =0;i<9;i++)
		{
			for(int j=0;j<9;j++)//(this.nums[i]!=node.nums[i])
			{
				if(nums[i]==node.nums[j])
				{
					num += (Math.abs((i-j)%3)+Math.abs((i-j)/3));
				}
			}
		}
		return num;
	}
	
	public ArrayList<ba_num> getNeighbors()
	{
		ArrayList<ba_num> result = new ArrayList<ba_num>();
		int index = 0;
		for(int i =0;i<9;i++)
		{
			if(nums[i]==0)
			{
				index=i;
				break;
			}
		}
		if(index==0||index==1||index==3
				||index==4||index==6||index==7)
		{
			result.add(swap(this,index,index+1));
			
		}
		if(index==1||index==2||index==4
				||index==5||index==7||index==8)
		{
			result.add(swap(this,index,index-1));
			
		}
		if(index==0||index==1||index==2
				||index==3||index==4||index==5)
		{
			result.add(swap(this,index,index+3));
			
		}
		if(index==6||index==7||index==8
				||index==3||index==4||index==5)
		{
			result.add(swap(this,index,index-3));
			
		}
		return result;
	}
	
	public ba_num swap(ba_num old,int i ,int j)
	{
		int[] new_nums = swap(old.nums,i,j);
		return new ba_num(new_nums);
	}
	
	private int[] swap(int[] nums,int i , int j)
	{
		int[] new_nums = new int[nums.length];
		
		for(int k = 0;k<nums.length;k++)
		{
			new_nums[k] = nums[k];
		}
		new_nums[j] = nums[i];
		new_nums[i] = nums[j];
		return new_nums;
	}

	@Override
	public int compareTo(Object other) {
		int thisValue = this.getFCost();
		int otherValue = ((ba_num) other).getFCost();
		int v = thisValue - otherValue;
		return (v > 0) ? 1 : (v < 0) ? -1 : 0;
	}
	
	
}

public class aa {
	
	
	
	public static class PriorityList<ba_num> extends LinkedList {

		public void add(Comparable object) {
			for (int i = 0; i < size(); i++) {
				if (object.compareTo(get(i)) <= 0) {
					add(i, object);
					return;
				}
			}
			addLast(object);
		}
	}
	
	public static int[] getNums()
	{
		Random r = new  Random();
		ArrayList<Integer> result = new ArrayList<Integer>();
		
		for(int n=0,num=r.nextInt(9);n<9;n++)
		{
			while(result.contains(num))
				num = r.nextInt(9);
			result.add(num);
		}
		int[] r1 = new int[result.size()];
		for(int i = 0 ;i<result.size();i++)
		{
			r1[i] = result.get(i);
		}
		return r1;
	}
	
	public static boolean isAcheive(int[] a,int[] b)
	{
		int num1=0,num2=0;
		for(int i = 0 ;i<8;i++)
		{
			for(int j =i+1;j<9;j++)
			{
				if(a[i]>a[j])
					num1++;
				if(b[i]>b[j])
					num2++;
			}
		}
		return (num1-num2)%2==0;
	}
	
	static FileWriter fw;
	static{
		try {
			fw = new FileWriter("c:/aa.text");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void print(int[] a) throws IOException
	{
		fw.write("===================================\r\n");
		fw.write("*\t"+a[0]+"\t"+a[1]+"\t"+a[2]+"\t*\r\n");
		fw.write("*\t"+a[3]+"\t"+a[4]+"\t"+a[5]+"\t*\r\n");
		fw.write("*\t"+a[6]+"\t"+a[7]+"\t"+a[8]+"\t*\r\n");
		fw.write("===================================\r\n");
		fw.flush();
	}
	
	static void printP(ba_num a) throws IOException
	{
		print(a.nums);
		while((a=a.parent)!=null)
		{
			
			fw.write("                ↓              \r\n");
			
			print(a.nums);
		}
		fw.close();
	}
	
	public static void main(String[] args) throws IOException
	{
		PriorityList openList = new PriorityList();
		ArrayList<ba_num> all = new ArrayList<ba_num>();
		LinkedList<ba_num> closedList = new LinkedList<ba_num>();
		int[] src = getNums();
		int[] dest = getNums();
		while(!isAcheive(src,dest))
		{
			print(src);
			
			print(dest);
			fw.write("以上八星不能达到。false\r\n");
			fw.write("\r\n");
			src = getNums();
		}
		
		//src =new int[]{1,2,3,4,5,6,7,8,0};
		//dest = new int[]{1,2,3,0,4,6,7,5,8};
		print(src);
		print(dest);
		fw.write("下面开始转换步骤:\r\n\r\n");
		ba_num src_ba = new ba_num(src);
		
		all.add(src_ba);
		ba_num dest_ba = new ba_num(dest);
		
		src_ba.gCost=0;
		src_ba.hCost = src_ba.getHCost(dest_ba);
		openList.add(src_ba);
		all.add(dest_ba);
		all.add(src_ba);
		while (!openList.isEmpty()) {
			ba_num node = (ba_num) openList.removeFirst();
			if(node.equals(dest_ba))
			{
				printP(node);
				return;
			}
			List neighbors = node.getNeighbors();
			for (int i = 0; i < neighbors.size(); i++) {
				ba_num neighborNode = (ba_num)neighbors.get(i);
				if(all.contains(neighborNode))
				{
					neighborNode = all.get(all.indexOf(neighborNode));
				}else{
					all.add(neighborNode);
					if(all.size()%100==0)
					{
						System.out.println(all.size());
						System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
					}
				}
				boolean isOpen = openList.contains(neighborNode);
				boolean isClosed = closedList.contains(neighborNode);
				
				int costFromStart = node.gCost+ 1;
				if((!isOpen&&!isClosed)
						||costFromStart<neighborNode.gCost)
				{
					if (isClosed) {
						closedList.remove(neighborNode);
					}
					
					neighborNode.parent = node;
					neighborNode.gCost = costFromStart;
					neighborNode.hCost = neighborNode.getHCost(dest_ba);
					
					if (!isOpen) {
						openList.add(neighborNode);
					}
				}
			}
			closedList.add(node);
		}
		
	}
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值