Implementing the A * algorithm for artificial intelligence with JAVA 8 Digital Problem

xiaoxiao2021-03-06  38

// 8 Digital Class Class Eight {INT E [] = {{2, 8, 3}, {1, 6, 4}, {7, 0, 5}}; // Default Starting State Int Fax , fay; // Save the position INT f in the parent status 0 INT F; // Valuation function value Eight FORMER;

PUBLIC EIGHT () {FAX = -1; fax = -1; f = -1; former = null;}

Public ele h {for (int i = 0; i <3; i ) for (int J = 0; j <3; j ) {e [i] [j] = other.e [i] [j ];} Fax = other.fax; fax = 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 ele ht (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

Public void addelement (EIGHT E) {QE [size] = E; SIZE ;}

Public Boolean Contains (E) {IF (size == 0) Return False; Else {for (INT I = 0; I

Public Boolean ISempty () {if (size == 0) {Return True;} else returnaf;

Public Eight Elementat (int index) {

Return QE [INDEX];

Public void setElementat (EIGHT E, INDEX) {

QE [index] = E;

Public int size () {return size;}

Public int indexof (E) {for (int i = 0; i

Public void remove (EIGHT E) {for (INT i = 0; i

Public void removeallelements () {for (int i = 0; i

}

// 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 ) {= [i] [j ]! = DEST [i] [j]) h ;} return h;} // Generator 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 (EE [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);

}}} F (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);}}} -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, I, I, 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.addeElement (SON4); SWAP (E, I, J, I, N);}

Return Sons;} public static void main (String [] args) {INT depth = 0; // Depth Eight N = new ele h (); Eight Temp1 = new ele ht (), temp2 = new ele ht (); // Open Table Queue Open = new queue (); // Closed Table Queue Closed = New queue (); // Save Son State Table Queue Son = New Queue (); Open.AddeElement (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 0; I -) for (int J = 0; j Temp2.f) {Eight Tq = New Eight (); tq = Open.Elementat J); Open.SETELEMENTAT (Open.Elementat (j 1), j); open.selementat (tq, j 1);}}} // end while system.out.println ("fail!");

Return;} // end main} This program is a A * algorithm in manual intelligence, doing the algorithm in the book. The Queue class is a queue class yourself to implement the OPEN table and the Closed table. It turned out to do it with vector, but later found that the vector saved in the vector is only a reference, the state in the table is also changed, but I have to implement a queue class. Now I know that there is a LinkedList class that can be competent, but the homework is handed over, I am too lazy!

转载请注明原文地址:https://www.9cbs.com/read-78187.html

New Post(0)