Calendar Game POJ - 1082(关于日历的博弈问题)
題意:
兩個人輪流玩游戲,每個人可以把日期進行轉移,轉移規則:
1.可以轉移到當前日期的下一天。
2可以轉移到下個月的這一天。(但是如果下個月沒有這一天就不能進行第二種轉移)
3.如果A恰好移動到2001.11.4那么A贏,如果移動到2001.11.4之后則輸給你初始日期(只能在1900.1.1~2001.11.4)求先移動的人的勝負態
題目:
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
分析:
這是一條簡單的規律:一般情況下,月份和日子同奇偶則必勝,否則必敗;只有9月30和11月30是例外。
有31天的月份:1,3,5,7,8,10,12
30天的月份:4,6,9,11
奇葩月:2
(1).先從2001.11開始看,這個月只有4天,且每一天只有第一種轉移態,那么可以知道1和3號是必勝態,2和4號是必敗態,故2001.11月是奇勝
(2).再向前看10月,因為10.5~ 10.31的轉移態只有第一種轉移態,那么根據推斷,10.31為必敗,所以5 ~ 31的這些天里偶數的天為必勝態,奇數必敗。再看10月的1 ~ 4,對于4來說下一個可以到5和11.4,所以必勝,對于3可以到11.3和10.4所以必敗,所以1~4也是偶數必勝奇數必敗,故10月是偶數必勝,奇數必敗。
(3).再看9月,因為9.30可以轉到10.1和10.30,為了勝利就選擇到10.1故9.30必勝,然后看9.29,可以轉到10.29和9.30,同樣為了勝利轉到10.29,但是再看9.28,無論轉到9.29還是10.28都會輸,所以9.28是必敗,同理9月的1~ 27都是奇勝偶敗,加上28,29,30可以得出結論,9月的1~29號都是奇勝偶敗,且9.30也為勝。
(4).再看八月,8.31只能到9.1,故為敗,8.30可以到8.31故為勝,8.29可以到9.29和8.30,而這兩種都會輸,所以8.29是輸,則1~28號的推理過程相似所以8月是奇敗偶勝。
這樣可以按照相同的方法推出:
(5).7月:奇勝偶敗
。。。。。。。
3月:奇勝偶敗
再來看2月,如果有29天,則對于2.29可以到3.29和3.1,都會輸,所以2.29敗,2.28可以到2.29故會贏。所以這種情況是奇敗偶勝
如果只有28天,2.28可以到3.28故會贏,2.27可以到2.28和3.27,但都會輸,所以這種情況是奇敗偶勝
所以2月無論是有29天還是有28天都是奇敗偶勝
1月:奇勝偶敗
前一年:
12月:奇敗偶勝
11月:30可以到12.1,所以勝利,29可以到12.29所以勝利,28可以到29和12.28,但是都會輸,所以11月的1~29都是奇勝偶敗,且30也會勝利
所以對于任意一個日期(除了9.30和11.30)只要月份+天數為偶數就會勝利,否則失敗。
AC代碼:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; int t,n,m,k; int main(){cin>>t;while(t--){cin>>n>>m>>k;if(m==9&&k==30||m==11&&k==30)cout<<"YES"<<endl;else if((m+k)%2==0)cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0; }總結
以上是生活随笔為你收集整理的Calendar Game POJ - 1082(关于日历的博弈问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长光所考博信息
- 下一篇: Python-JS (JS介绍~JS的基