hdu1079 Calendar Game

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


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
 
题意:给你一个日期,日期的范围为1900.1.1~2001.11.4,两个人轮流走,每个人可以走当前日期的下一天或者下个月的同样天数(如果不存在就不能走),谁先走到2001.11.4就获胜,问先手是否必胜。
思路:因为只有一个游戏,所以我们只需要判断每一个日期是否为必胜点或者必败点就行了,我们知道2011.11.4是必败点,所以我们可以依次往前推,求出每个日期对应的状态。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node{
    int year,month,day;
};
bool operator<(node a,node b){
    if(a.year==b.year){
        if(a.month==b.month){
            return a.day<b.day;
        }
        return a.month<b.month;
    }
    return a.year<b.year;
}

map<node,int>mp;

int tianshu1[30]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int tianshu2[30]={0,31,29,31,30,31,30,31,31,30,31,30,31};

int runnian(int x)
{
    if( (x%4==0 && ((x%100)!=0) ) || (x%400==0) )return 1 ;
    return 0;
}

node qianyitian(int year,int month,int day)
{
    int i,j;
    node temp2;
    if(day>1){
        temp2.year=year;temp2.month=month;temp2.day=day-1;
    }
    else if(day==1){
        if(month==1){
            temp2.year=year-1;temp2.month=12;temp2.day=31;
        }
        else if(month==3){
            if(runnian(year)){
                temp2.year=year;temp2.month=2;temp2.day=29;

            }
            else{
                temp2.year=year;temp2.month=2;temp2.day=28;
            }
        }
        else{
            temp2.year=year;temp2.month=month-1;temp2.day=tianshu1[month-1];
        }
    }
    return temp2;
}

node xiageyue(int year,int month,int day)
{
    int i,j,tianshu;
    node temp2;
    if(month==12){
        temp2.year=year+1;temp2.month=1;temp2.day=day;
    }
    else if(month==2){
        temp2.year=year;temp2.month=3;temp2.day=day;
    }
    else if(month==1){
        if(runnian(year)){
            tianshu=29;
        }
        else tianshu=28;
        if(day>tianshu){
            temp2.year=temp2.month=temp2.day=0;
        }
        else{
            temp2.year=year;temp2.month=2;temp2.day=day;
        }
    }
    else{
        tianshu=tianshu1[month+1];
        if(day<=tianshu){
            temp2.year=year;temp2.month=month+1;temp2.day=day;
        }
        else{
            temp2.year=temp2.month=temp2.day=0;
        }


    }
    if(temp2.year==2001){
        if( (temp2.month>11) || (temp2.month==11 && temp2.day>4)  ){
            temp2.year=temp2.month=temp2.day=0;
        }

    }
    return temp2;
}

void get_bisheng()
{
    int i,j,year=2001,month=11,day=4,bisheng=0;
    node temp,temp1;
    temp1.year=2001;temp1.month=11;temp1.day=4;
    mp[temp1]=0;
    //while(year!=1899)
    while(year!=1899)
    {
        temp=qianyitian(year,month,day);
        //printf("---+++>%d %d %d\n",temp.year,temp.month,temp.day);
        if(bisheng==0){
            mp[temp]=1;
        }
        else{
            temp1=xiageyue(temp.year,temp.month,temp.day);
            //printf("+++>%d %d %d\n",temp1.year,temp1.month,temp1.day);
            if(temp1.year!=0){
                if(mp[temp1]==0){
                    mp[temp]=1;
                }
                else mp[temp]=0;
            }
            else{
                mp[temp]=0;
            }
        }
        year=temp.year;month=temp.month;day=temp.day;bisheng=mp[temp];
        //printf("--->%d %d %d %d\n",temp.year,temp.month,temp.day,mp[temp]);
    }
}


int main()
{
    //freopen("o.txt","w",stdout);
    int n,m,i,j,T;
    node a;
    mp.clear();
    get_bisheng();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&a.year,&a.month,&a.day);
        if(mp[a]==0)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}


你可能感兴趣的:(博弈)