当前位置:首页 > 芯闻号 > 充电吧
[导读]题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题面: Calendar Game Time Limit: 5000/1000 MS (Ja

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079


题面:

Calendar Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3240    Accepted Submission(s): 1903


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 

 
题目大意:

    给定一个初始日期,对于当前日期,A,B轮流操作。操作方式有两种,以天数为单位后移一天,或者以月为单位,后移一月,但如果后面那个月份没有对应的天,那么就不能进行月移操作。若刚好移到指定日期者获胜。


解题:

    抛开日期操作不说,这是一道很简单的博弈问题,但由于结合了日期操作,所以处理较繁琐。博弈策略是这样的,对于一个日期,如果能够通过日移操作或月移操作达到一个必败态,那么该状态就为胜态,否则就为必败态。也就是去检验后一个月,和后一天的状态。这道题的亮点在于,出了任何小差错,就可能导致不能AC。

提升版:

    ZJUT 1587

代码:

#include 
#include 
#include 
#include 
#include 
#define eps 1e-7
using namespace std;
struct date
{
	int y,m,d;
};
//日期状态
bool status[2016][13][32],flag,sign;
//每月天数
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//日移操作
date next_day(int y,int m,int d)
{
	date tmp;
	//如果是12月份,可能会换年,需特殊处理
	if(m==12)
	{
		if(d==31)
		  tmp.y=y+1,tmp.m=1,tmp.d=1;
		else
		  tmp.y=y,tmp.m=12,tmp.d=d+1;
	}
	//如果不是2月,一般处理
	else if(m!=2)
	{
	   //看是不是该月最后一天,可能会换月,需特殊处理
       if(d==month[m])
		   tmp.y=y,tmp.m=m+1,tmp.d=1;
	   else
		   tmp.y=y,tmp.m=m,tmp.d=d+1;
	}
	//如果是2月,需要看是不是闰年
	else
	{
		if(y%400==0||(y%4==0&&y%100!=0))
		{
			if(d==29)
			  tmp.y=y,tmp.m=3,tmp.d=1;
			else
			  tmp.y=y,tmp.m=2,tmp.d=d+1;
		}
		else
		{
			if(d==28)
			  tmp.y=y,tmp.m=3,tmp.d=1;
			else
			  tmp.y=y,tmp.m=2,tmp.d=d+1;		
		}
	}
	return tmp;
}
//月移操作
date next_month(int y,int m,int d)
{
	date tmp;
	//flag为月移是否合法标志
	flag=true;
	//特判12月,可能会跨年
	if(m==12)
		tmp.y=y+1,tmp.m=1,tmp.d=d;
	//特判1、2月
	else if((m!=2)&&(m!=1))
	{
		//到达月末,换月
		if(d>month[m+1])
			flag=false;
		else
		  tmp.y=y,tmp.m=m+1,tmp.d=d;
	}
	//1月,因为后移到2月,要看2月是否在闰年
	else if(m==1)
	{
		if((y%4==0&&y%100!=0)||(y%400==0))
		{
			if(d>29)
				flag=false;
			else
			  tmp.y=y,tmp.m=2,tmp.d=d;
		}
		else
		{
			if(d>28)
				flag=false;
			else
			  tmp.y=y,tmp.m=2,tmp.d=d;
		}
	}
	//2月最多29天,对于3月都是可以的
	else
		tmp.y=y,tmp.m=m+1,tmp.d=d;
	return tmp;
}
//年移操作,此题不需要
date next_year(int y,int m,int d)
{
	//sign为年移是否合法标志
	sign=true;
	date tmp;
	//只有2月29号是特殊的
	if(m==2&&d==29)
		sign=false;
	else
	  tmp.y=y+1,tmp.m=m,tmp.d=d;
	return tmp;
}
int main()
{
	date tmp,tmp2;
	//标记11.1-11.4号
	for(int i=1;i<=4;i++)
	{
       if(i%2)
		   status[2001][11][i]=0;
	   else
		   status[2001][11][i]=1;
	}
	if(status[2001][11][1])
		status[2001][10][31]=0;
	else
		status[2001][10][31]=1;
	//10.5-10.30只能日移,不能月移
	for(int i=30;i>=5;i--)
	{
		tmp=next_day(2001,10,i);
		//如果后一天是必败态,那么当前就是胜态
		if(status[tmp.y][tmp.m][tmp.d])
			status[2001][10][i]=0;
		else
			status[2001][10][i]=1;
	}
	//10.1-10.4既可月移也可日移
	for(int i=4;i>=1;i--)
	{
        tmp=next_day(2001,10,i);
		tmp2=next_month(2001,10,i);
		//如果月移或日移到必败态,那么当前为胜态,否则为必败态
		if(status[tmp.y][tmp.m][tmp.d]||status[tmp2.y][tmp2.m][tmp2.d])
			status[2001][10][i]=0;
		else
			status[2001][10][i]=1;
	}
	//2001.1-2001.9既可月移又可日移
	for(int i=9;i>=1;i--)
	{
       for(int j=month[i];j>=1;j--)
	   {
		   tmp=next_day(2001,i,j);
		   if(status[tmp.y][tmp.m][tmp.d])
			   status[2001][i][j]=0;
		   else
		   {
			   tmp=next_month(2001,i,j);
			   if(flag)
			   {
				   if(status[tmp.y][tmp.m][tmp.d])
					   status[2001][i][j]=0;
				   else
					   status[2001][i][j]=1;
			   }
			   else
				   status[2001][i][j]=1;
		   }
	   }
	}
	//1900-2000,即可月移又可日移
	for(int i=2000;i>=1900;i--)
	{
		for(int j=12;j>=1;j--)
		{
			int k;
			if(j==2&&(i%4==0&&i%100!=0)||(i%400==0))
				k=29;
			else
				k=month[j];
			for(;k>=1;k--)
			{
				tmp=next_day(i,j,k);
				if(status[tmp.y][tmp.m][tmp.d])
					status[i][j][k]=0;
				else
				{
					tmp=next_month(i,j,k);
					if(flag)
					{
                       if(status[tmp.y][tmp.m][tmp.d])
						   status[i][j][k]=0;
					   else
						   status[i][j][k]=1;
					}
					else
						status[i][j][k]=1;
				}
			}
		}
	}
	//输入输出
	int yy,mm,dd,T;
	cin>>T;
	while(T--)
	{
		cin>>yy>>mm>>dd;
		if(status[yy][mm][dd])
			cout<<"NOn";
		else
			cout<<"YESn";
	}
	return 0;
}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

特朗普集团近日取消了其新推出的T1智能手机“将在美国制造”的宣传标语,此举源于外界对这款手机能否以当前定价在美国本土生产的质疑。

关键字: 特朗普 苹果 AI

美国总统特朗普在公开场合表示,他已要求苹果公司CEO蒂姆·库克停止在印度建厂,矛头直指该公司生产多元化的计划。

关键字: 特朗普 苹果 AI

4月10日消息,据媒体报道,美国总统特朗普宣布,美国对部分贸易伙伴暂停90天执行新关税政策,同时对中国的关税提高到125%,该消息公布后苹果股价飙升了15%。这次反弹使苹果市值增加了4000多亿美元,目前苹果市值接近3万...

关键字: 特朗普 AI 人工智能 特斯拉

3月25日消息,据报道,当地时间3月20日,美国总统特朗普在社交媒体平台“真实社交”上发文写道:“那些被抓到破坏特斯拉的人,将有很大可能被判入狱长达20年,这包括资助(破坏特斯拉汽车)者,我们正在寻找你。”

关键字: 特朗普 AI 人工智能 特斯拉

1月22日消息,刚刚,新任美国总统特朗普放出重磅消息,将全力支持美国AI发展。

关键字: 特朗普 AI 人工智能

特朗普先生有两件事一定会载入史册,一个是筑墙,一个是挖坑。在美墨边境筑墙的口号确保边境安全,降低因非法移民引起的犯罪率过高问题;在中美科技产业之间挖坑的口号也是安全,美国企业不得使用对美国国家安全构成威胁的电信设备,总统...

关键字: 特朗普 孤立主义 科技产业

据路透社1月17日消息显示,知情人士透露,特朗普已通知英特尔、铠侠在内的几家华为供应商,将要撤销其对华为的出货的部分许可证,同时将拒绝其他数十个向华为供货的申请。据透露,共有4家公司的8份许可被撤销。另外,相关公司收到撤...

关键字: 华为 芯片 特朗普

曾在2018年时被美国总统特朗普称作“世界第八奇迹”的富士康集团在美国威斯康星州投资建设的LCD显示屏工厂项目,如今却因为富士康将项目大幅缩水并拒绝签订新的合同而陷入了僵局。这也导致富士康无法从当地政府那里获得约40亿美...

关键字: 特朗普 富士康

今年5月,因自己发布的推文被贴上“无确凿依据”标签而与推特发生激烈争执后,美国总统特朗普签署了一项行政令,下令要求重审《通信规范法》第230条。

关键字: 谷歌 facebook 特朗普

众所周知,寄往白宫的所有邮件在到达白宫之前都会在他地进行分类和筛选。9月19日,根据美国相关执法官员的通报,本周早些时候,执法人员截获了一个寄给特朗普总统的包裹,该包裹内包含蓖麻毒蛋白。

关键字: 美国 白宫 特朗普
关闭