POJ ACM习题【No.1657】

系统 1477 0
Distance on Chessboard
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12505 Accepted: 4354

Description

国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间。如下图所示:
POJ ACM习题【No.1657】
王、后、车、象的走子规则如下:
  • 王:横、直、斜都可以走,但每步限走一格。
  • 后:横、直、斜都可以走,每步格数不受限制。
  • 车:横、竖均可以走,不能斜走,格数不限。
  • 象:只能斜走,格数不限。


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

Input

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

Output

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

Sample Input

    2
a1 c3
f5 f8
  

 

 

解题思路:

1.判断车的原则是如果X和Y不相同,则是2,否则是1

2.判断王的原则是在|X1-X2|和|Y1-Y2|中取较大值

3.判断象的原则是如果X+Y取2模的值不相等,则为Inf

如果X1+Y2 = X2+Y2,或者X1-X2 = Y1-Y2,则为同一条斜线上,否则为2

4.判断后的原则是如果车为1,或者象为1,则后也是1,否则是2。

 

最后务必记得要判断X1 = X2 && Y1 = Y2的情况

 

 

    import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int num = Integer.valueOf(cin.nextLine()).intValue();
		String[] str = new String[2];
		String a, b;
		int x1, y1, x2, y2 = 0;
		int kr, qr, cr, xr = 0;
		
		for(int i = 0; i < num; i++)
//		while(cin.hasNext())
		{
			str = cin.nextLine().split(" ");
			a = str[0];
			b = str[1];
			x1 = convert(a.charAt(0));
			y1 = Integer.valueOf(a.substring(1)).intValue();
			x2 = convert(b.charAt(0));
			y2 = Integer.valueOf(b.substring(1)).intValue();			
			
			if(x1==x2 && y1==y2)
			{
				System.out.println("0 0 0 0");
				continue;
			}
				
			
			kr = King(x1, y1, x2, y2);
			qr = Queen(x1, y1, x2, y2);
			cr = Che(x1, y1, x2, y2);
			xr = Xiang(x1, y1, x2, y2);
			System.out.print(kr + " " 
					+ qr + " " + cr + " ");
			if(xr == -1)
				System.out.println("Inf");
			else
				System.out.println(xr);
		}

	}
	
	private static int convert(char x)
	{
		return x-96;
	}

	private static int King(int x1, int y1, int x2, int y2)
	{
		int x = Math.abs(x1 - x2);
		int y = Math.abs(y1 - y2);
		if(x > y)
			return x;
		else
			return y;
	}
	
	private static int Queen(int x1, int y1, int x2, int y2)
	{
		if(x1 == x2 || y1 == y2)
			return 1;
		if(directCon(x1, y1, x2, y2) == true)
			return 1;
		else
			return 2;
	}
	
	private static int Che(int x1, int y1, int x2, int y2)
	{
		if(x1 == x2 || y1 == y2)
			return 1;
		else
			return 2;
	}
	
	private static boolean directCon(int x1, int y1, int x2, int y2)
	{
		if((x1+y1) == (x2+y2))
			return true;
		if((x1-x2) == (y1-y2))
			return true;
		return false;
	}
	
	private static int Xiang(int x1, int y1, int x2, int y2)
	{
		if((x1 + y1)%2 != (x2 + y2)%2)
			return -1;
		if(directCon(x1, y1, x2, y2) == true)
			return 1;
		return 2;
	}

}

  

POJ ACM习题【No.1657】


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论