节点类
package Node;
public class Node {
public int [ ] [ ] currentState = new int [ 3 ] [ 3 ] ;
public Node DadState = null;
public Node child1 = null , child2 = null , child3 = null , child4= null;
private int f;
public int h= 0 ;
public Node ( int [ ] [ ] States, Node dad, Node child1, Node child2, Node child3, Node child4, int h) {
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ )
currentState[ i] [ j] = States[ i] [ j] ;
}
this . child1 = child1;
this . child2 = child2;
this . child3 = child3;
this . child4 = child4;
this . h= h;
}
public void setF ( int g)
{
f= g+ h;
}
public int getF ( ) {
return f;
}
}
操作类
package serves;
import Node. Node;
import java. util. ArrayList;
import java. util. List;
import java. util. Scanner;
public class serves {
Node Begin;
Node End;
List< Node> open = new ArrayList < > ( ) ;
List< Node> close = new ArrayList < > ( ) ;
Scanner scanner= new Scanner ( System. in) ;
public serves ( ) {
System. out. println ( "起始位置:" ) ;
int [ ] [ ] startState= new int [ 3 ] [ 3 ] ;
int [ ] [ ] purposeState= new int [ 3 ] [ 3 ] ;
for ( int i= 0 ; i< 3 ; i++ )
for ( int j= 0 ; j< 3 ; j++ ) {
System. out. println ( "请输入你的数字:" ) ;
startState[ i] [ j] = scanner. nextInt ( ) ;
}
Begin = new Node ( startState, null, null, null, null, null, 0 ) ;
System. out. println ( "目标位置:" ) ;
for ( int i= 0 ; i< 3 ; i++ )
for ( int j = 0 ; j < 3 ; j++ ) {
System. out. println ( "请输入你的数字:" ) ;
purposeState[ i] [ j] = scanner. nextInt ( ) ;
}
End = new Node ( purposeState, null, null, null, null, null, 0 ) ;
}
public boolean judgeIfResult ( Node begin, Node end) {
int [ ] oneArray = new int [ 9 ] ;
int [ ] oneArray2 = new int [ 9 ] ;
int inverseNumber1 = 0 , inverseNumber2 = 0 , count1 = 0 , count2 = 0 ;
int i, j;
for ( i= 0 ; i< 3 ; i++ ) {
for ( j= 0 ; j< 3 ; j++ ) {
oneArray[ count1++ ] = begin. currentState[ i] [ j] ;
oneArray2[ count2++ ] = end. currentState[ i] [ j] ;
}
}
for ( i= 0 ; i< 9 ; i++ ) {
for ( j= 0 ; j< i; j++ ) {
if ( oneArray[ j] > oneArray[ i] && oneArray[ j] != 0 && oneArray[ i] != 0 )
inverseNumber1 ++ ;
}
}
for ( i= 0 ; i< 9 ; i++ ) {
for ( j= 0 ; j< i; j++ ) {
if ( oneArray2[ j] > oneArray2[ i] && oneArray2[ j] != 0 && oneArray2[ i] != 0 )
inverseNumber2 ++ ;
}
}
if ( inverseNumber1 % 2 == inverseNumber2 % 2 )
return true ;
else
return false ;
}
public boolean ifFinish ( Node begin, Node end) {
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ ) {
if ( begin. currentState[ i] [ j] != end. currentState[ i] [ j] )
return false ;
}
}
return true ;
}
public void printShow ( ) {
System. out. println ( "移动步骤如下:" ) ;
for ( int count = close. size ( ) - 1 ; count >= 0 ; count-- )
{
System. out. println ( "第" + ( close. size ( ) - count) + "步:" ) ;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ ) {
System. out. print ( close. get ( count) . currentState[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
}
}
public void addState ( Node parent ) {
int count;
Node newState;
int [ ] [ ] twoArray1 = new int [ 3 ] [ 3 ] ;
int [ ] [ ] twoArray2 = new int [ 3 ] [ 3 ] ;
int [ ] [ ] twoArray3 = new int [ 3 ] [ 3 ] ;
int [ ] [ ] twoArray4 = new int [ 3 ] [ 3 ] ;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ ) {
twoArray1[ i] [ j] = parent. currentState[ i] [ j] ;
twoArray2[ i] [ j] = parent. currentState[ i] [ j] ;
twoArray3[ i] [ j] = parent. currentState[ i] [ j] ;
twoArray4[ i] [ j] = parent. currentState[ i] [ j] ;
}
}
int x= 0 , y= 0 ;
for ( int i= 0 ; i< 3 ; i++ ) {
for ( int j= 0 ; j< 3 ; j++ ) {
if ( twoArray1[ i] [ j] == 0 )
{
x= i;
y= j;
}
}
}
if ( x- 1 >= 0 )
{
count = twoArray1[ x] [ y] ;
twoArray1[ x] [ y] = twoArray1[ x- 1 ] [ y] ;
twoArray1[ x- 1 ] [ y] = count;
newState = new Node ( twoArray1, null, null, null, null, null, 0 ) ;
if ( ! compare ( Begin, newState) )
{
parent. child1 = newState;
newState. DadState = parent;
newState. h = parent. h + 1 ;
int count2 = differentNum ( newState, End) ;
newState. setF ( count2) ;
open. add ( newState) ;
}
}
if ( x+ 1 <= 2 )
{
count= twoArray2[ x] [ y] ;
twoArray2[ x] [ y] = twoArray2[ x+ 1 ] [ y] ;
twoArray2[ x+ 1 ] [ y] = count;
newState = new Node ( twoArray2, null, null, null, null, null, 0 ) ;
if ( ! compare ( Begin, newState) )
{
parent. child2= newState; newState. DadState= parent;
newState. h= parent. h+ 1 ;
int count2 = differentNum ( newState, End) ;
newState. setF ( count2) ;
open. add ( newState) ;
}
}
if ( y- 1 >= 0 )
{
count= twoArray3[ x] [ y] ;
twoArray3[ x] [ y] = twoArray3[ x] [ y- 1 ] ;
twoArray3[ x] [ y- 1 ] = count;
newState = new Node ( twoArray3, null, null, null, null, null, 0 ) ;
if ( ! compare ( Begin, newState) )
{
parent. child3= newState; newState. DadState= parent;
newState. h= parent. h+ 1 ;
int count2= differentNum ( newState, End) ;
newState. setF ( count2) ;
open. add ( newState) ;
}
}
if ( y+ 1 <= 2 )
{
count= twoArray4[ x] [ y] ;
twoArray4[ x] [ y] = twoArray4[ x] [ y+ 1 ] ;
twoArray4[ x] [ y+ 1 ] = count;
newState = new Node ( twoArray4, null, null, null, null, null, 0 ) ;
if ( ! compare ( Begin, newState) )
{
parent. child4= newState; newState. DadState= parent;
newState. h= parent. h+ 1 ;
int count2= differentNum ( newState, End) ;
newState. setF ( count2) ;
open. add ( newState) ;
}
}
}
public boolean compare ( Node begin, Node newState) {
if ( begin != null && ifFinish ( begin, newState) )
return true ;
if ( begin. child1 != null)
compare ( begin. child1, newState) ;
if ( begin. child2 != null)
compare ( begin. child2, newState) ;
if ( begin. child3 != null)
compare ( begin. child3, newState) ;
if ( begin. child4 != null)
compare ( begin. child4, newState) ;
return false ;
}
public int differentNum ( Node first, Node second) {
int count= 0 ;
for ( int i = 0 ; i < 3 ; i++ )
for ( int j = 0 ; j < 3 ; j++ )
if ( first. currentState[ i] [ j] != second. currentState[ i] [ j] )
count++ ;
return count;
}
public void openSort ( ) {
Node node;
int left= 0 , right= open. size ( ) - 1 ;
while ( left< right)
{
for ( int i= left+ 1 ; i <= right ; i++ ) {
if ( open. get ( left) . getF ( ) > open. get ( i) . getF ( ) ) {
node = open. get ( i) ;
open. set ( i, open. get ( left) ) ;
open. set ( left, node) ;
}
}
left++ ;
for ( int i= right- 1 ; i >= left ; i-- ) {
if ( open. get ( right) . getF ( ) < open. get ( i) . getF ( ) ) {
node= open. get ( i) ;
open. set ( i, open. get ( right) ) ;
open. set ( right, node) ;
}
}
right-- ;
}
}
public void thisIsStart ( ) {
if ( judgeIfResult ( Begin, End) ) {
Node replace = Begin;
open. add ( replace) ;
while ( ! ifFinish ( replace, End) ) {
open. remove ( 0 ) ;
addState ( replace) ;
openSort ( ) ;
replace = open. get ( 0 ) ;
}
Node willAddClose = replace;
while ( willAddClose != null)
{
close. add ( willAddClose) ;
willAddClose = willAddClose. DadState;
}
printShow ( ) ;
}
else {
System. out. println ( "没有结果!" ) ;
}
}
}
测试
package test;
import serves. serves;
public class test {
public static void main ( String[ ] args) {
serves serves = new serves ( ) ;
serves. thisIsStart ( ) ;
}
}
运行结果