poj 2243 Knight Moves
Knight MovesTime
Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10262 Accepted: 5786
Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
Source
Ulm Local 1996
//176K 407MS C++ 1040B //没剪枝的..bfs #include<iostream> #include<queue> using namespace std; int mov[8][2]={1,2,2,1,-1,2,2,-1,1,-2,-2,1,-1,-2,-2,-1}; struct node{ int x,y; int cnt; }; int sx,sy,ex,ey; int bfs() { int vis[10][10]={0}; node t={sx,sy,0}; queue<node>Q; Q.push(t); vis[sx][sy]=1; while(!Q.empty()){ t=Q.front(); Q.pop(); //printf("%d\n",t.cnt); if(t.x==ex && t.y==ey) return t.cnt; for(int i=0;i<8;i++){ node t0=t; t0.x+=mov[i][0]; t0.y+=mov[i][1]; if(t0.x>=0 && t0.x<8 && t0.y>=0 && t0.y<8 && !vis[t0.x][t0.y]){ vis[t0.x][t0.y]=1; t0.cnt++; Q.push(t0); } } } } int main(void) { char a[5],b[5]; while(scanf("%s %s",a,b)!=EOF) { sx=a[0]-'a'; sy=a[1]-'1'; ex=b[0]-'a'; ey=b[1]-'1'; printf("To get from %s to %s takes %d knight moves.\n",a,b,bfs()); } return 0; }
//184K 47MS C++ 1652B //A* 入门练习 .. 可耻的copy别人代码了 //用过才知道,很高效!! #include<iostream> #include<queue> using namespace std; struct node{ int x,y,step; int g,h,f; friend bool operator < (const node &a,const node &b){ //运算符重载 if(a.f==b.f) return a.g<b.g; return a.f>b.f; } }k; bool vis[10][10]; //关闭列表 int x1,x2,y1,y2,ans; int mov[8][2]={1,2,2,1,-1,2,2,-1,1,-2,-2,1,-1,-2,-2,-1}; priority_queue<node>Q; //开启列表 bool in(const node &a) { if(a.x>=0 && a.y>=0 && a.x<8 && a.y<8) return true; return false; } int Heuristic(const node &a) //估价函数 { return (abs(a.x-x2)+abs(a.y-y2))*10; } void Astar() { node t; while(!Q.empty()){ t=Q.top(); Q.pop(); vis[t.x][t.y]=true; if(t.x==x2 && t.y==y2){ ans=t.step; return; } for(int i=0;i<8;i++){ node s=t; s.x+=mov[i][0]; s.y+=mov[i][1]; if(in(s) && !vis[s.x][s.y]){ s.g=t.g+23; s.h=Heuristic(s); s.f=s.g+s.h; s.step++; Q.push(s); } } } } int main(void) { char a[5],b[5]; while(scanf("%s %s",a,b)!=EOF) { x1=a[0]-'a'; y1=a[1]-'1'; x2=b[0]-'a'; y2=b[1]-'1'; memset(vis,false,sizeof(vis)); k.x=x1; k.y=y1; k.g=k.step=0; k.h=Heuristic(k); k.f=k.g+k.h; while(!Q.empty()) Q.pop(); Q.push(k); Astar(); printf("To get from %s to %s takes %d knight moves.\n",a,b,ans); } return 0; }