POJ1657(Distance on Chessboard)

Distance on Chessboard
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12922 Accepted: 4509

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

Source

Problem: 1657
Memory: 288K        Time: 0MS
Language: C
++        Result: Accepted

    
* Source Code

      #include 
<iostream>
      #include 
<string>
      #include 
<cstdlib>
      #include 
<cmath>
      
using namespace std;

      
int main()
      
{
          
int nCase;
          
int x, y;
          
string s, e;
          
          cin
>>nCase;
          
while(nCase--)
          
{
              cin
>>s>>e;
              x 
= abs(s[0- e[0]);
              y 
= abs(s[1- e[1]);

              
if(!&& !y)
              
{
                  cout
<<"0 0 0 0"<<endl;
                  
continue;
              }


              
//====王===
              cout<<max(x, y);
              
              
//===后===
              if(x == y || !|| !y)
                  cout
<<" 1";
              
else
                  cout
<<" 2";

              
//===车===
              if(!|| !y)
                  cout
<<" 1";
              
else
                  cout
<<" 2";

              
//===象===
              if(abs(x - y ) % 2 != 0)
                  cout
<<" Inf"<<endl;
              
else if(x == y)
                  cout
<<" 1"<<endl;
              
else
                  cout
<<" 2"<<endl;
          }

          
return 0;
      }


posted on 2009-05-31 16:35  Xredman  阅读(256)  评论(0编辑  收藏  举报

导航