poj 1082[博弈,日历处理,stack overflow错误]

来源:互联网 发布:骨传导耳机危害 知乎 编辑:程序博客网 时间:2024/05/02 19:49
Calendar Game
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3698 Accepted: 1671

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

YESNONO

Source

Taejon 2001




/*
关键的是
1、日历处理上,就是闰年的2月要特别处理。
2、SG函数的计算,刚开始用记忆化(意思就是如果算过了就不算,否则递归地算)算,这样会stack overflow(因为如果一开始就算1900 1 1,那递归的结果就是会接着算1900 1 2,把1900 1 1放在stack中,然后1900 1 2又会接着算1900 1 3,如此这般,最后的出口是2001 11 4,大概要365*(2011-1900)即30000多次,即把数据保存在stack中30000多次,然后就stack overflow了,实际上当这样递归到达4800次stack就已经overflow了,测试程序见下)。
这个程序在VS2008下出现错误,在VC6.0下说是stack overflow。
3、知道了原因,1900 1 1 不行改用从2001 11 3号开始从后往前算,就是把1900 1 1 到2001 11 3全部计算一遍时间复杂度大概是365*(2011-1900)即30000多(当然还要乘上一个常数,了不起就是10),完全可以承受。
#include<iostream>
using namespace std;

int f(int i)
{
return i*2/2+4-4;
}

int f2(int k)
{
if(k==0) return 1;
f(k);
f2(k-1);
return 0;
}

int main()
{
f2(4800);
return 0;
}
*/










#include<iostream>using namespace std;int mm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int sg[2001-1900+10][13][32];inline bool is_leap_year(int y){return ((y%4==0)&&(y%100!=0))||(y%400==0);}inline bool valid_day(int &y,int &m,int &d){return y*366+m*31+d<=2001*366+11*31+4;}bool next_day(int &y,int &m,int &d){int i,j,k;if(m==12&&d==31)//next year;{y++;m=d=1;return valid_day(y,m,d);}//next month;if(is_leap_year(y)&&m==2&&d==28){d++;return valid_day(y,m,d);}if(is_leap_year(y)&&m==2&&d==29){d=1;m=3;return valid_day(y,m,d);}if(mm[m]==d){m++;d=1;return valid_day(y,m,d);}//next day;d++;//cout<<"come here"<<endl;return valid_day(y,m,d);}bool next_month(int &y,int &m,int &d){int i,j,k;//next year;if(m==12){y++;m=1;return valid_day(y,m,d);}if(is_leap_year(y)&&m==1){if(d<=29) {m++;return valid_day(y,m,d);}return 0;}m++;if(d<=mm[m]) return valid_day(y,m,d);else return 0;}int SG(int y,int m,int d){if(sg[y-1900][m][d]!=-1) return sg[y-1900][m][d];int next_y=y,next_m=m,next_d=d;//hava a valid next day/*if(next_day(next_y,next_m,next_d)) cout<<"have next day"<<endl;else cout<<"haven't next day"<<endl;cout<<"next day is"<<next_y<<' '<<next_m<<' '<<next_d<<endl;//return 0;*/if(next_day(next_y,next_m,next_d)==1){if(SG(next_y,next_m,next_d)==0)return sg[y-1900][m][d]=1;}//have a valid next monthnext_y=y;next_m=m;next_d=d;/*if(next_month(next_y,next_m,next_d)) cout<<"have next day"<<endl;else cout<<"haven't next day"<<endl;cout<<"next day is"<<next_y<<' '<<next_m<<' '<<next_d<<endl;return 0;*/if(next_month(next_y,next_m,next_d)==1){if(SG(next_y,next_m,next_d)==0)return sg[y-1900][m][d]=1;}return sg[y-1900][m][d]=0;}int main(){int i,j,k,n;int y,m,d;memset(sg,-1,sizeof(sg));sg[2001-1900][11][4]=0;for(i=2001;i>=1900;i--)//把2001 11 4到1900 1 1全算一遍,由于逆序,不会stack overflow,而且时间复杂度完全可以承受。for(j=12;j>=1;j--)for(k=31;k>=1;k--)if(valid_day(i,m,d))SG(i,j,k);//记忆化cin>>n;for(i=0;i<n;i++){scanf("%d%d%d",&y,&m,&d);SG(y,m,d)?puts("YES"):puts("NO");}return 0;}


原创粉丝点击