河南省第十一届ACM大学生程序设计竞赛——I-Attack City and Capture Territory

三国博弈问题
本文介绍了一个基于三国背景的简单博弈问题,通过分析火力点的攻防情况来预测胜利者。问题实质上与Nim游戏相似,利用异或运算判断先手是否能确保获胜。

题目描述:

The Three Kingdoms period was a relatively famous period in the history of China. From the Battle of Chibi (AD 211) to the reunification of China in the Western Jin Dynasty(AD 280). During the period, Cao's Wei State, Liu's Shu State, and Sun's Wu Guo's Three Kingdoms stood together. Therefore, it was called the Three Kingdoms period.

In the last years of the Eastern Han Dynasty, Dong_ Z specialized in power , the coalition forces of the world's princes crusade against each other. Among them, Liu_B and Sun_Q, who are school students, also participated in the crusade.

In AD 215 , Liu_B and Sun_Q simultaneously attacked JingZhou and directly threatened Dong Z's city. There were N firepower points on the high wall, each fire point with different s trength Xi . Liu_B and Sun_Q looked at the high walls and the strong gates, they did not attack the city traightaway. They negotiate to attack firepower point alternately. Who breaks through the last firepower point, he will win the city.

Because of limited weaponry, weapons of each side can only attack one firepower at a time. But they can control whether completely destroy this firepower point or weaken the strength of firepower point.

Liu_B has a strong think-tank. After calculation, he finds out who will attack first , who will more likely win the city .

 

输入描述:

The first line of the input contains one integer T, which is the number of  test cases (1<=T<=10).  Each test case specifies:

* Line 1:       N                ( 1 ≤ N ≤ 100 )

* Line 2:      X1 X2… Xn        ( 1 <= Xi<=1000    i=1…. n)

输出描述:

For each test case ,  print  “Liu_B is sure to win.” Or  “Liu_B is not sure to win.” ,  suppose Liu_B first attacks.

样例输入:

3
2
1 3
2
3 3
5
1 2 3 4 5

样例输出:

Liu_B is sure to win.
Liu_B is not sure to win.
Liu_B is sure to win.

这其实是一个简单的博弈问题,和经典的Nim游戏基本一样

原理不多说了,直接将输入的数组n的每一个数都进行异或运算,若最后结果不为零则先手必胜,若为零则先手必败

代码如下:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
	int t,n,m;
	cin>>t;
	while(t--){
		cin>>n;
		int x=0;
		for(int i=0;i<n;i++){
			cin>>m;
			x^=m;
		}
		if(x)cout<<"Liu_B is sure to win."<<endl;
		else cout<<"Liu_B is not sure to win."<<endl;
	} 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值