| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12505 | Accepted: 4354 |
Description
王、后、車、象的走子規則如下:
-
王:橫、直、斜都可以走,但每步限走一格。
-
后:橫、直、斜都可以走,每步格數不受限制。
-
車:橫、豎均可以走,不能斜走,格數不限。
- 象:只能斜走,格數不限。
寫一個程序,給定起始位置和目標位置,計算王、后、車、象從起始位置走到目標位置所需的最少步數。
Input
Output
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;
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

