[Codeforces 3A]Shortest path of the king
本来以为是欧式距离啥玩意乱搞。。。然后其实是bfs暴搜。。。

1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdlib> 6 #include <queue> 7 #include <string> 8 #include <vector> 9 using namespace std; 10 11 struct NODE{ 12 int dis,x,y; 13 vector<pair<int,int> > path; 14 NODE(int d,int _x,int _y){dis=d;x=_x;y=_y;} 15 }; 16 17 queue<NODE>q; 18 char a[10],b[10]; 19 bool v[100][100]; 20 int mv[10][10]={ 21 {0,1}, 22 {1,0}, 23 {-1,0}, 24 {0,-1}, 25 {1,-1}, 26 {-1,1}, 27 {1,1}, 28 {-1,-1} 29 }; 30 int main(){ 31 scanf("%s%s",a,b); 32 a[0]-='a'; 33 a[1]-='0'; 34 b[0]-='a'; 35 b[1]-='0'; 36 a[0]++,b[0]++; 37 v[a[0]][a[1]]=1; 38 q.push(NODE(0,a[0],a[1])); 39 while(q.size()){ 40 NODE top=q.front();q.pop(); 41 if(top.x==b[0]&&top.y==b[1]){ 42 printf("%d\n",top.dis); 43 for(int i=0;i<top.path.size();i++){ 44 if(top.path[i].first==1)putchar('R'); 45 if(top.path[i].first==-1)putchar('L'); 46 if(top.path[i].second==1)putchar('U'); 47 if(top.path[i].second==-1)putchar('D'); 48 putchar('\n'); 49 } 50 } 51 for(int i=0;i<8;i++){ 52 int newx=top.x+mv[i][0]; 53 int newy=top.y+mv[i][1]; 54 if(1<=newx&&newx<=8&&1<=newy&&newy<=8&&!v[newx][newy]){ 55 v[newx][newy]=1; 56 NODE tmp=top; 57 tmp.path.push_back(make_pair(mv[i][0],mv[i][1])); 58 tmp.x=newx; 59 tmp.y=newy; 60 tmp.dis++; 61 q.push(tmp); 62 } 63 } 64 } 65 }