Node
public class Node {
private int[][]nowState;
private int f;//价值
private int h;//层数
private Node parent;
public Node(int [][]nowState ,int h,Node parent){
this.nowState=nowState;
this.h=h;
this.parent=parent;
}
public int getF() {
return f;
}
public void setF(int g) {
this.f=g+h;
}
public int[][] getNowState() {
return nowState;
}
public void setNowState(int[][] nowState) {
this.nowState = nowState;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
}
Mape
import java.util.ArrayList;
import java.util.List;
public class Mape {
private Node start;
private Node target;
private List open=new ArrayList<>();
private Listclose=new ArrayList<>();
int x,y;//存空格位置
public Mape(int [][]start,int[][]target){
this.start=new Node(start,0,null);
this.target=new Node(target,0,null);
}
public int diffNum(Node p,Node q){
int num=0;
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
if(p.getNowState()[i][j]!=q.getNowState()[i][j])
num++;
}
}
return num;
}
public boolean isEqual(Node p,Node q){
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
if(p.getNowState()[i][j]!=q.getNowState()[i][j])
return false;
}
}
return true;
}
public void findBlank(Node p){
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
if(p.getNowState()[i][j]==0){
x=i;y=j;
}
}
}
}
public boolean isExist(Node p){
for(int i=0;i<close.size();i++){
if(isEqual(p,close.get(i)))
return true;
}
return false;
}
public void addState(Node p){
Node q;
int [][]a;
//对上移操作
a=new int[3][3];
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
a[i][j]=p.getNowState()[i][j];
}
}
findBlank§;
if(x-1>=0){
int tmp=a[x][y];
a[x][y]=a[x-1][y];
a[x-1][y]=tmp;
q=new Node(a,p.getH()+1,p);
if(!isExist(q)){
q.setF(diffNum(q,target));
open.add(q);
}
}
//对下移操作
a=new int[3][3];
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
a[i][j]=p.getNowState()[i][j];
}
}
findBlank§;
if(x+1<=2){
int tmp=a[x][y];
a[x][y]=a[x+1][y];
a[x+1][y]=tmp;
q=new Node(a,p.getH()+1,p);
if(!isExist(q)){
q.setF(diffNum(q,target));
open.add(q);
}
}
//对左移操作
a=new int[3][3];
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
a[i][j]=p.getNowState()[i][j];
}
}
findBlank§;
if(y-1>=0){
int tmp=a[x][y];
a[x][y]=a[x][y-1];
a[x][y-1]=tmp;
q=new Node(a,p.getH()+1,p);
if(!isExist(q)){
q.setF(diffNum(q,target));
open.add(q);
}
}
//对右移操作
a=new int[3][3];
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
a[i][j]=p.getNowState()[i][j];
}
}
findBlank§;
if(y+1<=2){
int tmp=a[x][y];
a[x][y]=a[x][y+1];
a[x][y+1]=tmp;
q=new Node(a,p.getH()+1,p);
if(!isExist(q)){
q.setF(diffNum(q,target));
open.add(q);
}
}
}
public void sort(){
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 aStar(){
open.add(start);
Node p=null;
while(open!=null){
p=open.get(0);
if(isEqual(p,target))
break;
open.remove(0);
close.add§;
addState§;
sort();
}
if(isEqual(p,target)){
Listanswer=new ArrayList<>();
while(p.getParent()!=null){
answer.add§;
p=p.getParent();
}
System.out.println(“运行步骤:”);
for(int i=answer.size()-1;i>=0;i–){
System.out.println(answer.size()-i+"😊;
show(answer.get(i));
}
}
else{
System.out.println(“无结果”);
}
}
public void show(Node p){
for(int i=0;i<p.getNowState().length;i++){
for(int j=0;j<p.getNowState()[i].length;j++){
System.out.print(p.getNowState()[i][j]+" ");
}
System.out.println();
}
}
}
Main
import java.util.Scanner;
public class Main {
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
System.out.println(“起始位置:”);
int [][]origin=new int[3][3];
int [][]purpose=new int[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
origin[i][j]=scanner.nextInt();
System.out.println(“目标位置:”);
for(int i=0;i<3;i++)
for (int j = 0; j < 3; j++)
purpose[i][j]=scanner.nextInt();
Mape mape=new Mape(origin,purpose);
mape.aStar();
}
}