隐式图的搜索问题
1.结点类
public class Node {
//存储当前状态的情况
int [][]nowState=new int[3][3];
Node parent=null;
Node child1=null,child2=null,child3=null,child4=null;
private int f;//价值
int h=0;//层数
public Node(int [][]a,Node p,Node c1,Node c2,Node c3,Node c4,int h){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
nowState[i][j]=a[i][j];
}
child1=c1;
child2=c2;
child3=c3;
child4=c4;
this.h=h;
}
public void setF(int g)
{
f=g+h;
}
public int getF(){
return f;
}
}
{
//存储当前状态的情况
int [][]nowState=new int[3][3];
Node parent=null;
Node child1=null,child2=null,child3=null,child4=null;
private int f;//价值
int h=0;//层数
public Node(int [][]a,Node p,Node c1,Node c2,Node c3,Node c4,int h){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
nowState[i][j]=a[i][j];
}
child1=c1;
child2=c2;
child3=c3;
child4=c4;
this.h=h;
}
public void setF(int g)
{
f=g+h;
}
public int getF(){
return f;
}
}
2.A*算法实现类
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Doit {
Node start;
Node target;
List<Node> list=new ArrayList<>();
List<Node> path=new ArrayList<>();
Scanner scanner=new Scanner(System.in);
public Doit(){
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();
start=new Node(origin,null,null,null,null,null,0);
System.out.println("目标位置:");
for(int i=0;i<3;i++)
for (int j = 0; j < 3; j++)
purpose[i][j]=scanner.nextInt();
target=new Node(purpose,null,null,null,null,null,0);
}
//判断是否可行
public boolean isExist(Node a1,Node a2){
int []b1=new int[9];
int []b2=new int[9];
int pd1=0,pd2=0,n1=0,n2=0;
int i,j;
for(i=0;i<3;i++) {
for(j=0;j<3;j++){
b1[n1++]=a1.nowState[i][j];
b2[n2++]=a2.nowState[i][j];
}
}
for(i=0;i<9;i++){
for(j=0;j<i;j++){
if(b1[j]>b1[i]&&b1[j]!=0&&b1[i]!=0)
pd1++;
}
}
for(i=0;i<9;i++){
for(j=0;j<i;j++){
if(b2[j]>b2[i]&&b2[j]!=0&&b2[i]!=0)
pd2++;
}
}
if(pd1%2==pd2%2)
return true;
else
return false;
}
public boolean isEqual(Node a1,Node a2){
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++){
if(a1.nowState[i][j]!=a2.nowState[i][j])
return false;
}
}
return true;
}
public void show(){
System.out.println("移动步骤如下:");
for(int k=path.size()-1;k>=0;k--)
{
System.out.println(path.size()-k+":");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++){
System.out.print(path.get(k).nowState[i][j]+" ");
}
System.out.println();
}
}
}
public void add(Node leaf){
int tmp;
Node p;
int [][]a=new int[3][3];
int [][]b=new int[3][3];
int [][]c=new int[3][3];
int [][]d=new int[3][3];
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++){
a[i][j]=leaf.nowState[i][j];
b[i][j]=leaf.nowState[i][j];
c[i][j]=leaf.nowState[i][j];
d[i][j]=leaf.nowState[i][j];
}
}
int x=0,y=0;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++){
if(a[i][j]==0)
{
x=i;
y=j;
}
}
}
if ((x - 1) >= 0)
{
tmp=a[x][y];
a[x][y]=a[x-1][y];
a[x-1][y]=tmp;
p = new Node(a,null,null,null,null,null,0);
if (!compare(start, p))
{
leaf.child1=p;p.parent=leaf;
p.h=leaf.h+1;
int count=differentNum(p,target);
p.setF(count);
list.add(p);
}
}
if (x + 1 <= 2)
{
tmp=b[x][y];
b[x][y]=b[x+1][y];
b[x+1][y]=tmp;
p = new Node(b,null,null,null,null,null,0);
if (!compare(start, p))
{
leaf.child2=p;p.parent=leaf;
p.h=leaf.h+1;
int count=differentNum(p,target);
p.setF(count);
list.add(p);
}
}
if (y - 1 >= 0)
{
tmp=c[x][y];
c[x][y]=c[x][y-1];
c[x][y-1]=tmp;
p = new Node(c,null,null,null,null,null,0);
if (!compare(start, p))
{
leaf.child3=p;p.parent=leaf;
p.h=leaf.h+1;
int count=differentNum(p,target);
p.setF(count);
list.add(p);
}
}
if (y + 1 <= 2)
{
tmp=d[x][y];
d[x][y]=d[x][y+1];
d[x][y+1]=tmp;
p = new Node(d,null,null,null,null,null,0);
if (!compare(start, p))
{
leaf.child4=p;p.parent=leaf;
p.h=leaf.h+1;
int count=differentNum(p,target);
p.setF(count);
list.add(p);
}
}
}
public boolean compare(Node root,Node p){
if (root != null && isEqual(root,p))return true;
if (root.child1 != null)compare(root.child1, p);
if (root.child2 != null)compare(root.child2, p);
if (root.child3 != null)compare(root.child3, p);
if (root.child4 != null)compare(root.child4, p);
return false;
}
public int differentNum(Node p,Node q){
int count=0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if(p.nowState[i][j]!=q.nowState[i][j])
count++;
return count;
}
public void aStar(){
if (isExist(start,target)) {
Node p=start;
list.add(p);
while(!isEqual(p,target)){
list.remove(0);
add(p);
sort();
p=list.get(0);
}
Node q = p;
while (q != null)
{
path.add(q);
q = q.parent;
}
show();
}
else{
System.out.println("不可行!");
}
}
public void sort(){
Node node;
int left=0,right=list.size()-1;
while(left<right)
{
for(int i=left+1;i<=right;i++){
if(list.get(left).getF()>list.get(i).getF()){
node=list.get(i);
list.set(i,list.get(left));
list.set(left,node);
}
}
left++;
for(int i=right-1;i>=left;i--){
if(list.get(right).getF()<list.get(i).getF()){
node=list.get(i);
list.set(i,list.get(right));
list.set(right,node);
}
}
right--;
}
}
}
3.主类
public class Main {
public static void main(String[]args){
Doit doit=new Doit();
doit.aStar();
}
}