现在的位置: 首页 > 综合 > 正文

Calendar Game POJ1082

2013年03月17日 ⁄ 综合 ⁄ 共 4876字 ⁄ 字号 评论关闭

Calendar Game
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3508   Accepted: 1574

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

思路:
        从1900-1-1到2001-11-4期间任一个日期开始,每次要么向后移动一天,要么向后移动一个月。最后准确到达2001-11-4的为胜者。
        典型的博弈论,算法思路为在初始化的过程中便获得2001.11.4到1990.1.1期间所有日期的胜败情况。对于y-m-d日期,若其下一天和下一月中有一个为必败情况,则今天为必胜。若两者均为必胜,则今天为必败。主要难度在于前一日(getDayBefore)和后一月(isValidNextMonth)日期的判定,要注意闰年情况的特殊性。

代码如下:
#include <iostream>
using namespace std;

struct Date
{
	int y,m,d;
}date;

bool state[102][13][32];
bool leapFlag;
int month[]={31,31,28,31,30,31,30,31,31,30,31,30,31};

bool is_leapYear(int y)   //是否为闰年
{
	if(y%4==0)
	{
		if(y%400!=0)return false;
		return true;
	}
	return false;
}

bool getDayBefore()      //获取前一天的日期
{
	bool loopFlag=true;
	if(date.d>1){date.d--;return loopFlag;}
	if(date.m==3)
	{
		date.m--;
		if(leapFlag) date.d=29;
		else date.d=28;
	}else if(date.m==1)
	{
		date.y--;
		date.m=12;
		date.d=month[12];
		loopFlag=false;
	}else
	{
		date.m--;
		date.d=month[date.m];
	}
	return loopFlag;
}

bool isValidNextMonth()  //判断下个月的这天是否有效
{
	if(date.y>=101 && ((date.m==10 && date.d>4)||(date.m>10)))return false;
	if(date.d<=month[(date.m+1)%12])return true;                //将month[0]=31 便可用 %12
	if(leapFlag && date.m==1 && date.d<30)return true;
	return false;
}

void init()
{
	memset(state,0,102*13*32);
	date.y=101;
	date.m=11;
	date.d=3;
	Date time={101,11,4};
	bool loopFlag;
	while(date.y>=0)
	{
		leapFlag=is_leapYear(date.y+1900);
		loopFlag=true;
		while(loopFlag)
		{
			if(!state[time.y][time.m][time.d])
			{
				state[date.y][date.m][date.d]=true;
			}else
			{
				if(isValidNextMonth())
				{
					if(date.m==12)
					{
						if(!state[date.y+1][1][date.d])state[date.y][date.m][date.d]=true;
					}else if(!state[date.y][date.m+1][date.d])state[date.y][date.m][date.d]=true;
				}
			}
			time=date;
			loopFlag=getDayBefore();
		}
	}
}

int main()
{
	freopen("in.txt","r",stdin);
	int i,n;
	int y,m,d;
	cin>>n;
	init();
	for(i=0;i<n;i++)
	{
		cin>>y>>m>>d;
		if(state[y-1900][m][d])cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}

网上还有一种超简单的方法,分析:2种操作,日+1,或月+1,(除了几个特殊日期外)均能改变月+日的奇偶性,    而目标11.4,月+日为奇数,所以只要起始日期的月+日为偶数,Adam,the first mover,就可能赢。而两个特殊日期(9.30,11.30),尽管月+日为奇数,但下一步仍然可以得到奇数。


代码如下:

#include <iostream>
using namespace std;

bool win(int m,int d)
{
	if((m+d)%2==0)return true;
	if(d==30 && (m==9 ||m==11))return true;
	return false;
}

int main()
{
	freopen("in.txt","r",stdin);
	int i,n;
	int y,m,d;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>y>>m>>d;
		if(win(m,d))cout<<"YES"<<endl;
		else  cout<<"NO"<<endl;
	}
	return 0;
}

我将两者得到的结果进行对比,


	for(i=0;i<102;i++)
	{
		for(j=1;j<13;j++)
		{
			for(z=1;z<=month[j];z++)
			{
				if(win(j,z)!=state[i][j][z])
				{
					cout<<"不一致! "<<i+1900<<"-"<<j<<"-"<<z<<endl;
				//	system("PAUSE");
				}
			}
		}
	}

发现对于1990-1-1到2001-11-4的所有日期的胜败情况确实是完全一致的。

看来这绝非巧合,通过对数据的仔细分析,我已经能够从心里接受这个规律。现分析如下:

         如果轮到我时的日期mm+dd为偶数,那我必赢。因为11.4为奇数,我总可以采取两种move方式中的一种使得我move后的日期为奇数。而且我可以保证不把9.30和11.30这两个奇数日期留给对方。这样以来,仅有的两个能够由奇数变成奇数的日期(9.30-10.1和11.30-12.1)对方没有机会碰到,那么他只能一直是move
from奇数to偶数,也就不可能赢了。

问题①如何保证我每次都可以move from 偶数 to 奇数?
       单纯的mm - dd变成(mm+1) - dd或mm - (dd+1)当然可以实现偶数到奇数的转变。我们再考虑以下一些特殊情况即可。
一:对于x.31这种month[x]=31的月份,且x+31为偶数比如3.31,采用dd+1移动变成(x+1).1为奇数。
二:对于x.30这种month[x]=30的月份,且x+30为偶数比如6.30,采用mm+1移动变成(x+1).30为奇数。
三:对于平年,1.29直接dd+1移动变成1.30为奇数;对于1.31也dd+1变为2.1为奇数。

②如何保证不给对手留下9.30或11.30的日期?
一:遇到8.30直接dd+1变成8.31
二:遇到11.29直接mm+1变成12.29

抱歉!评论已关闭.