2013
11-10

# Distance on Chessboard

• 王：横、直、斜都可以走，但每步限走一格。
• 后：横、直、斜都可以走，每步格数不受限制。
• 车：横、竖均可以走，不能斜走，格数不限。
• 象：只能斜走，格数不限。

2
a1 c3
f5 f8

2 1 2 1
3 1 1 Inf

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;
}

}

1. 杨姐可是个男的啊啊啊啊啊啊啊啊啊啊啊啊啊啊————————————————————————————————————————————————————————————————————————

2. 嗯 分析得很到位，确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样：push时，比较要push的elem和辅助栈的栈顶，elem<=min.top()，则min.push(elem).否则只要push（elem）就好。在pop的时候，比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();}，否则{stack.pop();}.