HDU1079 Calendar Game SG函数

20 篇文章 0 订阅

题目链接:HDU1079

Calendar Game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3893    Accepted Submission(s): 2347


Problem Description
Adam and Eve enter this year’s ACM International Collegiate Programming Contest. Last night, they played the Calendar Game, in celebration of this contest. This game consists of the dates from January 1, 1900 to November 4, 2001, the contest day. The game starts by randomly choosing a date from this interval. Then, the players, Adam and Eve, make moves in their turn with Adam moving first: Adam, Eve, Adam, Eve, etc. There is only one rule for moves and it is simple: from a current date, a player in his/her turn can move either to the next calendar date or the same day of the next month. When the next month does not have the same day, the player moves only to the next calendar date. For example, from December 19, 1924, you can move either to December 20, 1924, the next calendar date, or January 19, 1925, the same day of the next month. From January 31 2001, however, you can move only to February 1, 2001, because February 31, 2001 is invalid. 

A player wins the game when he/she exactly reaches the date of November 4, 2001. If a player moves to a date after November 4, 2001, he/she looses the game. 

Write a program that decides whether, given an initial date, Adam, the first mover, has a winning strategy. 

For this game, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.
 

Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input. Each test case is written in a line and corresponds to an initial date. The three integers in a line, YYYY MM DD, represent the date of the DD-th day of MM-th month in the year of YYYY. Remember that initial dates are randomly chosen from the interval between January 1, 1900 and November 4, 2001. 
 

Output
Print exactly one line for each test case. The line should contain the answer "YES" or "NO" to the question of whether Adam has a winning strategy against Eve. Since we have T test cases, your program should output totally T lines of "YES" or "NO". 
 

Sample Input
  
  
3 2001 11 3 2001 11 2 2001 10 3
 

Sample Output
  
  
YES NO NO
 
题意:一个游戏两人轮流走,给出一个日期,每次可以跳到下个月的这一天或是其后一天,一直到2001年11月4日,问最优解先手赢还是后手赢。
题目分析:考察SG函数,一共2个后继状态,因此vis数组开到3即可。日期从1900年到2001年6w多天,直接打表即可。一个比较麻烦的事是要在日历上遍历所有情况,因此需要详尽地讨论闰年和大小月的问题。
//
//  main.cpp
//  HDU1079
//
//  Created by teddywang on 2016/8/31.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sg[105][13][33],yy,mm,dd;
int da[10]={1,3,5,7,8,10,12};


int judge(int i,int j,int k)
{
    int flag=0;
    for(int t=0;t<7;t++)
    {
        if(da[t]==j)
        {
            flag=1;
            break;
        }
    }
    if(j==2) flag=-1;
    if(!flag&&k==31) return 0;
    if(flag==-1&&k>29) return 0;
    if(flag==-1&&i%4!=0&&k==29) return 0;
    if(flag==-1&&i==0&&k==29) return 0;
    return 1;
}

int mex(int y,int m,int d)
{
    if(sg[y][m][d]!=-1) return sg[y][m][d];
    int vis[3]={0,0,0};
    int y1=y,m1=m,d1=d+1;
    int y2=y,m2=m+1,d2=d;
    int flag=0;
    for(int i=0;i<7;i++)
    {
        if(da[i]==m1)
        {
            flag=1;
            break;
        }
    }
    if(m1==2) flag=-1;
    if((flag==1&&d1==32)||(flag==0&&d1==31))
    {
        d1=1;
        m1++;
    }
    else if(flag==-1&&y%4==0&&y&&d1==30)
    {
        d1=1;
        m1++;
    }
    else if(flag==-1&&(y%4!=0||y==0)&&d1==29)
    {
        d1=1;
        m1++;
    }
    if(m1==13)
    {
        m1=1;
        y1++;
    }
    sg[y1][m1][d1]=mex(y1,m1,d1);
    if(sg[y1][m1][d1]>1) vis[2]=1;
    else vis[sg[y1][m1][d1]]=1;
    if(m2==13)
    {
        m2=1;
        y2++;
    }
    if(judge(y2,m2,d2)==1)
    {
        sg[y2][m2][d2]=mex(y2,m2,d2);
        if(sg[y2][m2][d2]>1) vis[2]=1;
        else vis[sg[y2][m2][d2]]=1;
    }
    for(int i=0;i<3;i++)
    {
        if(!vis[i]) return i;
    }
    return -1;
}


void getsg()
{
    for(int i=0;i<=101;i++)
    {
        for(int j=1;j<=12;j++)
        {

            for(int k=1;k<=31;k++)
            {
                if(judge(i,j,k)==0) continue;
                if(i==101&&j==11&&k>=4) return;
                sg[i][j][k]=mex(i,j,k);
                //cout<<sg[i][j][k]<<endl;
            }
        }
    }
}

int main()
{
    memset(sg, -1, sizeof(sg));
    sg[101][11][4]=0;
    getsg();
    int T;
    cin>>T;
    while(T--)
    {
        cin>>yy>>mm>>dd;
        yy-=1900;
        if(sg[yy][mm][dd]==0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值