A *, Octa digital

Source: Internet
Author: User

Main search process:
Create two tables. The Open Table stores all the nodes that have been generated but not inspected. The closed table records the accessed nodes.
Traverse each node of the current node, Put N nodes into close, and take the sub-node X of N nodes,-> calculate the estimated value of X->

While (open! = NULL)
{

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

If (N nodes in close) continue;

If (x node = target node) break; // All subnodes
Else
{
If (X in open) compares the estimated values of two X. f // note that the estimated values of two different paths of the same node
If (the estimated value of X is less than the estimated value of Open table)
Update the estimate value in the Open table. // obtain the estimate value of the minimum path ??

If (X in close) compares the estimated values of two X. // note that the estimated values of two different paths of the same node
If (the estimated value of X is less than the estimated value of close table)
Remove X from the close list; put x nodes into open // obtain the estimated value of the minimum path.

If (x not in both)
Evaluate the value of X;
Insert X into the Open table. // No sorting has been performed.

}

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.

}

/*

Through several days of learning heuristic search and solving a corresponding algorithm question, I have a further understanding of the search algorithm.

Thank you for translating these blog masters:

Http://blog.vckbase.com/panic/archive/2005/03/20/3778.aspx

Http://blog.vckbase.com/panic/archive/2005/03/28/4144.html

Http://www.5igis.com/algorithm/shortpath.htm

Http://hi.baidu.com/sonwkuni/blog/item/8be877af32b069cb7dd92a30.html

*/

// I used the STL priority queue and did not update the existing values in the Open table. I just used a new node to add it to the Open table.

// Of course, you can use the minimum heap you have written to maintain this value. If you are interested, refer to here: Click to open the link.

# Include <queue> # include <string> # include <algorithm> # include <cstdio> using namespace STD; struct node {string a; // name int D; // depth, and the G ()'s value int Z; // 0's Weizhi int P; // priority, the F ()'s value friend bool operator <(const node & N1, const node & N2) // The smaller the P value, the higher the priority {return n1.p> n2.p;} node (): D (0), Z (0), P (999999999) {}}; node temp; node now; string start ("123456780 "); // initialization status string end ("12345 6780 "); // the final state int FAC [10] = {1, 1, 2, 6, 24,120,720,504 0, 40320,362 880}; int hash [362881] [2]; // hash table: [] [0] = equal to 0 indicates that the Open table has not been entered. If the value is greater than 0, the table is already in the open list, and its value is F. // [] [1] = greater than 0, which indicates that it is already in the close list, and its value is the position where the F value int dir [9] [4] = {// 0 is located and can be reached after the switch. 9 indicates the location where the data cannot be moved {1, 3, 9, 9 }, // 0 {,}, // 1 {,}, // 2 {,}, // 3 }, // 4 {2, 4, 8, 9}, // 5 {3, 7, 9}, // 6 {4, 6, 8, 9}, // 7 {5, 7, 9, 9 }, // 8}; int XY [9] [2] = {// Set coordinates for each position {1, 1}, // 0 {1, 2}, // 1 {1, 3}, // 2 {2, 1}, // 3 {2, 2}, // 4 {2, 3 }, // 5 {3, 1}, // 6 {3, 2}, // 7 {3, 3}, // 8}; priority_queue <node> q; void Init () {for (INT I = 0; I <9; ++ I) scanf ("% s", & End [I]) ;} int hashindex () // arrange with hash {int result = 0; string TT = temp. a; For (Int J = 1; j <9; ++ J) // starts from the second count {int COUNT = 0; // The number of reverse orders for (int K = 0; k <j; ++ K) // if the preceding value is greater than the following value, the number of reverse orders + 1 {If (TT [J] <TT [k]) + + count ;} result + = count * FAC [J]; // 1- 9 class} return result;} int H () // distance from Manhattan {string a = end; string B = temp. a; int ans = 0; For (INT I = 0; I <9; ++ I) {If (B [I] = '0') continue; for (Int J = 0; j <9; ++ J) {If (B [I] = A [J]) {int xx = xy [I] [0]-XY [J] [0]; If (XX <0) xx =-xx; // | x1-x2 | int YY = xy [I] [1]-XY [J] [1]; If (yy <0) YY =-yy; // | y1-y2 | ans + = (xx + YY); // | x1-x2 | + | y1-y2 | break;} return ans;} bool BFS (int zz, int XX) // 0 position, switched Location {temp = now; temp. A [zz] = temp. A [XX]; temp. A [XX] = '0'; If (temp. A = END) // return true if the target node is found; int isok = hashindex (); temp. d + = 1; temp. z = xx; temp. P = temp. d + H (); If (hash [isok] [1]> 0 & hash [isok] [1]> temp. p) // if it is already in the close list, and the current F value is smaller than the original {// It indicates a better path, because their H values are the same. hash [isok] [1] = 0; // exit Q from the close table. push (temp); Return false;} If (hash [isok] [0] = 0 | hash [isok] [0]> temp. p) // not in the Open table, Or it is already in the Open table, but the current F value is smaller than the original {hash [isok] [0] = temp. p; // enter the Open table or update the value of the original node Q in the Open table. push (temp); Return false ;}} int a_star () {int ans = 0; temp. A = start; temp. D = 0; temp. z = 8; hash [hashindex ()] [0] = 0; // start state temp. P = 0; q. push (temp); While (! Q. empty () {now = Q. top (); q. pop (); temp = now; int nowhashindex = hashindex (); If (hash [nowhashindex] [1]> 0) continue; // skip for (INT I = 0; I <4; ++ I) {If (dir [now. z] [I] <9 & BFS (now. z, dir [now. z] [I]) // if it can be extended {ans = now. d + 1; return ans ;}} hash [nowhashindex] [1] = now. p; // enter the close table} int main () {Init (); printf ("% d \ n", a_star (); Return 0 ;}

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.