Distance on Chessboard
POJ - 1657solution:
终点和起点在同一点时所有值都为0;
王:0或是起点和终点的横纵坐标差的最大值;
后:0或是1或是2;
终点在起点的同行或同列或同对角线上时为1,,否则为2;
车:0或是1或是2;
终点在起点的同行或同列上是为1,否则为2;
象:0或是1或是2或是Inf;
终点在起点的对角线上是为1,在同一颜色的格子内为2,若不在同一颜色的格子内,则为Inf;
code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
int start1,start2,end1,end2;
struct yuu
{
int x,y,s;
};
int bfs1(int x,int y)//王
{
return max(abs(x-start2),abs(y-end2));//王起点和终点的横纵坐标差的最大值;
}
int queen(int x,int y)//后
{
if(x==start2||y==end2||abs(x-start2)==abs(y-end2))//同行或同列或同对角线;
return 1;
else return 2;
}
int che(int x,int y)//车
{
if(x==start2||y==end2)//同行或同列;
return 1;
else return 2;
}
int xiang(int x,int y)//象
{
int t=abs(x-start2);
int s=abs(y-end2);
if((t+s)%2)return -1;//不在同一颜色的格子内;
else
{
if(t==s)return 1;//在对角线上;
else return 2;//在同一颜色的格子内,且不在对角线上;
}
}
int main()
{
int n,m;
char a[10],b[10];
scanf("%d",&m);
while(m--)
{
scanf("%s%s",a,b);
start1=a[0]-'a'+1-1;
start2=b[0]-'a'+1-1;
end1=a[1]-'0'-1;
end2=b[1]-'0'-1;
if(start1==start2&&end1==end2)//起点和终点相同时;
{
printf("0 0 0 0\n");
continue;
}
printf("%d ",bfs1(start1,end1));
printf("%d ",queen(start1,end1));
printf("%d ",che(start1,end1));
int p=xiang(start1,end1);
if(p==-1)printf("Inf\n");
else
printf("%d\n",p);
}
}