1007:Distance on Chessboard
-
时间限制:
- 1000ms 内存限制:
- 65536kB
-
描述
-
国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间。如下图所示:
王、后、车、象的走子规则如下:
- 王:横、直、斜都可以走,但每步限走一格。
- 后:横、直、斜都可以走,每步格数不受限制。
- 车:横、竖均可以走,不能斜走,格数不限。
- 象:只能斜走,格数不限。
写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。
输入
- 王:横、直、斜都可以走,但每步限走一格。
-
第一行是测试数据的组数t(0 <= t <= 20)。以下每行是一组测试数据,每组包括棋盘上的两个位置,第一个是起始位置,第二个是目标位置。位置用"字母-数字"的形式表示,字母从"a"到"h",数字从"1"到"8"。
输出
- 对输入的每组测试数据,输出王、后、车、象所需的最少步数。如果无法到达,就输出"Inf". 样例输入
-
2 a1 c3 f5 f8
样例输出
-
2 1 2 1 3 1 1 Infimport java.util.Scanner;
本题是细节题,不要忘记Inf这个。。。public class Main { public static void main(String[] args) { Integer n = 0; Scanner sc = new Scanner(System.in); String sn = sc.next(); n = Integer.parseInt(sn); String begin[] = new String[n]; String end[] = new String[n]; for (int i = 0; i < n; i++) { begin[i] = sc.next(); end[i] = sc.next(); } for(int i=0;i<n;i++){ int x = Math.abs(begin[i].charAt(0)-end[i].charAt(0)); int y = Math.abs(begin[i].charAt(1)-end[i].charAt(1)); if(x==0&&y==0) { System.out.println(0+" "+0+" "+0+" "+0); } else{ if(x<y) System.out.print(y+" "); else System.out.print(x+" "); if(x==y||x==0||y==0) System.out.print(1+" "); else System.out.print(2+" "); if(x==0||y==0) System.out.print(1+" "); else System.out.print(2+" "); if(Math.abs(x-y)%2!=0){ System.out.println("Inf"); } else if(x==y) System.out.println(1); else System.out.println(2); } } } }