Distance on Chessboard POJ - 1657 简单模拟

Distance on Chessboard

  POJ - 1657 

solution:

终点和起点在同一点时所有值都为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);
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值