POJ1657--Distance on Chessboard

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    string s1,s2;
    cin>>t;
    while(t--)
    {
       cin>>s1>>s2;
       int x = abs(s1[0]-s2[0]);
       int y = abs(s1[1]-s2[1]);
       if(x+y==0)
       {
           cout<<"0 0 0 0"<<endl;
           continue;
       }
       if(x>y) cout<<x<<" ";//王只能走一步
       else cout<<y<<" ";
       if(x==0||y==0||x==y)
            cout<<1<<" ";
       else
            cout<<2<<" ";
       if(x==0||y==0)
        cout<<1<<" ";
       else
        cout<<2<<" ";
       if(x==y) cout<<1<<endl;//象在x,y相差为偶数的时候,才能走到
       else if(abs(x-y)%2==0) cout<<2<<endl;
       else cout<<"Inf"<<endl;
        //t--;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

Distance on Chessboard
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 25313 Accepted: 8670

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值