2017年上海金马五校程序设计竞赛:Problem G : One for You (博弈)

Description

Given a m × n chessboard, a stone is put on the top-left corner (1, 1). Kevin and Bob take turns moving the stone. One can only move the stone one block right or downward and cannot stay in the same block. Kevin goes first and the one who cannot move will lose the game.

However, Bob thinks the game is somehow unfair. So she proposes to get one chance to hack one block to make it inaccessible before the game starts. Notice that she can only hack no more than one block. She can choose any block except top-left corner (1, 1) and bottom-right corner (m, n), and then hack it. After the game starts, both players cannot move stone onto the hacked block.

Help Bob to find out the strategy to choose the block in case of win if there is a way.

Input

There are several test cases.

Each test case contains a line with two integers m, n (2 ≤ m, n ≤ 2,000,000,000).

Output

For each test case, if there is no solution, print "NO", else print "YES" in one line.

Sample Input

2 2
3 4

Sample Output

YES
NO

分析:

给出一个m*n的二维数组,这个二维数组中有一个石子,在(1,1)位置,现在两个人要玩一个移动石子的游戏,移动的时候每次只能向右或向下移动一格。两个人轮换着移动石子 ,当轮到某个人 的时候,如果石子不能够被移动了,这个人就输了。

游戏规定Kevin先移动,Bob后移动,但是Bob觉得这对它不公平,因此现在允许Bob最多可以堵一个方格(不能堵(1,1)位置,也不能堵(m,n)位置),他可以在游戏开始前选择堵一个方格(当然也可以放弃这个堵方格的机会,在原地图上玩),游戏开始后Bob就没权利改变石子的位置了。问给出一个m*n的数组,问Bob能不能赢。

博弈问题。首先移动石子只能向下或向右:则可以很容易得出下面图中结论。

接下来来看,如果移动动石子的总次数是奇数的时候,下图分析怎样去堵格子Bob能赢,从而得出结论:

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

int main()
{
    long long m,n;  ///m行n列
    while(~scanf("%lld%lld",&m,&n))
    {
        long long step = (m-1)+(n-1);
        if(step%2==0)  ///偶数步,不用堵就可以赢
            printf("YES
");
        else
        {
            int a,b;
            if(m<n)  
            {
                a = m; b = n;
            }
            else
            {
                a = n; b = m;
            } 
            ///上面都啰嗦,其实看abs(m-n)就OK
            if(b-a == 1)
                printf("NO
");
            else
                printf("YES
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cmmdc/p/6941224.html