#include <iostream>#include<cstdio>#include<string>#include<string.h>#include<math.h>#include<queue>#include<map>#include<algorithm>using namespacestd;intDx,dy;structnode{intstep; intx; inty; FriendBOOL operator<(node A,node b) {returnA.step>B.step; }};intdir[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{2,-1},{1,-2}};p riority_queue<node>Q;intBFs () {ints,d; while(!Q.empty ()) {Node Temp=Q.top (); Q.pop (); if(DX==TEMP.X&&DY==TEMP.Y) {returnTemp.step;} for(intI=0;i<8; i++) {s=temp.x+dir[i][0]; D=temp.y+dir[i][1]; if(s>0&&s<9&&d>0&&d<9) {node Next; Next.x=s; Next.y=D; Next.step=temp.step+1; Q.push (next); } } } return 0;}intMain () {intA,b,ss; Charf,g; while(cin>>f>>b>>g>>dy) {a=f-'a'+1; DX=g-'a'+1; while(!q.empty ()) Q.pop (); node n; N.x=A; N.Y=b; N.step=0; Q.push (n); SS=BFS (); printf ("To get from%c%d to%c%d takes%d Knight moves.\n", F,B,G,DY,SS); } return 0; }
UVA 439 BFS Knight's movement