zoukankan      html  css  js  c++  java
  • HDU

    题目

    Problem Description

    Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes the checkerboard game.The size of the chesserboard is n*m.First of all, a coin is placed in the top right corner(1,m). Each time one people can move the coin into the left, the underneath or the left-underneath blank space.The person who can't make a move will lose the game. kiki plays it with ZZ.The game always starts with kiki. If both play perfectly, who will win the game?
     
    Input
    Input contains multiple test cases. Each line contains two integer n, m (0<n,m<=2000). The input is terminated when n=0 and m=0.
     
    Output
    If kiki wins the game printf "Wonderful!", else "What a pity!".
     
    Sample Input
    5 3
    5 4
    6 6
    0 0
     
    Sample Output
    What a pity!
    Wonderful!
    Wonderful!
     
    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2147

    题意

    一个n*m大小的棋盘,右上角放一个硬币,每一次操作是把硬币向下,左或者左下向移动一格,两人轮流操作,首先不能移动硬币的玩家失败。(kiki先行)

    题解

    由于硬币只能向左下进行移动,所以可以看做,在进行一次移动操作后就是一场交换过先后手的新比赛。如n*m的棋假如kiki向左下走一步之后,就相当于另一场以zzki先手的棋盘大小为(n-1)*(m-1)的新比赛。

    所以当n>1,m>1,时每一步都会衍生出3个小的状态。

    当n=1时,此时只有一种操作,容易知道,当另一个数为奇数时先手胜利,当另一个数为偶数时后手胜利。m=1同理

    当n=2时,此时有三种情况,一:1*(m-1)。二:2*(m-1)。三:1*m。第一种情况 和第三种情况都会回到n=1时。并且当m为奇数时,选择第三种情况,先手胜利,当m为偶数时可以选择第一种情况,先手胜利,所以但n=2时先手必胜。m=2同理

    当n=3时,有三种情况,一:2*(m-1)。二:3*(m-1)。三:2*m,当选择第一种或第三种,转换先后手使棋盘变为n=2的情况,由于n=2的情况先手必胜,所以转换先后手后,就为后手胜利,所以不能选择第一种或者第三种情况,所以两方都会选择第二种情况,直到,m=3时,此时棋盘为3*3,此时选择第二种操作,也会变成n=2的情况。所以3*3先手,所以m-2为奇数时先手胜利,偶数时后手胜利。m=3同理。

    很容易发现,但n=4时,情况与n=2时相似,因为n=1与n=3的胜利判断条件是相同的,所以但n=4时也是先手必胜。

    同样,n=5时也与n=3相似。

    递推得,当n为偶数,先手必胜。当n为奇数时,另一个数为奇数时先手胜利,当另一个数为偶数时后手胜利。

    即,当n与m同为奇数为后手胜利,反之先手胜利。

    AC代码

    #include<iostream>
    using namespace std;
    int main() {
        int n, m;
        while (scanf("%d%d",&n,&m),n){
            printf("%s
    ", n % 2 && m % 2? "What a pity!": "Wonderful!");
        }
    
    }
  • 相关阅读:
    Javascript事件处理程序的3种方式
    JS原生AJAX
    所谓的渐进增强,优雅降级?
    1059 老师的苦恼
    HTML5 参数传递
    HDU 5289 Assignment(二分+RMQ-ST)
    HDU 3333 Turing Tree(离线树状数组)
    mac下 mysql 插入中文乱码解决
    校园商铺-7商品类别模块-5商品类别删除后端开发
    校园商铺-7商品类别模块-3商品类别添加后端开发
  • 原文地址:https://www.cnblogs.com/komet/p/13639596.html
Copyright © 2011-2022 走看看