POJ1657 Distance on chessboard

Distance on Chessboard
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25623   Accepted: 8757

Description

国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间。如下图所示: 

王、后、车、象的走子规则如下: 
  • 王:横、直、斜都可以走,但每步限走一格。 
  • 后:横、直、斜都可以走,每步格数不受限制。 
  • 车:横、竖均可以走,不能斜走,格数不限。 
  • 象:只能斜走,格数不限。


写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。 

Input

第一行是测试数据的组数t(0 <= t <= 20)。以下每行是一组测试数据,每组包括棋盘上的两个位置,第一个是起始位置,第二个是目标位置。位置用"字母-数字"的形式表示,字母从"a"到"h",数字从"1"到"8"。 

Output

对输入的每组测试数据,输出王、后、车、象所需的最少步数。如果无法到达,就输出"Inf".

Sample Input

2
a1 c3
f5 f8

Sample Output

2 1 2 1
3 1 1 Inf

Source

[Submit]   [Go Back]   [Status]   [Discuss]

 

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int n;
10     cin >> n;
11     while(n--) {
12         char b, e;
13         int B, E;
14         cin >> b >> B >> e >> E;
15         int keen, queen, car, ele;
16         int x, y;
17         x = abs(b - e);
18         y = abs(B - E);
19         if(x == y && y == 0) {
20             keen = 0; queen = 0; car = 0; ele = 0;
21         }
22         else {
23             if(x < y) keen = y;
24             else keen = x;
25             if(x == y || x == 0 || y == 0) queen = 1;
26             else queen = 2;
27             if(x == 0 || y == 0) car = 1;
28             else car = 2;
29             if((x - y) % 2 != 0) ele = -1;
30             else if(x == y) ele = 1;
31             else ele = 2;
32         }
33         if(ele == -1) {
34             printf("%d %d %d Inf
", keen, queen, car);
35         } else {
36             printf("%d %d %d %d
", keen, queen, car, ele);
37         }
38     }
39     return 0;
40 }
View Code

 

 

原文地址:https://www.cnblogs.com/xzrmdx/p/5147549.html