HDU 1079 Calendar Game

Calendar Game

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


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求解,如果当前日期可达到的状态都为P状态,则当前为N状态,递归求解.

解题代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define cle(name, n) memset(name, n, sizeof(name))
 4 #define LL long long
 5 const int max_y = 2003;
 6 int datas[max_y][13][35];
 7 
 8 const int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 9 
10 bool is_leap(int year){
11     if ((year%4 == 0 && year%100) || year%400 == 0)
12         return true;
13     return false;
14 }
15 
16 int Get_SG(int year, int mon, int day){
17     if (datas[year][mon][day] != -1)
18         return datas[year][mon][day];
19     if (year == 2001 && mon == 11 && day == 4)
20         return datas[year][mon][day] = 0;
21     if (year > 2001 || (year == 2001 && (mon > 11 || (mon == 11 && day > 4))))
22         return datas[year][mon][day] = 1;
23     
24     int dy, dm, dd;
25     dy = year, dm = mon, dd = day;
26     dd ++;
27     if (dd > month[dm]){//使日期合法 
28         if(is_leap(year) && dm == 2){
29             if(dd > 29){
30                 dd = 1;
31                 dm ++;
32             }
33         }
34         else{
35             dd = 1;
36             dm ++;
37             if(dm > 12){
38                 dm = 1;
39                 dy ++;
40             }
41         }
42     }
43     if(!Get_SG(dy, dm, dd))
44         return datas[year][mon][day] = 1;
45     dy = year, dm = mon, dd = day;
46     dm ++;
47     if(dm > 12){
48         dm = 1;
49         dy ++;
50     }
51     if (dd <= month[dm] || (dm == 2 && is_leap(year) && dd <= 29)) //判断日期是否合法 
52         if(!Get_SG(dy, dm, dd))
53             return datas[year][mon][day] = 1;
54     return datas[year][mon][day] = 0;
55 }
56 
57 int main(){
58 //freopen("data.in", "r", stdin);
59 //freopen("data1.out", "w", stdout);
60     int T;
61     scanf ("%d", &T);
62     int year, mon, day;
63     cle(datas, -1);
64     while(T --){
65         scanf ("%d%d%d", &year, &mon, &day);
66         if(Get_SG(year, mon, day))
67             printf ("YES\n");
68         else printf ("NO\n");
69     }
70     return 0;
71 }
View Code

网上有一种直接通过输入的数据来判断输赢的方法:(以下为转载)

 

如果轮到我时的日期mm+dd为偶数,那我必赢。因为11.4为奇数,我总可以采取两种move方式中的一种使得我move后的日期为奇数。而且我可以保证不把9.30和11.30这两个奇数日期留给对方。这样以来,仅有的两个能够由奇数变成奇数的日期(9.30-10.1和11.30-12.1)对方没有机会碰到,那么他只能一直是move from奇数to偶数,也就不可能赢了。
 
问题①如何保证我每次都可以move from 偶数 to 奇数?
       单纯的mm - dd变成(mm+1) - dd或mm - (dd+1)当然可以实现偶数到奇数的转变。我们再考虑以下一些特殊情况即可。
一:对于x.31这种month[x]=31的月份,且x+31为偶数比如3.31,采用dd+1移动变成(x+1).1为奇数。
二:对于x.30这种month[x]=30的月份,且x+30为偶数比如6.30,采用mm+1移动变成(x+1).30为奇数。
三:对于平年,1.29直接dd+1移动变成1.30为奇数;对于1.31也dd+1变为2.1为奇数。
 
②如何保证不给对手留下9.30或11.30的日期?
一:遇到8.30直接dd+1变成8.31
二:遇到11.29直接mm+1变成12.29
 
代码如下:
#include <iostream>
#include <stdio.h>
using namespace std;

bool win(int m,int d)
{
    if((m+d)%2==0)return true;
    if(d==30 && (m==9 ||m==11))return true;
    return false;
}

int main()
{
//    freopen("data.in","r",stdin);
    int i,n;
    int y,m,d;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>y>>m>>d;
        if(win(m,d))cout<<"YES"<<endl;
        else  cout<<"NO"<<endl;
    }
    return 0;
}

 

posted on 2013-11-18 14:47  圣手摘星  阅读(189)  评论(0编辑  收藏  举报

导航