A * Algorithm for Solving 8 digital Problems

Source: Internet
Author: User

The Eight Digital problems that have plagued me for many days have finally been solved. At one time, I didn't know how to solve the Eight Digital problems. Many of the online problems were solved using the * algorithm, but the versions have their own merits, I have looked at the code of each version for a while, but I am also confused. I have been studying the * Algorithm for the past two days and want to use an instance to study the * problem, in this way, if we can effectively solve typical 8 digital problems, we can also improve ourselves. Most of the versions I can see on the Internet are c ++, And I want to implement it myself through java. After all, it is much easier to implement algorithms using Java and at the same time it is easy to understand.

I want to explain the solution to the 8 digital problem from the following aspects:

1. Understand A * Algorithm

2 A * pseudo-code of the Algorithm

3. How to apply the * algorithm to 8 digital problems (that is, to establish a suitable heuristic algorithm)

4 8 digital problems are quite special. You need to determine whether feasible solutions are available.

5. Implement the application of the * Algorithm in 8 digital Problems

1. Understand A * Algorithm

I read a lot of articles on the Internet and found that a * algorithm is well described. The connection is as follows: A * Algorithm

1. Starting from status A, and storing it as a pending vertex into an "enable list ". Enabling a list is like a shopping list. Although there is only one element in the list, it will be added later. Your path may pass other States it contains, or it may not. Basically, this is a list of squares to be checked.
2. Search for the statuses that can be reached by moving the start point up, down, left, and right around the start point. They are also added to the Enable list. Save status A as the "parent square" for all these states ". When we want to describe the path, the parent square information is very important. It will be explained later.
3. Delete status A from the Enable list and add it to a "close list ".

4. Retrieve the state with the lowest fvalue in the enabled list and determine the nature of the State.

(1) If the gvalue in this status appears in the Enable list, it is determined that the gvalue is passed through the current node (removed from open to expand the node) whether the gvalue brought by the parent node is smaller than the original parent node. If the value is smaller than the original value, you need to change its parent node and gvalue.

(2) If the gvalue in this status appears in the close list, it is determined that the gvalue is passed through the current node (removed from open to expand the node) whether the gvalue brought by the parent node is smaller than the original parent node. If the value is smaller than the previous value, delete the node from the closed list, add it to the enabled list, and change its parent node to the current node.

(3) The list is neither enabled nor closed, so it is very easy to add directly to the enabled list (keep the enabled list in ascending order)

(4) If the extracted node is the target node, it is successfully exited.

2 A * pseudo-code of the Algorithm

Create two tables. The Open Table stores all the nodes that have been generated but not inspected. The closed table records the accessed nodes.

Calculates the estimated value of the start point;

Place the start point in the Open table;

While (open! = NULL)

