packagecom.Mytest;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.LinkedList;importjava.util.PriorityQueue;importjava.util.Queue;importjava.util.Scanner;importjava.util.Stack;public class test {//A星算法
classEightNumNode{int[][] numMap; //eight num map
int x; //the space coordinate in lateral
int y; //the space coordinate in vertical
int parentID; //the ID of parent
int selfID; //the ID of itself
int g; //the depth in the tree
int h; //the num in the wrong places(or Manhattan Distance)
int f; //f = g+h
}public staticEightNumNode beginStat;public staticEightNumNode endStat;public static ArrayListopenList;public static ArrayListcloseList;public static StackpathStack;public static int idCounter = 0;public int findSpaceX(int[][] M){for(int i=0; i
}public int findSpaceY(int[][] M){for(int i=0; i
}public boolean checkSame(int[][] A, int[][] B){for(int i=0; i
}public void printMap(int[][] M){for(int i=0; i
System.out.print(M[i][j]+" ");
}
System.out.println();
}
}public int countH (int[][] A, int[][] B){//the num in the wrong places
int hSum = 0;for(int i=0; i
hSum++;
}
}
}returnhSum;
}/*public int countH (int[][] A, int[][] B){// Manhattan Distance
int hSum = 0;
for(int i=0; i
for(int j=0; j
if(A[i][j] != B[i][j] && A[i][j] != 0){
for(int m=0; m
for(int n=0; n
if(A[i][j] == B[m][n]){
hSum += Math.abs(i-m)+Math.abs(j-n);
}
}
}
}
}
}
return hSum;
}*/
public int checkInOpen(int[][] checkMap){for(int i=0; i
}return -1;
}public int checkInClose(int[][] checkMap){for(int i=0; i
}return -1;
}public int checkByID(ArrayList checkList, intcheckID){for(int i=0; i
}return -1;
}public void sortedAdd(ArrayListeList, EightNumNode e){int eIndex=0;for(; eIndex
;
eList.add(eIndex, e);
}public voidexpendPointN(EightNumNode par){if(par.y-1 >= 0){//move left
EightNumNode son = newEightNumNode();
son.numMap= new int[3][3];for(int i=0; i
son.numMap[i][j]=par.numMap[i][j];
son.numMap[par.x][par.y]= son.numMap[par.x][par.y-1];
son.numMap[par.x][par.y-1] = 0;
son.x=par.x;
son.y= par.y-1;
son.parentID=par.selfID;
son.g= par.g + 1;
son.h=countH(son.numMap, endStat.numMap);
son.f= son.g +son.h;if(par.parentID == -1){//the root node
son.selfID = idCounter++;
sortedAdd(openList, son);
}else{boolean flag = true;for(int i=0; i
flag= false;if(flag){//not N's parent
int oIndex =checkInOpen(son.numMap);int cIndex =checkInClose(son.numMap);if( oIndex > -1){//in openList
if(son.g
son.selfID=openList.get(oIndex).selfID;
openList.remove(oIndex);
sortedAdd(openList, son);
}
}else if(cIndex > -1){//in closeList
if(son.g
son.selfID=closeList.get(cIndex).selfID;
closeList.remove(cIndex);
sortedAdd(openList, son);
}
}else{//Neither in openList nor in closeList
son.selfID = idCounter++;
sortedAdd(openList, son);
}
}
}
}if(par.x-1 >= 0){//move up
EightNumNode son = newEightNumNode();
son.numMap= new int[3][3];for(int i=0; i
son.numMap[i][j]=par.numMap[i][j];
son.numMap[par.x][par.y]= son.numMap[par.x-1][par.y];
son.numMap[par.x-1][par.y] = 0;
son.x= par.x-1;
son.y=par.y;
son.parentID=par.selfID;
son.g= par.g + 1;
son.h=countH(son.numMap, endStat.numMap);
son.f= son.g +son.h;if(par.parentID == -1){//the root node
son.selfID = idCounter++;
sortedAdd(openList, son);
}else{boolean flag = true;for(int i=0; i
flag= false;if(flag){//not N's parent
int oIndex =checkInOpen(son.numMap);int cIndex =checkInClose(son.numMap);if( oIndex > -1){//in openList
if(son.g
son.selfID=openList.get(oIndex).selfID;
openList.remove(oIndex);
sortedAdd(openList, son);
}
}else if(cIndex > -1){//in closeList
if(son.g
son.selfID=closeList.get(cIndex).selfID;
closeList.remove(cIndex);
sortedAdd(openList, son);
}
}else{//Neither in openList nor in closeList
son.selfID = idCounter++;
sortedAdd(openList, son);
}
}
}
}if(par.y+1 < 3){//move right
EightNumNode son = newEightNumNode();
son.numMap= new int[3][3];for(int i=0; i
son.numMap[i][j]=par.numMap[i][j];
son.numMap[par.x][par.y]= son.numMap[par.x][par.y+1];
son.numMap[par.x][par.y+1] = 0;
son.x=par.x;
son.y= par.y+1;
son.parentID=par.selfID;
son.g= par.g + 1;
son.h=countH(son.numMap, endStat.numMap);
son.f= son.g +son.h;if(par.parentID == -1){//the root node
son.selfID = idCounter++;
sortedAdd(openList, son);
}else{boolean flag = true;for(int i=0; i
flag= false;if(flag){//not N's parent
int oIndex =checkInOpen(son.numMap);int cIndex =checkInClose(son.numMap);if( oIndex > -1){//in openList
if(son.g
son.selfID=openList.get(oIndex).selfID;
openList.remove(oIndex);
sortedAdd(openList, son);
}
}else if(cIndex > -1){//in closeList
if(son.g
son.selfID=closeList.get(cIndex).selfID;
closeList.remove(cIndex);
sortedAdd(openList, son);
}
}else{//Neither in openList nor in closeList
son.selfID = idCounter++;
sortedAdd(openList, son);
}
}
}
}if(par.x+1 < 3){//move down
EightNumNode son = newEightNumNode();
son.numMap= new int[3][3];for(int i=0; i
son.numMap[i][j]=par.numMap[i][j];
son.numMap[par.x][par.y]= son.numMap[par.x+1][par.y];
son.numMap[par.x+1][par.y] = 0;
son.x= par.x+1;
son.y=par.y;
son.parentID=par.selfID;
son.g= par.g + 1;
son.h=countH(son.numMap, endStat.numMap);
son.f= son.g +son.h;if(par.parentID == -1){//the root node
son.selfID = idCounter++;
sortedAdd(openList, son);
}else{boolean flag = true;for(int i=0; i
flag= false;if(flag){//not N's parent
int oIndex =checkInOpen(son.numMap);int cIndex =checkInClose(son.numMap);if( oIndex > -1){//in openList
if(son.g
son.selfID=openList.get(oIndex).selfID;
openList.remove(oIndex);
sortedAdd(openList, son);
}
}else if(cIndex > -1){//in closeList
if(son.g
son.selfID=closeList.get(cIndex).selfID;
closeList.remove(cIndex);
sortedAdd(openList, son);
}
}else{//Neither in openList nor in closeList
son.selfID = idCounter++;
sortedAdd(openList, son);
}
}
}
}
}public void driver(int[][] beginM, int[][] endM){
openList= new ArrayList();
closeList= new ArrayList();
pathStack= new Stack();//set the begin state
beginStat = newEightNumNode();
beginStat.numMap=beginM;
beginStat.x=findSpaceX(beginM);
beginStat.y=findSpaceY(beginM);
beginStat.parentID= -1;
beginStat.selfID= idCounter++;
beginStat.g= 0;
beginStat.h=countH(beginM, endM);
beginStat.f= beginStat.g +beginStat.h;//set the end state
endStat = newEightNumNode();
endStat.numMap=endM;
endStat.x=findSpaceX(endM);
endStat.y=findSpaceY(endM);
endStat.parentID= -1;
sortedAdd(openList, beginStat);
EightNumNode bestNode;while (!openList.isEmpty()) {
bestNode= openList.remove(0);
closeList.add(bestNode);if(checkSame(bestNode.numMap, endStat.numMap)){
System.out.println("Success!!!");
pathStack.push(bestNode);break;
}elseexpendPointN(bestNode);
}if(openList.isEmpty()) {
System.out.println("Fail!!!");
}else{int tempParID =pathStack.peek().parentID;inttempParIndex;
EightNumNode tempNode;while(tempParID != -1){
tempParIndex=checkByID(closeList, tempParID);
tempNode=closeList.get(tempParIndex);
pathStack.push(tempNode);
tempParID=tempNode.parentID;
}
System.out.println("The path from beginning to end:");int i=0;while(!pathStack.empty()){
System.out.println("step "+(i++));
printMap(pathStack.pop().numMap);
System.out.println();
}
}
}public static voidmain(String[] args) {/*int[][] beginMap = { { 2, 8, 3 },
{ 1, 6, 4 },
{ 7, 0, 5 } };
int[][] endMap = { { 1, 2, 3 },
{ 8, 0, 4 },
{ 7, 6, 5 } };*/Scanner scan= newScanner(System.in);
System.out.println("Please input the beginMAP:");int[][] beginMap = new int[3][3];for(int i=0; i
beginMap[i][j]=scan.nextInt();
System.out.println("Please input the endMAP:");int[][] endMap = new int[3][3];for(int i=0; i
endMap[i][j]=scan.nextInt();
test t= newtest();//t.driver(beginMap, endMap);
if(t.checkSame(beginMap, endMap)){
System.out.println("The beginMap and endMap are the same.");
System.out.println("No need to move!");
}else{
t.driver(beginMap, endMap);
}
}
}