Wannafly Winter Camp 2019 Day4 F 小小马 (思维题)

版权声明:欢迎神犇指教 https://blog.csdn.net/sdxtcqs/article/details/87937878

https://zhixincode.com/contest/16/problem/F
题意:给出一个 n m n*m 的黑白格相间的国际象棋棋盘上,即对于点 ( x , y ) (x,y) ,如果 x , y x,y 的奇偶性相同,则为黑格,否则为白格,给出一个马的起点和终点,问是否存在一条从起点到达终点的黑白相间的路径。马只能走马步,即假设马现在在 ( x , y ) (x,y) 上,它只能跳到 ( x 2 , y 1 ) ( x 2 , y 1 ) , ( x 2 , y + 1 ) ( x 2 , y + 1 ) , ( x 1 , y 2 ) ( x 1 , y 2 ) , ( x 1 , y + 2 ) ( x 1 , y + 2 ) , ( x + 1 , y 2 ) ( x + 1 , y 2 ) , ( x + 1 , y + 2 ) ( x + 1 , y + 2 ) , ( x + 2 , y 1 ) ( x + 2 , y 1 ) , ( x + 2 , y + 1 ) ( x + 2 , y + 1 ) (x-2,y-1)(x−2,y−1), (x-2,y+1)(x−2,y+1), (x-1,y-2)(x−1,y−2), (x-1,y+2)(x−1,y+2), (x+1,y-2)(x+1,y−2), (x+1,y+2)(x+1,y+2), (x+2,y-1)(x+2,y−1), (x+2,y+1)(x+2,y+1) 八个格子。无论什么时候,马都不能跳出棋盘。

这是一道思维题,我们不难发现,走马步达到的位置一定与当前位置不同颜色,那么只要能够从起点合法到达终点,且起点终点颜色不同,就一定符合条件。
而对于能否达到终点,只需考虑一下几种情况:
1.对于一个 3 4 3*4 或者更大的棋盘,马可以从任一一点到达其他所有点。
2.对于 3 3 3*3 的棋盘,只有 ( 2 , 2 ) (2,2) 这个点无法到达。
3.对于 2 m 2*m 的棋盘,需要满足两点,一是起点的横坐标不等于终点横坐标,二是起点的纵坐标与终点的纵坐标距离减 2 2 后是 4 4 的倍数。
4.对于 n 2 n*2 的棋盘,与情况3类似。

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

int n,m,sx,sy,ex,ey;
bool res;
int main()
{
    scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&ex,&ey);
    if(n==1||m==1) res=0;
    else if((sx&1)^(sy&1)==(ex&1)^(ey&1)) res=0;
    else if((n>=3&&m>=4)||(n>=4&&m>=3)) res=1;
    else if(n==3&&m==3)
    {
        if((sx==2&&sy==2)||(ex==2&&ey==2)) res=0;
        else res=1;
    }
    else if(n==2)
    {
        if(sx==ex) res=0;
        else if((abs(sy-ey)-2)%4==0) res=1;
        else res=0;
    }
    else if(m==2)
    {
        if(sy==ey) res=0;
        else if((abs(sx-ex)-2)%4==0) res=1;
        else res=0;
    }
    if(res) printf("Yes\n");
    else printf("No\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdxtcqs/article/details/87937878
今日推荐