zoukankan      html  css  js  c++  java
  • HDU

    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!

    题解:

    这道题我是直接手画了几个样例,大致推出来感觉只要n,m只要有一个奇数就赢,试了试还真过了((^o^)/~)

    事后又去看了看大佬们的做法,原来可以画PN图推出来

    从左下角往前推:

    显然,最左下角的点是P

                 
                 
                 
                 
                 
                 
    P            

    这是7*7的表格,如图1,7位置为P。

    由于1,6和2,7位置只能向1,7位置移动,所以1,6与2,7为N。

                 
                 
                 
                 
                 
    N            
    P N          

    同理,第1列和第7行就可以填充完毕。

    P            
    N            
    P            
    N            
    P            
    N            
    P N P N P N P

    再反观2,6位置,作为2,6位置上的人,想赢得这场比赛,所以肯定会向1,7移动,因此2,6也是N

    P            
    N            
    P            
    N            
    P            
    N N          
    P N P N P N P

    每个位置上,都会向赢比赛的趋向走,所以剩余各个点的P、N都可以填充完毕

    P N P N P N P
    N N N N N N N
    P N P N P N P
    N N N N N N N
    P N P N P N P
    N N N N N N N
    P N P N P N P

    此图填完,可以找到规律:

    只有在行列数均为奇数时,为P,其他情况均为N。

    代码:

    #include <cstdio>
    
    using namespace std;
    
    int main(){
    	
    	int n,m;
    	while(scanf("%d %d",&n,&m) && (n || m)){
    		if(n%2 == 0 || m%2 == 0)printf("Wonderful!
    ");
    		else printf("What a pity!
    ");
    	}
    	
    	return 0;
    } 
  • 相关阅读:
    leetcode Remove Nth Node From End of List
    leetcode Plus One
    leetcode climbing stairs
    leetcode Merge Two Sorted Lists
    leetcode Maximum Subarray
    leetcode Binary Tree Level Order Traversal I II
    leetcode Pascal's Triangle II
    leetcode pascal's triangle
    leetcode valid parentheses
    leetcode Path Sum
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9513993.html
Copyright © 2011-2022 走看看