2.2 1657:棋盘上的距离

题目:http://poj.grids.cn/practice/1657

/*2013.06.01
  *尝试了一下直接用文本编辑器来写代码,不用调试直接submit!
  *CE两次、WA三次后,终于AC了!
  *WA的原因第一次为判定后的步数出了些问题
  *然后就是两点相同的时候应该都为0,此程序应特殊判断
*/
#include <iostream>
#include <cstring>

using namespace std ;

const int MAXN = 5;
const int black = 1;
const int white = 2;

//取绝对值
int myabs( int n ){
	return n > 0 ? n : -n;
}

//判断是否为奇数
bool isOdd( int n ){
	return n % 2;
}

//判定所在的格子颜色
int judgeColor( int a , int b ){
	if( isOdd( a ) && isOdd( b ) )
		return black;
	if( isOdd( a ) && !isOdd( b ) )
		return white;
	if( !isOdd( a ) && isOdd( b ) )
		return white;
	if( !isOdd( a ) && !isOdd( b ) )
		return black;
	return -1;//保险起见
}

int main(void){
	int ncase;
	cin >> ncase;
	while(ncase--){
		char beg[MAXN], end[MAXN];
		cin >> beg >> end;
		if( strcmp( beg , end ) == 0 ){
			cout << "0 0 0 0" << endl;
			continue;
		}
		//没有添加异常处理,默认全部输入都是符合规定的!!!
		//把字符串转换为整数的坐标,方便计算
		int x1 = beg[0] - 'a' + 1;
		int y1 = beg[1] - '0';
		int x2 = end[0] - 'a' + 1;
		int y2 = end[1] - '0';
		//计算相对位置
		int disX = myabs(x1 - x2);
		int disY = myabs(y1 - y2);
		//计算王的步数
		int king = disX < disY ? disX : disY;
		king += myabs( disX - disY );
		//计算后的步数
		int queen = 0;
		if( disX == disY ){
			queen = 1;
		}else if( !disX || !disY ){
			queen = 1;
		}else{
			queen = 2;
		}
		//计算车的步数
		int car = ( disX && disY ) ? 2 : 1;
		//计算象的步数
		char elephant[MAXN];
		int s1 = judgeColor( x1 , y1 );
		int s2 = judgeColor( x2 , y2 );
		if( s1 == s2 ){
			if( disX == disY ){
				strcpy( elephant , "1" );
			}else{
				strcpy( elephant , "2" );
			}
		}else{
			strcpy( elephant , "Inf" );
		}
		cout << king << " " << queen << " " << car << " " << elephant << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值