總時間限制: 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()
解題思路:
這個問題是給定棋盤上的起始位置和終止位置,分別判斷王、後、車、象從起始位置到達終止位置須要的步數。首先,王、後、車、象彼此獨立,分別考慮就能夠了。因此這個題目重點要分析王、後、車、象的行走規則特色,從而推出它們從起點到終點的步數。