总时间限制: java
1000mspython
内存限制: 测试
65536kBcode
描述内存
国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间。以下图所示:
input
王、后、车、象的走子规则以下:
it
王:横、直、斜均可以走,但每步限走一格。
io
后:横、直、斜均可以走,每步格数不受限制。
class
车:横、竖都可以走,不能斜走,格数不限。
import
象:只能斜走,格数不限。
写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。
输入
第一行是测试数据的组数t(0 <= t <= 20)。如下每行是一组测试数据,每组包括棋盘上的两个位置,第一个是起始位置,第二个是目标位置。位置用"字母-数字"的形式表示,字母从"a"到"h",数字从"1"到"8"。
输出
对输入的每组测试数据,输出王、后、车、象所需的最少步数。若是没法到达,就输出"Inf".
样例输入
2 a1 c3 f5 f8
样例输出
2 1 2 1 3 1 1 Inf
来源
POJ Monthly--2004.05.15 Liu Rujia@POJ
#include <stdio.h> #include <math.h> void main( ){ int nCases, i; scanf("%d", &nCases); for(i = 0; i < nCases; i++){ char begin[5], end[5]; //用begin 和end 分别存储棋子的起止位置。 scanf("%s %s", begin, end); int x, y; //用x 和y 分别存储起止位置之间x 方向和y 方向上的距离。 x = abs(begin[0] - end[0]); y = abs(begin[1] - end[1]); if(x == 0 && y == 0) printf("0 0 0 0\n"); //起止位置相同,全部棋子都走0 步。 else{ if(x < y) printf("%d", y); // 王的步数 else printf("%d", x); if(x == y || x == 0 || y == 0) printf(" 1");// 后的步数 else printf(" 2"); if(x == 0 || y == 0) printf(" 1"); // 车的步数 else printf(" 2"); if(abs(x - y) % 2 != 0) printf(" Inf\n"); // 象的步数 else if(x == y) printf(" 1\n"); else printf(" 2\n"); } } }
package openjudge; import java.util.Scanner; import java.io.BufferedInputStream; public class E1657 { public static void main(String[] args) { int nCases; Scanner in = new Scanner(new BufferedInputStream(System.in)); nCases = in.nextInt(); String begin = null; String end = null; for(int i = 0; i < nCases; i++){ begin = in.next(); end = in.next(); int x = Math.abs(begin.charAt(0) - end.charAt(0)); int y = Math.abs(begin.charAt(1) - end.charAt(1)); if(x == 0 && y ==0) System.out.print("0 0 0 0\n"); 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.print(" Inf\n"); else if(x == y) System.out.print(" 1\n"); else System.out.print(" 2\n"); } } in.close(); } }
# Python 3.2.5 import sys nCases = int(input()) for i in range(nCases): line = str(input()) begin, end = line.split(' ') x = abs(ord(begin[0]) - ord(end[0])) y = abs(ord(begin[1]) - ord(end[1])) if x == 0 and y == 0: sys.stdout.write('0 0 0 0\n') else: if x < y: sys.stdout.write('%d' % y) else: sys.stdout.write('%d' % x) if x == y or x == 0 or y == 0: sys.stdout.write(' 1') else: sys.stdout.write(' 2') if x== 0 or y == 0: sys.stdout.write(' 1') else: sys.stdout.write(' 2') if abs(x - y) % 2 != 0: sys.stdout.write(' Inf\n'); elif x == y: sys.stdout.write(' 1\n') else: sys.stdout.write(' 2\n')
# Jython 2.7 # coding: UTF-8 from java.io import BufferedInputStream from java.util import Scanner from java.lang import System from java.lang import Math jin = Scanner(BufferedInputStream(System.in)) nCases = jin.nextInt() for x in range(nCases): begin = jin.next() end = jin.next() x = Math.abs(ord(begin[0]) - ord(end[0])) y = Math.abs(ord(begin[1]) - ord(end[1])) if x ==0 and y == 0: print '0 0 0 0', else: if x < y: print y, else: print x, if x == y or x == 0 or y == 0: print ' 1', else: print ' 2', if x == 0 or y == 0: print ' 1', else: print ' 2', if Math.abs(x - y) % 2 != 0: print(' Inf') elif x == y: print(' 1') else: print(' 2') jin.close()
解题思路:
这个问题是给定棋盘上的起始位置和终止位置,分别判断王、后、车、象从起始位置到达终止位置须要的步数。首先,王、后、车、象彼此独立,分别考虑就能够了。因此这个题目重点要分析王、后、车、象的行走规则特色,从而推出它们从起点到终点的步数。