java游戏设计:3×3数字拼图代码_BFSAlgorithm.java

package test;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

public class BFSAlgorithm {

//private static int[] tState = {7,8,5,3,2,6,0,4,1};//初始状态

//private static int[] sState = {1,2,3,4,5,6,7,8,0};//目标状态

//private static int SIZE = 3;//几乘几的拼图

private static int[] tState = {6,1,8,4,10,9,3,7,2,11,14,12,13,0,5,15};//初始状态

private static int[] sState = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};//目标状态

private static int SIZE = 4;//几乘几的拼图

//private static int[] sState = {1,2,3,4,5,6,7,8,25,18,10,12,13,14,15,16,33,17,9,20,21,22,23,24,27,11,19,28,29,30,31,32,26,34,35,36,46,37,38,40,41,42,44,0,52,47,39,48,43,49,50,59,54,61,55,56,57,58,51,45,60,53,62,63};//初始状态

//private static int[] tState = {

//1,2,3,4,5,6,7,8,9,10,

//11,12,13,14,15,16,17,18,19,20,

//21,22,23,24,25,26,27,28,29,30,

//31,32,33,34,35,36,37,38,39,40,

//41,42,43,44,45,46,47,48,49,50,

//51,52,53,54,55,56,57,58,59,60,

//61,62,63,0};

//private static int SIZE = 8;//几乘几的拼图

public static void main(String[] args){

boolean isSolved = false;//是否已经解决

int depth = 0;//步数

int sHashCode = Arrays.hashCode(sState);//起始的hash码

HashMap searchStatusList = new HashMap();//记录所有搜索过的状态的列表

SearchStatus startStatus = new SearchStatus();

startStatus.setDepth(depth);

startStatus.setLastHashCode(0);

startStatus.setState(sState);

startStatus.setNum(-1);

searchStatusList.put(sHashCode, startStatus);

ArrayList openList = new ArrayList();//要搜索的列表

openList.add(sState);

while(!isSolved){

depth++;

int blankPosition = -1;

ArrayList tempList = new ArrayList();

outer:

for(int check=0;check

for(int i=0;i

if(openList.get(check)[i]==0){

blankPosition = i;

break;

}

}

int lastHashCode = Arrays.hashCode(openList.get(check));

int[] canMove = GetCanMove(blankPosition);//得到可移动的位置数组

for(int i=0;i<4;i++){

if(canMove[i] != -1){//可以移动

int[] childState = openList.get(check).clone();//移动完的状态

childState[blankPosition] = childState[canMove[i]];

childState[canMove[i]] = 0;

int childHashCode = Arrays.hashCode(childState);//得到子状态(移动完的状态)的hashCode值

if(!searchStatusList.containsKey(childHashCode)){//如果不存在该值,则插入进去,留作比较

SearchStatus childStatus = new SearchStatus();

childStatus.setDepth(depth);

childStatus.setState(childState);

childStatus.setNum(childState[blankPosition]);

childStatus.setLastHashCode(lastHashCode);

searchStatusList.put(childHashCode, childStatus);

tempList.add(childState);

}

if(Arrays.equals(childState,tState)){

isSolved = true;

break outer;

}

}

}

}

openList.clear();

openList = tempList;

}

ArrayList listMoved = new ArrayList();

int tHashCode = Arrays.hashCode(tState);

SearchStatus checkSearchStatus = (SearchStatus) searchStatusList.get(tHashCode);

int checkHashCode = 0;

while(sHashCode!=checkHashCode){

listMoved.add(checkSearchStatus);

checkHashCode = checkSearchStatus.getLastHashCode();

checkSearchStatus = (SearchStatus) searchStatusList.get(checkHashCode);

}

System.out.println(depth + "步");

ArrayList listResult = new ArrayList();

for(int i=listMoved.size()-1;i>=0;i--){

listResult.add(listMoved.get(i).getNum());

System.out.println(Arrays.toString(listMoved.get(i).getState()));

}

System.out.println(listResult.toString());

}

//private static double GetHashCode(int[] sArray)

// {

//double hash = 0d;

// for (int i = 0; i < sArray.length; i++)

// hash += (double)(sArray[i] * Math.pow(10, sArray.length - i - 1));

// return hash;

// }

private static int[] GetCanMove(int blankPosition){

int[] canMove = {-1,-1,-1,-1};//上、下、左、右

if((blankPosition % SIZE)!=0 && (blankPosition % SIZE)!=(SIZE-1)){

canMove[2] = blankPosition - 1;//左边的数字可以移动

canMove[3] = blankPosition + 1;//右边的数字可以移动

}else if((blankPosition % SIZE) == 0){

canMove[3] = blankPosition + 1;//右边的数字可以移动

}else if((blankPosition % SIZE) == (SIZE-1)){

canMove[2] = blankPosition - 1;//左边的数字可以移动

}

if((blankPosition - SIZE) >= 0){

canMove[0] = blankPosition - SIZE;//上边的数字可以移动

}

if((blankPosition + SIZE) < tState.length){

canMove[1] = blankPosition + SIZE;//下边的数字可以移动

}

return canMove;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值