HDU2147 kiki's game (SG表找规律)

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? 

InputInput 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. 

OutputIf 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),每次可向左,或向上,或左上走一步。走到(1,1)者胜利。

没有什么好的思路,SG函数转移没问题,但是MLE,所以打表找规律。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2000;
int SG[maxn+10][maxn+10];
int ex[maxn],n,m;
void solve()
{
    int times=0;
    for(int i=1;i<=maxn;i++)
     for(int j=1;j<=maxn;j++)
     {
         if(i==1&&j==1){
             SG[i][j]=0;
             continue;
         }
         times++;
         if(i>1) ex[SG[i-1][j]]=times;
         if(j>1) ex[SG[i][j-1]]=times;
         if(i>1&&j>1) ex[SG[i-1][j-1]]=times;
         for(int k=0;k<10;k++){
             if(ex[k]!=times) {
                    SG[i][j]=k; break;
             }
         }
     }
}
int main()
{
    solve();
    for(int i=1;i<=10;i++){
        for(int j=1;j<=10;j++){
            if(SG[i][j]) printf("P ");
            else printf("N ");
        }
        printf("
");
    }
}
N P N P N P N P N P
P P P P P P P P P P
N P N P N P N P N P
P P P P P P P P P P
N P N P N P N P N P
P P P P P P P P P P
N P N P N P N P N P
P P P P P P P P P P
N P N P N P N P N P
P P P P P P P P P P


Process exited normally.
Press any key to continue . . .

发现n和m都为奇数点才是必胜N态。

但是至于证明,我暂时还没想到。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m; 
int main()
{
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) return 0;
        n%=2; m%=2;
        if(n==0||m==0) printf("Wonderful!
");
        else printf("What a pity!
");
    } return 0;
}

一个小时后,突然想起10000kb的内存,用bool应该没问题,果然。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2000;
bool SG[maxn+10][maxn+10];
int n,m;
void solve()
{
    for(int i=1;i<=maxn;i++)
     for(int j=1;j<=maxn;j++)
     {
         if(i==1&&j==1){
             SG[i][j]=false;
             continue;
         }
         if(i>1&&!SG[i-1][j]) SG[i][j]=true;
         if(j>1&&!SG[i][j-1]) SG[i][j]=true;
         if(i>1&&j>1&&!SG[i-1][j-1]) SG[i][j]=true;
     }
}
int main()
{
    solve();
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) return 0;
        if(SG[n][m]) printf("Wonderful!
");
        else printf("What a pity!
");
    } return 0;
}
原文地址:https://www.cnblogs.com/hua-dong/p/8405315.html