Knight moves
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6458 accepted submission (s): 3900
The start position and end point are given on an 8x8 chessboard. In fact, the server guard can only output the Shortest Path size according to the requirements of chess.
Simple BFS direct Template
# Include <iostream> # include <cstdio> # include <queue> # include <cstring> # include <algorithm> using namespace STD; int ya, Yb, vis [10] [10]; int ans; char a [3], B [3]; struct node {int A, B, depth; // number of steps taken by the depth record}; int d [8] [2] = {-,-, 2,-1, 1,-2, -1,-2,-2,-1}; void BFS (int x, int y) {int I; node T, P; queue <node> q; T. A = x; T. B = y; T. depth = 0; vis [T. a] [T. b] = 1; q. push (t); While (! Q. empty () {T = Q. front (); q. pop (); If (T. A = ya & T. B = Yb) {printf ("to get from % s to % s takes % d knight moves. \ n ", a, B, T. depth); Return ;}for (I = 0; I <8; I ++) {P. A = T. A + d [I] [0]; p. B = T. B + d [I] [1]; If (P. a> 0 & P. A <= 8 & P. b> 0 & P. B <= 8 &&! Vis [p. a] [p. b]) {P. depth = T. depth + 1; q. push (p) ;}}} int main () {int XA, XB; while (~ Scanf ("% S % s", & A, & B) {ans = 0; XA = A [0]-'A' + 1; ya = B [0]-'A' + 1; XB = A [1]-'0'; Yb = B [1]-'0'; memset (VIS, 0, sizeof vis); BFS (XA, XB);} return 0 ;}
HDU 1372 knight moves