首页 > ACM题库 > HDU-杭电 > HDU 3951-Coin Game-博弈论-[解题报告]HOJ
2015
04-14

HDU 3951-Coin Game-博弈论-[解题报告]HOJ

Coin Game

问题描述 :

After hh has learned how to play Nim game, he begins to try another coin game which seems much easier.
Parking Log

The game goes like this:
Two players start the game with a circle of n coins.
They take coins from the circle in turn and every time they could take 1~K continuous coins.
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn’t take 1, 3, 4, because 1 and 3 aren’t continuous)
The player who takes the last coin wins the game.
Suppose that those two players always take the best moves and never make mistakes.
Your job is to find out who will definitely win the game.

输入:

The first line is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=109,1<=K<=10).

输出:

The first line is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=109,1<=K<=10).

样例输入:

2
3 1
3 2

样例输出:

Case 1: first
Case 2: second

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

 

题目大意:给你n个硬币,把它围成一个圆圈。现在有两个人玩这样的一个翻转游戏,每次翻转1–k个硬币,最后一个翻转硬币者胜。

 

 

思路:博弈

1) 若k=1,则一次只能去翻一枚,奇数先手赢,偶数后手赢。

2)若k>1:

a: 先手一次翻完,先手赢;

b: 先手不能翻完,第一次必定断环。只要后手一次翻完,或将其分为相等数量的两段,

之后先手怎么操作后手就怎么操作,后手必赢。

 

 

代码:

#include<iostream>
using namespace std;
int main()
{
	int t,n,k,i;
	cin>>t;
	for(i=1;i<=t;i++)
	{
		cin>>n>>k;
		if(k==1)
		{
			if(n%2==1)
			{
				cout<<"Case "<<i<<": first"<<endl;
			}
			else
			{
				cout<<"Case "<<i<<": second"<<endl;
			}
		}	
		else
		{
			if(k>=n)
			{
				cout<<"Case "<<i<<": first"<<endl;
			}
			else
			{
				cout<<"Case "<<i<<": second"<<endl;
			}
		}
	}
	return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/no_retreats/article/details/8693435


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  3. 第一题是不是可以这样想,生了n孩子的家庭等价于n个家庭各生了一个1个孩子,这样最后男女的比例还是1:1