SCAU 2019级寒假训练 L - Bishops

在这里插入图片描述
我写的时候是拿着棋盘图观察了一下,下面是我的一些归纳结论:
在这里插入图片描述

首先必须要得出的结论是,由于是对角线走法,黑白格肯定不能相通,一个坐标在白格上,另一个在黑格上的话,肯定就直接impossible了。
那么,我们怎么知道坐标是对应黑格还是白格呢?
其实,只要两个坐标成对角线关系,那么肯定满足坐标和或者差为一个定值,当对角线方向是左上到右下时,差为定值,当方向是左下到右上时,坐标和为定值。

观察上图,我们发现,只要是在白格上,无论定值是和还是差,该定值恒为偶数,反之,在黑格上,则为奇数

那么我们只用看坐标x和y的和差,就知道他们是在哪种格子上了。

最后一步解决步长问题,无非就是1或者2(因为肯定最麻烦也可以第一步瞬移到另外一个的所在对角线上再走过去),这时候只需要判断是否在一条对角线上就行了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <limits.h>
#include <cstdlib>
using namespace std;
typedef long long ll;
int main()
{
    int kase;
    cin>>kase;
    int num=0;
    while(kase--)
    {
         ll x1,y1,x2,y2;
         scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
         int flag=1;

         ll t1 = x1+y1;
         ll t2 = abs((x1-y1));
         ll t3 = x2 + y2;
         ll t4 = abs((x2-y2));
         ll res;
         if((t1&1)!=(t3&1))
         {
             flag = 0;
         }
         else
         {
               if(x1-y1==x2-y2)
               {
                   if(t1==t3||t2==t4)
                    res = 1;
                    else
                    res = 2;
               }
               else
               {
                   if(t1==t3)
                    res = 1;
                    else
                    res = 2;
               }
         }
         if(flag)
         printf("Case %d: %lld\n",++num,res);
         else
         {printf("Case %d: impossible\n", ++num);}
    }
    return 0;
}

发布了32 篇原创文章 · 获赞 23 · 访问量 1829

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/103999645