https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 380
After doing this problem, we finally have a little understanding of BFS.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 intmap[Ten][Ten];6typedefstructnode7 {8 intx, y, Z;9NodeintAintBintc) {x = A; y = b; z =C;}Ten node () {} One }queue; A -Queue q[ -]; - the intd[8][2] = { {2,1}, {2, -1}, { -2, -1}, { -2,1}, {1,2}, {1, -2}, { -1,2}, { -1, -2 } }; - intBFsintX1,intY1,intX2,inty2) - { - intmove =0, number =1; + if(x1 = = x2 && y1 = = y2)return 0; -memset (Map,0,sizeof(map)); +q[0] = node (x1, y1,0); A while(Move <Number ) at { -Queue P = q[move++]; - for(intK =0; K <8; k++) - { - intxx = p.x + d[k][0]; - intyy= p.y + d[k][1]; in if(xx = = x2 && yy = = y2)returnP.z +1; - if(!map[xx][yy] && xx>0&& xx<9&& yy>0&& yy <9) to { +MAP[XX][YY] =1; -q[number++] = node (xx, yy, P.z +1); the } * } $ }Panax Notoginseng } - the intMain () + { A Chars1, S2; the intx1, x2, y1, y2; + while(Cin >> S1 >> y1 >> S2 >>y2) - { $X1 = (int) (S1-'a'+1); $x2 = (int) (S2-'a'+1); - intx=BFS (x1, y1, x2, y2); -cout <<"To get from"<<s1<<y1<<" to"<<s2<<y2<<"takes"<<x <<"Knight moves."<<Endl; the } - return 0;Wuyi}
UVa 439 Knight's Movement (BFS)