{

Obtain the node N with the smallest value of F from the Open table;

If (N node = target node ){

Break;

}

For (current node n
(X)

{

Calculate the estimated value of X;

If (X in open)

{

If (the value of X is less than the value of X in the Open Table ){

Set n to the father of X;

Update the estimate value in the Open table; // obtain the estimate value of the minimum path.

}

}

If (x inclose ){

If (the estimated value of X is less than the estimated value of X in the close table ){

Set n to the father of X;


Remove this node from the close table

Put x nodes into open // obtain the estimated value of the minimum path

}

}

If (x not inboth ){

Set n to the father of X;

Evaluate the value of X;

Insert X into the Open table. // sort open in ascending order.

}

} // End

Insert n nodes into the close table;

Sort the nodes in the Open table according to the estimated value. // It is actually the size of node F in the Open table, which is performed downward from the node in the smallest path.

} // End while (open! = NULL)

Save path, that is, from the end point, each node moves along the parent node until the start point, this is your path;


3. How to apply the * algorithm to 8 digital problems (that is, to establish a suitable heuristic algorithm)

A * algorithm has a formula f (x) = g (x) + h (x)

Where g (x) is the number of steps consumed from the initial state to the current state, and h (x) is an inspiration value to estimate the number of steps required from the current state to the target State, generally, h (x) is less than or equal to the actual number of required steps, so that the optimal solution is not ignored, because h (x) has some relationship with the space, if h (x) if you set more steps than the actual number of required steps, the solution space may ignore the optimal solution. For example, the width-first search is the effect of h (x) = 0, and the depth-first search is the effect of g (x) = 0.However, the distance between h (x) and H * (x) [actual number of required steps] cannot be too large; otherwise, h (x) will not be too capable of distinguishing, the algorithm efficiency is not very high. A good evaluation of h (x) is: h (x) at H * (N)[Actual number of required steps]And try to be close to H * (N)[Actual number of required steps].

 
Then, 8 digital problem g (x) is the number that goes through the upper, lower, left, and left spaces to get the number of steps required for the new State. h (x) is the distance between the Current State and the target State, is the sum of all numbers not in the target position, must be less than H * (X)

4 8 digital problems are quite special. You need to determine whether feasible solutions are available.

The general solution on the Internet is to judge by odd permutation and even arrangement of the reverse order number. Here we can see the reverse order number introduction.

Only when the parity of the target State is the same as the parity of the initial state can the conversion be performed. I still need to check the specific principle.

5. Implement the application of the * Algorithm in 8 digital Problems

Import Java. util. arraylist; import Java. util. comparator; import Java. util. iterator; import Java. util. list; import Java. util. priorityqueue; public class astar {public static void main (string ARGs []) {int [] [] startstatus = {2, 3, 1}, {5, 0, 8}, {4, 6, 7 }}; // start status int [] [] endstatus = {, 3}, {, 6}, {, 0 }}; // end status // int [] [] endstatus = {2, 3, 1}, {5, 8, 7}, {4, 6, 0 }}; astardoer test = new astardoer (startstatus, endstatus ); Test. run () ;}} class astardoer {int [] [] startstatus; int [] [] endstatus; nodecomparator CMP = new nodecomparator (); priorityqueue <node> open = new priorityqueue <node> (1000000, CMP); priorityqueue <node> close = new priorityqueue <node> (1000000, CMP ); public astardoer (INT [] [] startstatus, int [] [] endstatus) {This. startstatus = new int [3] [3]; this. endstatus = new int [3] [3]; for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; J ++) {This. startstatus [I] [J] = startstatus [I] [J]; this. endstatus [I] [J] = endstatus [I] [J] ;}} private int getreverse (INT [] [] Status) // obtain the parity of the reverse order {int reverse = 0; For (INT I = 0; I <9; I ++) {for (Int J = I + 1; j <9; j ++) {int K = I/3; int M = I % 3; if (status [k] [m]> status [J/3] [J % 3]) {reverse + = 1 ;}} return reverse ;} private Boolean check (INT [] [] startstatus, int [] [] endstatus) {// determine whether a feasible solution exists: int gets = 0; int Gete = 0; gets = getreverse (S Tartstatus); Gete = getreverse (endstatus); If (gets % 2) = (Gete % 2) {return true;} return false;} private void initstart () {// Add the Start Node to the open list node startnode = new node (startstatus); startnode. gvalue = 0; startnode. parent = NULL; startnode. hvalue = startnode. geth (endstatus); startnode. fvalue = startnode. gvalue + startnode. hvalue; open. add (startnode);} private Boolean isinlist (node lnode, node newnodeparent) {// judge this node Whether or not it has already appeared before. The previously appeared G value must be smaller than the current G value, because the while (newnodeparent! = NULL) {If (lnode. equal (newnodeparent. status) {return true;} newnodeparent = newnodeparent. parent;} return false;} private void initchild (node newnode, list <node> newnodechild) {// generate the subnode int whitespace = 0; whitespace = newnode. getwhitespace ();/* Get the space position to determine the Left shift to generate the node */If (whitespace % 3 )! = 2) {node lnode = new node (newnode. status); lnode. status [whitespace/3] [whitespace % 3] = lnode. status [whitespace/3] [(whitespace % 3) + 1]; lnode. status [whitespace/3] [(whitespace % 3) + 1] = 0; If (isinlist (lnode, newnode. parent) = false) {lnode. parent = newnode; lnode. gvalue = newnode. gvalue + 1; lnode. hvalue = lnode. geth (endstatus); lnode. fvalue = lnode. gvalue + lnode. hvalue; newnodechild. add (lnode) ;}}/* Get the space position to determine the right shift to generate the node */I F (whitespace % 3 )! = 0) {node lnode = new node (newnode. status); lnode. status [whitespace/3] [whitespace % 3] = lnode. status [whitespace/3] [(whitespace % 3)-1]; lnode. status [whitespace/3] [(whitespace % 3)-1] = 0; If (isinlist (lnode, newnode. parent) = false) {lnode. parent = newnode; lnode. gvalue = newnode. gvalue + 1; lnode. hvalue = lnode. geth (endstatus); lnode. fvalue = lnode. gvalue + lnode. hvalue; newnodechild. add (lnode) ;}}/* obtain the space position to determine the node generated by moving up */I F (whitespace/3 )! = 2) {node lnode = new node (newnode. status); lnode. status [whitespace/3] [whitespace % 3] = lnode. status [whitespace/3 + 1] [whitespace % 3]; lnode. status [whitespace/3 + 1] [whitespace % 3] = 0; If (isinlist (lnode, newnode. parent) = false) {lnode. parent = newnode; lnode. gvalue = newnode. gvalue + 1; lnode. hvalue = lnode. geth (endstatus); lnode. fvalue = lnode. gvalue + lnode. hvalue; newnodechild. add (lnode) ;}}/* Get the space position to determine if the node is moved down */If (W Hitespace/3 )! = 0) {node lnode = new node (newnode. status); lnode. status [whitespace/3] [whitespace % 3] = lnode. status [whitespace/3-1] [whitespace % 3]; lnode. status [whitespace/3-1] [whitespace % 3] = 0; If (isinlist (lnode, newnode. parent) = false) {lnode. parent = newnode; lnode. gvalue = newnode. gvalue + 1; lnode. hvalue = lnode. geth (endstatus); lnode. fvalue = lnode. gvalue + lnode. hvalue; newnodechild. add (lnode) ;}} private Boolean isopen Orclosein (node newnode, priorityqueue <node> openlist) {iterator <node> iter = openlist. iterator (); While (ITER. hasnext () {node iternode = ITER. next (); If (iternode. equal (newnode. status) {return true ;}} return false;} public void run () {If (check (startstatus, endstatus) = false) {system. out. println ("can not change to that"); return;} initstart (); While (open. isempty () = false) {node newnode = open. poll (); close. add (n Ewnode); If (newnode. Equal (endstatus) {int I = 0; system. Out. println ("Success finish this task ~~~~~ "); While (newnode! = NULL) {for (Int J = 0; j <9; j ++) {system. out. print (newnode. status [J/3] [J % 3]); system. out. print (""); If (J % 3 = 2) {system. out. println () ;}} system. out. println ("Step" + I + "status fvalue" + newnode. fvalue); newnode = newnode. parent; I + = 1;} break;} List <node> newnodechild = new arraylist <node> (); initchild (newnode, newnodechild); For (node ITER: newnodechild) {If (isopenorclosein (ITER, open) = true) {iterator <node> iternew = Open. iterator (); While (iternew. hasnext () {node iternode = iternew. next (); If (iternode. equal (ITER. status) {If (ITER. gvalue <iternode. gvalue) {iternode. parent = ITER. parent; iternode. gvalue = ITER. gvalue; iternode. fvalue = ITER. fvalue ;}}} else if (isopenorclosein (ITER, close) = true) {iterator <node> iternew = close. iterator (); While (iternew. hasnext () {node iternode = iternew. next (); If (iternode. equal (ITER. stat US) {If (ITER. gvalue <iternode. gvalue) {iternode. parent = ITER. parent; iternode. gvalue = ITER. gvalue; iternode. fvalue = ITER. fvalue; close. remove (iternode); open. add (ITER) ;}}} else {open. add (ITER) ;}}} class nodecomparator implements comparator <node >{@ override public int compare (node X, node y) {return X. fvalue-y. fvalue ;}} class node {node parent; Public int [] [] status = new int [3] [3]; int fvalue; I NT gvalue; int hvalue; Public node (INT [] [] Status) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {This. status [I] [J] = status [I] [J] ;}} public void setg (INT gvalue) {This. gvalue = gvalue;} public int getwhitespace () {int K = 0; For (INT I = 0; I <9; I ++) {If (status [I/3] [I % 3] = 0) {k = I; return K ;}} return K ;} public Boolean equal (INT [] [] endstatus) {for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {If (status [I] [J]! = Endstatus [I] [J]) {return false ;}} return true;} public int geth (INT [] [] endstatus) {int K = 0; for (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {If (status [I] [J]! = Endstatus [I] [J]) {k + = 1 ;}} return K ;}}

The above is my Analysis and Solution to the 8 digital problem. If there is a problem, I hope you can communicate with each other and finally solve a big problem. We will start to learn new things later ~

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.