POJ Knight Moves 2243 x

                  Knight Moves

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13974   Accepted: 7797

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

1)37行是很重要的!好好想想为什么

2)输入的格式get!

代码:

 1 #include<cstdio>
 2 #include<queue>
 3 #include<algorithm>
 4 #include<cstring>
 5
 6 using namespace std;
 7
 8 char s[5];
 9 bool v[9][9];
10 int ex,ey,sx,sy,ans;
11 int dx[8]={2,1,-1,-2,-2,-1,1,2};
12 int dy[8]={-1,-2,-2,-1,1,2,2,1};//八个方向
13
14 struct node
15 {
16     int x,y,step;
17 }cur,nxt;
18
19 queue<node>q;
20
21 void bfs()
22 {
23     if(ex==sx&&ey==sy) //特判起点等于终点 ,找到后进行输出就一定是最小的
24     {
25             printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+‘a‘-1),ey,char(sx+‘a‘-1),sy,0);
26             return;//格式
27     }
28     while(!q.empty()) q.pop();  // 多组数据初始化
29     memset(v,0,sizeof(v));      // 同上
30     cur.x=ex,cur.y=ey; cur.step=0; //起点
31     v[ex][ey]=true; //不要漏了标记起点
32     q.push(cur);
33     while(!q.empty())
34     {
35         cur=q.front();
36         q.pop(); //不要漏了当前出队
37         //v[cur.x][cur.y]=false; 出队,清除标记,是否需要?不需要,为什么?
38         for(int i=0;i<8;i++) //八方位搜索
39         {
40             int xx=cur.x+dx[i],yy=cur.y+dy[i];
41             if(xx>0&&xx<=8&&yy>0&&yy<=8&&!v[xx][yy])
42             {
43                 if(xx==sx&&yy==sy) //找到了,第一个找到的一定就是最近的,why?
44                 {
45                     printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+‘a‘-1),ey,char(sx+‘a‘-1),sy,cur.step+1);
46                     return ;
47                 }
48                 nxt.x=xx, nxt.y=yy; nxt.step=cur.step+1;
49                 v[nxt.x][nxt.y]=true;
50                 q.push(nxt); //扩展出的状态入队
51             }
52         }
53     }
54 }
55
56 int main()
57 {
58     while(scanf("%s",s)!=EOF) //注意输入,scanf读到空格
59     {
60         ex=s[0]-‘a‘+1; ey=s[1]-‘0‘;
61         scanf("%s",s);
62         sx=s[0]-‘a‘+1; sy=s[1]-‘0‘;
63         bfs();
64     }
65 }
时间: 2024-10-20 15:39:08

POJ Knight Moves 2243 x的相关文章

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 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 visit

poj 2243 Knight Moves

Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11794   Accepted: 6646 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 v

POJ 2243:Knight Moves(BFS)

可以理解为象棋中的马走“日”字形,从第一个位置到第二个位置所需的最短步数,简单的BFS 每走一步需判断一次是否到达目标点. 由于BFS写得不多,一直用DFS的思维理解,递归写一直溢出.超时~~ #include"cstdio" #include"iostream" #include"cstring" #include"queue" using namespace std; int dx[8]={-1,1,-2,2,-2,2,-

POJ 1915: Knight Moves

Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21362 Accepted: 9926 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can

POJ 1915 Knight Moves

Knight Moves Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the mini

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

poj 1915 Knight Moves (bfs搜索)

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21919   Accepted: 10223 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

poj 1915 Knight Moves 【双向bfs】

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22121   Accepted: 10332 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast