poj 1657 Distance on Chessboard

Distance on Chessboard

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 19434
Accepted: 6745

Description

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

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

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


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

Input

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

Output

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

Sample Input

2
a1 c3
f5 f8

Sample Output

2 1 2 1
3 1 1 Inf
  1: #include<iostream>
  2: #include<cmath>
  3: using namespace std;
  4: 
  5: int main()
  6: {
  7: 	char cs_pos[20];
  8: 	char ce_pos[20];
  9: 	int is_pos[20];
 10: 	int ie_pos[20];
 11: 	int x,y;
 12: 	int num;
 13: 	int i=0;
 14: 
 15: 	cin>>num;
 16: 	for(i=0;i<num;i++)
 17: 		cin>>cs_pos[i]>>is_pos[i]>>ce_pos[i]>>ie_pos[i];
 18: 	for(i=0;i<num;i++)
 19: 	{
 20: 		x=abs((int)(cs_pos[i]-ce_pos[i]));
 21: 		y=abs(is_pos[i]-ie_pos[i]);
 22: 		
 23: 		if(x==0&&y==0)
 24: 			cout<<"0"<<"";
 25: 		else if(x>=y)
 26: 			cout<<x<<"";
 27: 		else if(x<=y)
 28: 			cout<<y<<"";
 29: 		
 30: 		if(x==0&&y==0)
 31: 			cout<<"0"<<"";
 32: 		else if(x==0||y==0||x==y)
 33: 			cout<<"1"<<"";
 34: 		else
 35: 			cout<<"2"<<"";
 36: 		
 37: 		if(x==0&&y==0)
 38: 			cout<<"0"<<"";	
 39: 		else if(x==0||y==0)
 40: 			cout<<"1"<<"";
 41: 		else if(x==0&&y==0)
 42: 			cout<<"0"<<"";
 43: 		else if(x!=0&&y!=0)
 44: 			cout<<"2"<<"";
 45: 			
 46: 		if(x==0&&y==0)
 47: 			cout<<"0"<<"";
 48: 		else if(abs((int)cs_pos[i]-is_pos[i])%2==abs((int)ce_pos[i]-ie_pos[i])%2)
 49: 		{	
 50: 			if(x==y)
 51: 				cout<<"1"<<"";
 52: 			else
 53: 				cout<<"2"<<"";	
 54: 		}
 55: 		else if(abs((int)cs_pos[i]-is_pos[i])%2!=abs((int)ce_pos[i]-ie_pos[i])%2)
 56: 			cout<<"Inf";
 57: 				
 58: 		cout<<endl;		
 59: 	}
 60: 	return 0;
 61: }
 62: 
posted @ 2011-11-21 21:23  w0w0  阅读(139)  评论(0)    收藏  举报