HDU 1079 Calendar Game解题报告及思考

    技术2024-12-08  20

    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 题目分析:这道题我是先运用P/N法分析了一下,显然2001//11/4日是一个必败点(P点),往前2001//11/3和//2001//10/4是一个必胜点(N点)。这样的话我们就找到了这题的规律:每次从一个P点开始枚举,找到它的N点,直到找完所有的点。这里我们用到了:如果一个节点的后续节点都是N点,该节点为P点,否则该节点为N。也即是说,只要有一个后续节点是P点,该节点就是N点。 做完这题我有想起了http://acm.hdu.edu.cn/showproblem.php?pid=1847这道题,当时我分析这道题(见这里http://blog.csdn.net/zhangxiang0125/archive/2011/02/08/6174639.aspx)的时候用的是求sg值法。我说用P/N分析更简单,现在想想其实不然。因为这题说两位选手每次拿牌是2^i次。如果数据比较刁钻。用P/N分析很难一下子找出规律(在ACM那样的赛场上应该寻求尽快解决)。而求sg值得话,只要你掌握了公式或这有个很不错的模版,那么解决题目就可在瞬间秒杀。当然我只是菜鸟。。只是做题的一点小心得、还有待实践证明。望指正! 下面是1079的代码: #include<cstdio> #include<algorithm> using namespace std; int later_y,later_m,later_d; int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; bool sg[2002][13][35]; int is_leap(int x) { if(x%4==0 || (x%100==0&&x%400==0)) return 1; return 0; } int is_legal(int y,int m,int d) { if(y==2001) { if(m>11 ||(m==11&&d>4)) return 0; } if(m==2) { if(is_leap(y)) { if(d>29) return 0; else return 1; } } if(month[m]>=d) return 1; else return 0; } void mex() { memset(sg,false,sizeof(sg)); for(int i=2001;i>=1900;i--) { for(int j=12;j>0;j--) { for(int d=31;d>0;d--) { if(is_legal(i,j,d)) { if(!sg[i][j][d]) { later_y=i; later_m=j; later_d=d-1; if(later_d==0) { later_m--; if(later_m==0) { later_y--; later_m=12; } if(is_leap(later_y) && later_m==2) later_d=month[later_m]+1; else later_d=month[later_m]; } sg[later_y][later_m][later_d]=true; later_y=i; later_m=j-1; later_d=d; if(later_m==0) { later_y--; later_m=12; } if(is_legal(later_y,later_m,later_d)) sg[later_y][later_m][later_d]=true; } } } } } } int main() { freopen("game.in","r",stdin); freopen("game.out","w",stdout); int ncase,curyear,curmonth,curday; mex(); scanf("%d",&ncase); while(ncase--) { scanf("%d%d%d",&curyear,&curmonth,&curday); if(sg[curyear][curmonth][curday]) printf("YES/n"); else printf("NO/n"); } return 0; }
    最新回复(0)