湖南大学第十四届ACM程序设计新生杯(重现赛)I:II play with GG(博弈论||DP)
链接:https://ac.nowcoder.com/acm/contest/338/I
来源:牛客网
题目描述
IG won the S championship and many people are excited, ii and gg are no exception. After watching the game, the two of them also want to play a game.
There is now an infinite chessboard with only one piece. Initially the pieces are placed at the (x, y) position (the board coordinates are counted from 0). They took turns to move the pieces. ii moves first.
There are three ways to move
- Move the piece to the left by one space (from (x, y) to (x-1, y)).
- Move the piece down one space (from (x, y) to (x, y-1)).
- Move the piece to the lower left by one space (from (x, y) to (x-1, y-1)).
It should be noted that the pieces cannot be removed from the board.
The first person who can not operate is judged negative (in other words, the first person who moves the piece to (0,0) wins).
Now give the initial coordinates x, y of the piece. Under the premise that both take the optimal strategy, please output the name of the winner (ii or gg).
输入描述:
The input contains only one line. Enter two integers x y to represent the initial coordinates of the piece. (0<=x, y<=1000)。
输出描述:
the winner’s name (ii or gg)。
示例
输入
1 1
输出
ii
输入
124 654
输出
gg
题意
ii和gg一起玩游戏,将一个石子放在棋盘(x,y)的位置,每次移动石子,必须把石子从(x,y)位置移动到(x,y-1)、(x-1,y)、(x-1,y-1)这三个位置中的其中一个位置,将石子移动到(0,0)点的人获胜,ii先移动
思路
一、博弈论
推出一部分的NP态,可以发现,当x%2==0x\%2==0x%2==0并且y%2==0y\%2==0y%2==0时,先手处于必败态
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 0 | P | N | P | N | P | N |
| 1 | N | N | N | N | N | N |
| 2 | P | N | P | N | P | N |
| 3 | N | N | N | N | N | N |
| 4 | P | N | P | N | P | N |
| 5 | N | N | N | N | N | N |
N代表先手必胜,P代表先手必败
二、DP
根据题意可以知道,(0,1),(1,1),(1,0)点为N状态。
可以得出递推关系:当前点如果为P状态,那么这个点的前一步对应的三个可能的点一定都为N状态
AC代码
//NP
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
if((n&1)||(m&1))
cout<<"ii"<<endl;
else
cout<<"gg"<<endl;
return 0;
}
//DP
#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof(a))
const int maxn=1e3+10;
using namespace std;
int dp[maxn][maxn];
inline void init()
{
ms(dp,0);
dp[0][1]=dp[1][0]=dp[1][1]=1;
for(int i=1;i<maxn;i++)
{
for(int j=1;j<maxn;j++)
if(dp[i-1][j]&&dp[i][j-1]&&dp[i-1][j-1])
dp[i][j]=0;
else
dp[i][j]=1;
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
init();
int n,m;
cin>>n>>m;
if(dp[n][m])
cout<<"ii"<<endl;
else
cout<<"gg"<<endl;
return 0;
}
湖南大学第十四届ACM程序设计新生杯(重现赛)I:II play with GG(博弈论||DP)的更多相关文章
- 湖南大学第十四届ACM程序设计新生杯(重现赛)G a+b+c+d=? (16进制与LL范围)
链接:https://ac.nowcoder.com/acm/contest/338/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K6 ...
- 湖南大学第十四届ACM程序设计新生杯(重现赛)
RANK 0 题数 0 期末复习没有参加,补几道喜欢的题. A: AFei Loves Magic 签到 思路 :不需考虑 碰撞 直接计算最终状态即可. #include<bits/stdc ...
- 湖南大学第十四届ACM程序设计新生杯 E.Easy Problem
E.Easy Problem Description: Zghh likes number, but he doesn't like writing problem description. So h ...
- 湖南大学第十四届ACM程序设计新生杯 Dandan's lunch
Dandan's lunch Description: As everyone knows, there are now n people participating in the competiti ...
- 福建工程学院第十四届ACM程序设计大赛 - E - 外传:小晋逃生记
http://www.fjutacm.com/Contest.jsp?cid=705#P4 其实想清楚了就很简单,之前想了很多种方法,以为是二分什么的,看起来就像是一个单峰函数.但是发现直接暴力一波就 ...
- 福建工程学院第十四届ACM校赛M题题解 fwt进阶,手推三进制fwt
第九集,结束亦是开始 题意: 大致意思就是给你n个3进制的数字,让你计算有多少对数字的哈夫曼距离等于i(0<=i<=2^m) 思路: 这个是一个防ak题,做法是要手推公式的fwt 大概就这 ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2
题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)A - Array
题目描述 Given an array A with length n a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integ ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)L - Liao Han
题目描述 Small koala special love LiaoHan (of course is very handsome boys), one day she saw N (N<1e1 ...
随机推荐
- unity中使用www的库读取数据里面的数据
//使用www的库,读取数据里面的数据 string factoryControllerUrl = "http://IP地址:8086/DatabaseServerCode/GangShan ...
- min_25筛题目总结
看了网上众多博客后,我才发现,实现min_25只有脑子,没有代码. 当然可能是我太ruo了. min_25是一种想法,不是算法. 不要尝试套模板,因为很多题目并没有什么用. 最重要的一点,g不要看成是 ...
- python学习---装饰器
什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...
- oracle数据库 查看被锁定表及解锁方法
最近做项目,遇到了更新超时的问题,反复检查数据都正常,纳闷完了忽然想到是不是表被锁定了,一看果然,解决办法如下: --锁表查询语句 SELECT object_name, machine, s.sid ...
- 字符串的顺序倒置。(Reverse)
实际遇到的问题:在串口获取码表数据的时候,有的码表传到电脑上的数字顺序是颠倒的,即:123.45,会显示为54.321.需要重新处理数据.方法很多,也不难实现,现在列举其中5个. public str ...
- ERROR: Cannot load message class for [speech_control/command]. Are your messages built?
ubuntu14.04 ROS indigo 问题: 执行查看指定消息的命令,出现下面的错误提示,找不到该消息类型. ~$ rostopic echo /speech/command ERROR: C ...
- SQL-53 按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees
题目描述 按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employeesCREATE TABLE `dept_emp` (`emp_ ...
- complex类的定义和实现
#include<iostream> #include<cmath> using namespace std; class complex { public: complex( ...
- LINQ Expresstion Tree 表达式树
Expression trees represent code in a tree-like data structure, where each node is an expression, for ...
- java基础知识—运算符和基本选择结构
1.保存真假,使用boolean变量 boolean有两个值:true 真 false 假 2.从控制台接受输入信息,通过创建扫描器 Sacnner input=new Sacnner(System. ...