POJ 2243 Knight Moves

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.

 

 题目要求比较简单就是给你两个点问你走多少步能到,最好不要用dfs 不要看8*8这么大棋盘超时超死你
 1 #include<iostream>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 struct point
 7 {
 8     int x,y,step;
 9 };
10 bool used[10][10];//标记是否走过
11 int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};//走的方向
12 point _start,_end;//开始位置结束位置
13 int main()
14 {
15     int bfs(point s,point e);
16     char x1,x2;
17     int y1,y2;
18     while(~scanf("%c%d %c%d",&x1,&y1,&x2,&y2))
19     {
20         getchar();
21         _start.x=x1-'a',_start.y=y1-1;
22         _end.x=x2-'a',_end.y=y2-1;
23         _start.step=0;
24         int ans= bfs(_start,_end);
25         printf("To get from %c%d to %c%d takes %d knight moves.\n", x1, y1, x2, y2, ans);
26     }
27     return 0;
28 }
29 int bfs(point s,point e)
30 {
31     if(s.x==e.x&&s.y==e.y)
32         return s.step;
33     memset(used,0,sizeof(used));
34     queue<point>q;//定义队列q
35     point p;//结构体p
36     int dx,dy;
37     while(1)
38     {
39         for(int i=0;i<8;i++)
40         {
41             dx=s.x+dir[i][0];
42             dy=s.y+dir[i][1];
43             if(used[dx][dy]||dx>=8||dy<0||dy>=8||dx<0) continue;//符合要求就加入队列
44             p.x=dx,p.y=dy;
45             p.step=s.step+1;
46             if(p.x==e.x&&p.y==e.y)
47                 return p.step;
48             used[dx][dy]=true;
49             q.push(p);
50         }
51         s=q.front();//把s赋值为队列头
52         q.pop();//把队列头跳出
53     }
54 }

 

posted @ 2015-02-09 15:39  乱齐八糟  阅读(171)  评论(0编辑  收藏  举报