Using Java to realize a * algorithm in artificial intelligence to find 8 digital problems

Source: Internet
Author: User
Tags contains count implement int size
Algorithm | problem//8 Digital Class
Class eight{
int e[][] = {{2,8,3},{1,6,4},{7,0,5}}; Default starting state
int FaX, FaY; Save 0 of the parent state
int F; Valuation function Value
Eight former;

Public Eight () {
FaX =-1;
Fay=-1;
F=-1;
former = null;
}

Public Eight (eight) {
for (int i = 0; i<3; i++)
for (int j=0;j<3; J + +) {
E[I][J] = Other.e[i][j];
}
FaX = Other.fax;
FaY = Other.fay;
f = other.f;
former = Other.former;
}

public void print ()
{
for (int i1 = 0;i1<3;i1++)
for (int j1=0;j1<3;j1++) {
System.out.print (e[i1][j1]);
if (j1==2)
System.out.println ();
}
System.out.println ();
}

public void Listall (eight e) {
while (E.former!= null) {
E.former.print ();
E = new Eight (e.former);
}
return;
}

}

Class Queue extends object{//Queue class
private int size = 0;
Eight qe[] = new EIGHT[20];

public void print () {
for (int i=0;i<size;i++)
Qe[i].print ();
}

public void AddElement (eight e) {
Qe[size] = e;
size++;
}

Public Boolean contains (eight e) {
if (size = = 0)
return false;
else{
for (int i=0;i<size;i++) {
if (Qe[i].equals (e))
return true;
}
}
return false;
}

public Boolean IsEmpty () {
if (size = = 0) {
return true;
}
else return false;
}

Public eight ElementAt (int index) {

return Qe[index];
}

public void Setelementat (eight E,int index) {

Qe[index] = e;
}

public int size () {
return size;
}

public int indexOf (eight e) {
for (int i = 0; i < size; i++) {
if (Qe[i].equals (e))
return i;
}
return-1;
}

public void Removefirst () {
for (int i=0;i<size;i++) {
Qe[i] = qe[i+1];
}
size--;
}

public void Remove (eight e) {
for (int i = 0; i < size; i++) {
if (Qe[i].equals (e))
Qe[i] = null;
}
size--;
}


public void Removeallelements () {
for (int i = 0; i < size; i++) {
Qe[i] = null;
}
size = 0;
}

}

Algorithm implementation class
public class asearch{
static int dest[][] = {{1,2,3},{8,0,4},{7,6,5}};

static void Swap (eight ee,int i,int j,int m,int N) {
int temp;
temp = Ee.e[i][j];
EE.E[I][J] = Ee.e[m][n];
Ee.e[m][n] = temp;
}


static int Compare (eight a) {
int h =0,i,j;
for (i=0;i<3;i++)
for (j=0;j<3;j++) {
if (A.e[i][j]!=dest[i][j])
h++;
}
return h;
}

Build Child State
Static Queue born (eight e) {
int m=1,n=1,i=0,j=0;
Boolean flag = true;
Queue sons = new Queue ();
for (i=0;i<3&&flag;i++)
for (j=0;j<3&&flag;j++) {
if (e.e[i][j]==0) {
Flag=false;
Break
}
}
i--;
if (i-1>=0) {
M=i-1;
if (M!=e.fax) {
Swap (E,M,J,I,J);
E.print ();

Eight son1 = new Eight (e);
Son1.fax = i;
Son1.fay = j;
Son1.former = e;
Sons.addelement (Son1);
Swap (E,I,J,M,J);

}
}
if (i+1<3) {
m=i+1;
if (M!=e.fax) {
Swap (E,M,J,I,J);
E.print ();
Eight son2 = new Eight (e);
Son2.fax = i;
Son2.fay = j;
Son2.former = e;
Sons.addelement (Son2);
Swap (E,I,J,M,J);
}

}
if (j-1>=0) {
N=j-1;
if (N!=e.fay) {
Swap (E,I,N,I,J);
E.print ();
Eight son3 = new Eight (e);
Son3.fax = i;
Son3.fay = j;
Son3.former = e;
Sons.addelement (Son3);
Swap (E,i,j,i,n);
}

}
if (j+1<3) {
n=j+1;
if (N!=e.fay) {
Swap (E,I,N,I,J);
E.print ();
Eight son4 = new Eight (e);
Son4.fax = i;
Son4.fay = j;
Son4.former = e;
Sons.addelement (SON4);
Swap (E,i,j,i,n);
}

}
return sons;
}
public static void Main (string[] args) {

int depth=0; Depth
Eight n = new Eight ();
Eight temp1 = new Eight (), Temp2 = new Eight ();
Open Table
Queue open = new Queue ();
Closed table
Queue closed = new Queue ();
Table to save child states
Queue son = new queue ();
Open.addelement (n);

while (!open.isempty ()) {
N= open.elementat (0);
Open.removefirst ();
if (compare (n) ==0) {
N.listall (n);
System.out.println ("success!");
Return
}
Son = born (n);
depth++;
int count = Son.size ();
if (count==0)
Continue
else for (int t=0;t<count;t++) {
Temp1 = Son.elementat (t);
if (!open.contains (TEMP1) &&!closed.contains (Temp1)) {
temp1.f = depth + compare (TEMP1);
Open.addelement (TEMP1);
}
else if (Open.contains (Temp1)) {
temp1.f = depth + compare (TEMP1);
int pos = Open.indexof (Son.elementat (t));
Temp2 = Open.elementat (POS);
if (TEMP1.F&LT;TEMP2.F) {
Open.setelementat (Temp1,pos);
}
}
else if (Closed.contains (Temp1)) {
temp1.f = depth + compare (TEMP1);
int pos = Closed.indexof (TEMP1);
Temp2 = Closed.elementat (POS);
if (TEMP1.F&LT;TEMP2.F) {
Closed.remove (Son.elementat (t));
Open.addelement (TEMP1);
}
}
}//end for
Closed.addelement (n);
for (int i=open.size () -1;i>0;i--)
for (int j=0;j<i;j++) {
Temp1 = (eight) open.elementat (j);
Temp2 = (eight) open.elementat (j+1);
if (TEMP1.F&GT;TEMP2.F) {
Eight tq=new eight ();
TQ = Open.elementat (j);
Open.setelementat (Open.elementat (j+1), j);
Open.setelementat (tq,j+1);
}
}
}//end while

System.out.println ("fail!");
Return
}//end Main
}

This program is the implementation of artificial intelligence A * algorithm, according to the book algorithm. The queue class is a queued class that you write to implement the Open table and the closed table. Originally used vector to do, but later found that the vector to save only the reference, after the generation of child state in the table after the state also changed, had to implement a queue class themselves. Now know that there is a LinkedList class can be competent for this work, but the homework has been handed, I do not bother to change!




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.