1657:Distance on Chessboard 棋盘上的距离

  • 总时间限制: 

  • 1000ms

  •  
  • 内存限制: 

  • 65536kB

  • 描述

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

    14161741_OPtf.jpg


    王、后、车、象的走子规则如下:

    • 王:横、直、斜都可以走,但每步限走一格。

    • 后:横、直、斜都可以走,每步格数不受限制。

    • 车:横、竖均可以走,不能斜走,格数不限。

    • 象:只能斜走,格数不限。



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

  • 输入

  • 第一行是测试数据的组数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()

解题思路:

这个问题是给定棋盘上的起始位置和终止位置,分别判断王、后、车、象从起始位置到达终止位置需要的步数。首先,王、后、车、象彼此独立,分别考虑就可以了。所以这个题目重点要分析王、后、车、象的行走规则特点,从而推出它们从起点到终点的步数。

转载于:https://my.oschina.net/u/553266/blog/313410

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值