菜鸟学习ACM,纪录自己成长过程中的点滴。
学习的路上,与君共勉。
ACM学习-POJ-1082-Calendar Game
Calendar Game
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 file. 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 Source |
题目要求:
给你一个1900 1 1到2001 11 4之间的日期,然后两个人可以操作:月份+1或者天数+1(月份不行的话只能加天),最先到达2011 11 4的人获胜,问是否是第一个人获胜。
题目分析:
典型的博弈论,算法思路为在初始化的过程中便获得2001.11.4到1990.1.1期间所有日期的胜败情况。对于y-m-d日期,若其下一天和下一月中有一个为必败情况,则今天为必胜。若两者均为必胜,则今天为必败。主要难度在于前一日(getDayBefore)和后一月(isValidNextMonth)日期的判定,要注意闰年情况的特殊性。
虽然说这样可行。但是,网上给出了另外一种巧解,如下。
从后往前推,对于某一天的胜败由两天决定:后一天和后一个月的同一天(当然有的没有第二个决定因素)
若两者都是必胜态,则这一天必败。若有一天为必败,则这一天必胜。
2001年11月4日到2001年10月4日胜败是交替的,设月+日等于M。发现M为偶数时必胜,为奇数是必败。
2001年10月4日必胜,10月3日由10月4日和11月3号决定,两者都是必胜,所以10月3号必败。同理可判定10月剩余的日子。相邻的两个月的同一天胜败情况相反,但这一天M的奇偶性也相反(月份或日期两者只有一个减一),所以结论成立。
下面考虑特殊情况:
9月只有30天,9月30号由10月1号(必败)和10月30号(必胜)决定,为必胜。与上述结论矛盾,但9月29号为必胜,9月后面的日子依然遵循这样的规律,可见9月30号为一个特殊情况,同样的特殊情况还有11月30号(用同样的方法判断4,6月30号发现遵循结论),对于2月(不管是闰年还是平年)发现也符合。
故有如下超级简洁的代码:
#include <stdio.h>
int main()
{
int year_, month_, day_;
int n;
scanf("%d",&n);
while (n--)
{
scanf("%d %d %d", &year_, &month_, &day_);
if ((month_ == 9 || month_ == 10) && day_ == 30)
{
printf("YES\n");
}
else if ((month_ + day_) % 2 == 1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}
另外mark一篇详细比较的文章。
http://blog.csdn.net/tiantangrenjian/article/details/6938109