隐式图搜索问题
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class EightPuzzle implements Comparable {
private int[] nine = new int[9];
private int f;
private int g;
private int h;
private EightPuzzle parent;
private ArrayList<EightPuzzle> shortpath = new ArrayList<EightPuzzle>();
public int[] getNine() {
return nine;
}
public void setNine(int[] nine) {
this.nine = nine;
}
public int getG() {
return g;
}
public void setG(int g) {
this.g= g;
}
public int getF() {
return f;
}
public void setF(int evaluation) {
this.f = evaluation;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public EightPuzzle getParent() {
return parent;
}
public void setParent(EightPuzzle parent) {
this.parent = parent;
}
public boolean isTarget(EightPuzzle target){
return Arrays.equals(getNine(), target.getNine());
}
public void celF(EightPuzzle target){
int temp = 0;
for(int i=0;i<9;i++){
if(nine[i]!=target.getNine()[i])
temp++;
}
this.setH(temp);
if(this.getParent()==null){
this.setG(0);
}else{
this.g = this.parent.getG()+1;
}
this.setF(this.getG()+this.getH());
}
public boolean isSolvable(EightPuzzle target){
int reverse = 0;
for(int i=0;i<9;i++){
for(int j=0;j<i;j++){//遇到0跳过
if(nine[j]>nine[i] && nine[j]!=0 && nine[i]!= 0)
reverse++;
if(target.getNine()[j]>target.getNine()[i] && target.getNine()[j]!=0 && target.getNine()[i]!=0)
reverse++;
}
}
if(reverse % 2 == 0)
return true;
return false;
}
public int compareTo(Object o) {
EightPuzzle c = (EightPuzzle) o;
return this.f-c.getF();
}
public int getZeroPosition(){
int position = -1;
for(int i=0;i<9;i++){
if(this.nine[i] == 0){
position = i;
}
}
return position;
}
public int isContains(ArrayList<EightPuzzle> open){
for(int i=0; i<open.size(); i++){
if(Arrays.equals(open.get(i).getNine(), getNine())){
return i;
}
}
return -1;
}
public boolean isMoveUp() {
int position = getZeroPosition();
if(position<=2){
return false;
}
return true;
}
public boolean isMoveDown() {
int position = getZeroPosition();
if(position>=6){
return false;
}
return true;
}
public boolean isMoveLeft() {
int position = getZeroPosition();
if(position%3 == 0){
return false;
}
return true;
}
public boolean isMoveRight() {
int position = getZeroPosition();
if((position)%3 == 2){
return false;
}
return true;
}
public EightPuzzle moveUp(int move){
EightPuzzle temp = new EightPuzzle();
int[] tempnum = nine.clone();
temp.setNine(tempnum);
int position = getZeroPosition();
int p=0;
switch(move){
case 0:
p = position-3;
temp.getNine()[position] = nine[p];
break;
case 1:
p = position+3;
temp.getNine()[position] = nine[p];
break;
case 2:
p = position-1;
temp.getNine()[position] = nine[p];
break;
case 3:
p = position+1;
temp.getNine()[position] = nine[p];
break;
}
temp.getNine()[p] = 0;
return temp;
}
public void print(){
for(int i=0;i<9;i++){
if(i%3 == 2){
System.out.println(this.nine[i]);
}else{
System.out.print(this.nine[i]+" ");
}
}
}
public void printRoute(){
EightPuzzle temp = null;
int count = 0;
temp = this;
System.out.println("----------开始移动----------");
while(temp!=null){
shortpath.add(temp);
temp = temp.getParent();
count++;
}
for(int i=shortpath.size()-2 ; i>=0 ; i--){
System.out.println("第"+(shortpath.size()-i-1)+"步");
shortpath.get(i).print();
System.out.println("--------------------");
}
}
public void operation(ArrayList<EightPuzzle> open,ArrayList<EightPuzzle> close,EightPuzzle parent,EightPuzzle target){
if(this.isContains(close) == -1){
int position = this.isContains(open);
if(position == -1){
this.parent = parent;
this.celF(target);
open.add(this);
}else{
if(this.getG() < open.get(position).getG()){
open.remove(position);
this.celF(target);
open.add(this);
}
}
}
}
public static void main(String args[]) {
//定义open表
ArrayList<EightPuzzle> open = new ArrayList<EightPuzzle>();
ArrayList<EightPuzzle> close = new ArrayList<EightPuzzle>();
EightPuzzle start = new EightPuzzle();
EightPuzzle target = new EightPuzzle();
Scanner s = new Scanner(System.in);
int startarray[] = new int[9];
int targetarray[] = new int[9];
System.out.println("请输入初始状态:");
for (int i = 0; i < 9; i++) {
startarray[i] = s.nextInt();
}
System.out.println("请输入目标状态:");
for (int j = 0; j < 9; j++) {
targetarray[j] = s.nextInt();
}
s.close();
start.setNine(startarray);
target.setNine(targetarray);
start.celF(target);
open.add(start);
while (open.isEmpty() == false) {
Collections.sort(open);
EightPuzzle best = open.get(0);
open.remove(0);
close.add(best);
if (best.isTarget(target)) {
//输出
best.printRoute();
}
int move;
if (best.isMoveUp()) {
move = 0;
EightPuzzle up = best.moveUp(move);
up.operation(open, close, best, target);
}
if (best.isMoveDown()) {
move = 1;
EightPuzzle down = best.moveUp(move);
down.operation(open, close, best, target);
}
if (best.isMoveLeft()) {
move = 2;
EightPuzzle left = best.moveUp(move);
left.operation(open, close, best, target);
}
if (best.isMoveRight()) {
move = 3;
EightPuzzle right = best.moveUp(move);
right.operation(open, close, best, target);
}
}
}
}