Example explanation A *

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2243

The title is simple, a 8*8 chessboard, given the coordinates of two points, ask the knight in chess the minimum number of steps required to go from one point to another. The way to go is similar to Chinese chess inside the horse, eight directions.

Generally will be directly using BFS Search, 8*8 Direct search will not explode, now look at the use of a *.

The key to A * is how to choose a suitable h (x), which is to guarantee: H (x) <= D (x, y) + H (Y), as for the reason, see: http://www.cnblogs.com/be-saber/p/4780564.html

For a step of the way, it is a "day" word.

At this point we can choose Euclidean distance, we can find that H (x), D (x, y), H (Y) is the triangular three-side (possibly three points collinear), then the above conditions are certainly satisfied.

The code is as follows:

1#include <cstdio>2#include <iostream>3#include <queue>4#include <cmath>5#include <algorithm>6#include <cstring>7 using namespacestd;8 9 #definePOS Pair<int, int>Ten Const intMAXM =Ten ; One //h (x) take Euclidean distance A POS s, t; - BOOLVIS[MAXM][MAXM]; - floatGX[MAXM][MAXM]; the intSTEP[MAXM][MAXM]; - intdir[8][2] = {{1,2}, {1, -2}, {-1,2}, {-1, -2}, {2,1}, {2, -1}, {-2,1}, {-2, -1}} ; - // - BOOLjudge (POS x) { +     if(X.first <1|| X.first >8|| X.second <1|| X.second >8)return false ; -     return true ; + } A // at floatCaldis (pos x, pos y) { -     returnsqrt ((x.first-y.first) * (X.first-y.first) + (x.second-y.second) * (x.second-y.second)); - } - // - intsolve () { -memset (Vis,false,sizeof(Vis)); inmemset (GX,0,sizeof(GX)); -memset (step,0,sizeof(step)); topriority_queue< pair<float, Pos> >Heap; +      while( !heap.empty ()) Heap.pop (); -Heap.push (Make_pair (-Caldis (S, t), s)) ; theGx[s.first][s.second] =0 ; *      while( !Heap.empty ()) { $         intval =Heap.top (). First;Panax NotoginsengPOS x =Heap.top (). Second; - Heap.pop (); theVis[x.first][x.second] =true ; +         if(X.first = = T.first && X.second = =T.second) { A             returnStep[x.first][x.second]; the         } +          for(inti =0; I <8; i++ ){ - POS u; $U.first = X.first + dir[i][0], U.second = X.second + dir[i][1] ; $             if(Judge (U) &&!Vis[u.first][u.second]) { -Gx[u.first][u.second] = Gx[x.first][x.second] + sqrt (5) ; -Heap.push (Make_pair (-gx[u.first][u.second]-Caldis (U, T), u)) ; theStep[u.first][u.second] = Step[x.first][x.second] +1 ; -             }Wuyi         } the     }  -      return 0 ; Wu } - // About intMain () { $     ////////freopen ("1234.txt", "R", stdin); -     CharA, B, C, D; -      while(SCANF ("%c%c%c%c\n", &a, &b, &c, &d)! =EOF) { -s = Make_pair (A-'a'+1, B-'0') ; At = Make_pair (C-'a'+1, d'0') ; +         intStep =solve (); theprintf"To get from%c%c to%c%c takes%d Knight moves.\n", A, B, C, D, step); -     } $     return 0 ; the}

Example explanation A *

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.