R. Game

本文解析了一道关于游戏理论的博弈题目,通过分析n×n棋盘上两人轮流标记空格的游戏规则,得出最优策略下胜者的选择依据。文章提供了一个简单的C++代码实现,用于判断先手或后手获胜。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://codeforces.com/problemset/problem/630/R
There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor.
The game is played on a square field consisting of n × n cells. Initially all cells are empty. On each turn a player chooses and paint an empty cell that has no common sides with previously painted cells. Adjacent corner of painted cells is allowed. On the next turn another player does the same, then the first one and so on. The player with no cells to paint on his turn loses.
The professor have chosen the field size n and allowed the student to choose to be the first or the second player in the game. What should the student choose to win the game? Both players play optimally.
Input
The only line of the input contains one integer n (1 ≤ n ≤ 1018) — the size of the field.
Output
Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2.
Examples
Input
1
Output
1
Input
2
Output
2
题意:游戏在由n×n个单元组成的方形场上进行。最初所有单元格都是空的。在每个回合中,玩家选择并绘制一个空的单元格,该单元格与先前绘制的单元格没有共同的边。允许涂漆细胞的相邻角落。在下一个回合中,另一个玩家做同样的事情,然后是第一个玩家,依此类推。在轮到他时没有细胞画的玩家输了。
教授选择了字段大小n并允许学生选择成为游戏中的第一个或第二个玩家。学生应该选择什么才能赢得比赛?两位球员都发挥出色。
简单博弈题:就是给一个n*n的棋谱(或者说表格),每个人每次只能放一次。学生可以做第一个或第二个玩家,都很认真没有失误。规定最后一个表格是谁下谁就赢。我们可以知道每次两个人放一个,递推到最后必会出现的情况是1或者是2,所以,我们只要想是奇数还是偶数是谁赢,然后直接就可以知道答案了;
解题思路:判断n是奇数还是偶数,偶数则2赢,奇数则1赢;

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long i,a;
    cin>>a;
    if(a%2==0)
    {
        cout<<"2";
    }
    else
        cout<<"1";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值