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 ();
}
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 ();
}
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<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<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>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!
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.