HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10372    Accepted Submission(s):
6105

Problem 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 file 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.

题目大意:在一个棋盘上(象棋),水平方向a--h竖直方向1--8,每组样例给定两个坐标,求象棋中的 ‘马’ 至少需要几步能够从位置一到达位置二。

解题思路:广搜,因为“马走日” 所以马在一个位置上最多能够走八个位置,定义一个八个位置的数组,每次搜索八个位置即可。

AC代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <stack>
 6 #include <queue>
 7 using namespace std;
 8 int f[10][10];
 9 int a[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; //“马走日”所能够到达的八个位置
10 int change(char ch)  //将a--h转换成对应的1--8
11 {
12     if (ch == ‘a‘)
13         return 1;
14     if (ch == ‘b‘)
15         return 2;
16     if (ch == ‘c‘)
17         return 3;
18     if (ch == ‘d‘)
19         return 4;
20     if (ch == ‘e‘)
21         return 5;
22     if (ch == ‘f‘)
23         return 6;
24     if (ch == ‘g‘)
25         return 7;
26     if (ch == ‘h‘)
27         return 8;
28 }
29 void bfs(int x,int y,int aa,int b)  //广搜
30 {
31     memset(f,0,sizeof(f));
32     queue <int > q;  //也可以定义成一个结构体,这样可以少定义一个队列
33     queue <int > p;
34     q.push(x);
35     p.push(y);
36     f[q.front()][p.front()] = 1;
37     if (x == aa && y == b)  //如果第一个位置和第二个位置相同直接结束
38         return ;
39     while (!q.empty())
40     {
41         int x1 = q.front();
42         int y1 = p.front();
43         int ans = f[x1][y1];
44         for (int i = 0; i < 8; i ++)  //以此判断八个位置
45         {
46             int x2 = x1 + a[i][0];
47             int y2 = y1 + a[i][1];
48             if (x2>0&&x2<=8&&y2>0&&y2<=8&&f[x2][y2]==0)  //注意边界、已经找过的位置不用再找
49             {
50                 q.push(x2);
51                 p.push(y2);
52                 f[x2][y2] = ans+1;
53             }
54             if (x2 == aa && y2 == b)
55                 return ;
56         }
57         q.pop();
58         p.pop();
59     }
60 }
61 int main ()
62 {
63     char ch1[5],ch2[5];
64     int x,y,a,b;
65     while (scanf("%s",ch1)!=EOF)
66     {
67         scanf("%s",ch2);
68         x = ch1[0]-‘a‘+1;
69         a = ch2[0]-‘a‘+1;
70         y = ch1[1] - ‘0‘;
71         b = ch2[1] - ‘0‘;
72         bfs(x,y,a,b);
73         printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,f[a][b]-1);
74     }
75     return 0;
76 }
时间: 2024-10-22 05:20:49

HDU 1372 Knight Moves (bfs)的相关文章

HDU 1372 Knight Moves(bfs)

嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向,然后从起点到终点跑一遍最典型的bfs即可...注意HDU的坑爹输入和输出... AC代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 #include<cstring> 5 6 usi

HDU - 1372 Knight Moves(bfs入门)

HDU - 1372 Knight Moves 题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567 题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法. 输入: 输入包含一个或多个输入样例.每个测试样例将会有两个坐标,表示现在的位置和将要到达的地

HDOJ 题目1372 Knight Moves(BFS)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7551    Accepted Submission(s): 4513 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

poj2243 &amp;amp;&amp;amp; hdu1372 Knight Moves(BFS)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: pid=1372">http://acm.hdu.edu.cn/showproblem.php? pid=1372 Problem Description A friend of you is doing research on t

HDU 1372 Knight Moves【BFS】

题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次[email protected][email protected]) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue&

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB 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

杭电 1372 Knight Moves(广搜模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6439    Accepted Submission(s): 3886 Problem Description A friend of you is doing res