博弈——Play a game

New Year is Coming! 
ailyanlu is very happy today! and he is playing a chessboard game with 8600. 
The size of the chessboard is n*n. A stone is placed in a corner square. They play alternatively with 8600 having the first move. Each time, player is allowed to move the stone to an unvisited neighbor square horizontally or vertically. The one who can't make a move will lose the game. If both play perfectly, who will win the game?
Input
The input is a sequence of positive integers each in a separate line. 
The integers are between 1 and 10000, inclusive,(means 1 <= n <= 10000) indicating the size of the chessboard. The end of the input is indicated by a zero.
Output
Output the winner ("8600" or "ailyanlu") for each input line except the last zero. 
No other characters should be inserted in the output.
Sample Input
2
0
Sample Output
8600

题意:ailyanlu和8600玩游戏,有一个边长为n的正方形棋盘,棋子最开始在棋盘的某一个角落,玩家轮流移动这颗棋子,每回合可以移动到这颗棋子的前后左右且之前没移动到的格子,如果某个玩家不能移动棋子,则为输。8600先移动,输出赢家。

思路:

8600的最小必败态是n=1,n=1又可以转移到n=2,同样n=2可以转移到n=3,这样推下去,就是n是奇数是ailyanlu赢,否则8600赢。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n)&&n)
    {
        if(n&1)
            printf("ailyanlu\n");
        else
            printf("8600\n");
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
田忌赛马问题可以用矩阵博弈的方式来求解。假设田忌和齐王各有n匹马,马的速度不一样,田忌和齐王都知道各自马的速度,但不知道对方马的速度。现在要进行一场比赛,规则是田忌和齐王每次各选出一匹马进行比赛,速度快的获胜。每场比赛赢一分,平局不得分,输了不得分。比赛进行n场,求田忌最多能得多少分。 矩阵博弈的思路是构造一个n*n的矩阵,第i行第j列表示田忌用第i匹马与齐王用第j匹马比赛的得分。例如,第一行表示田忌用自己最快的马与齐王用不同的马比赛的得分,第二行表示田忌用自己第二快的马与齐王用不同的马比赛的得分,以此类推。 根据题意,构造比赛得分矩阵的方法如下: 1. 田忌用最快的马与齐王用最慢的马比赛,得分为1; 2. 田忌用第二快的马与齐王用第二慢的马比赛,得分为1; 3. 田忌用最慢的马与齐王用最快的马比赛,得分为0或-1。 注意,第三种情况得分为0或-1,是因为如果田忌用最慢的马与齐王用最快的马比赛,那么田忌必输,得分为-1;如果田忌用最慢的马与齐王用次慢的马比赛,那么田忌可能赢,得分为0或1。 根据上述方法可以构造比赛得分矩阵,然后使用线性规划的方法求解矩阵博弈问题。具体来说,可以将田忌和齐王的得分视为两个向量,将比赛得分矩阵视为一个矩阵,然后使用线性规划求解最大值问题。 以下是Matlab程序实现: ```matlab % 田忌赛马问题的矩阵博弈求解 n = 5; % 马匹数量 speeds = randperm(10, n); % 马的速度,随机生成 score_mat = zeros(n, n); % 得分矩阵 for i = 1:n for j = 1:n if i == j % 同一匹马不能比赛 continue; end if speeds(i) > speeds(j) % 田忌胜 score_mat(i, j) = 1; else % 田忌败 score_mat(i, j) = -1; end end end f = -ones(n, 1); % 目标函数 A = score_mat'; % 约束条件矩阵 b = ones(n, 1); % 约束条件向量 lb = zeros(n, 1); % 变量下界 ub = ones(n, 1); % 变量上界 options = optimset('Display', 'off'); % 不显示求解过程 x = linprog(f, A, b, [], [], lb, ub, options); % 求解线性规划问题 max_score = -sum(x); % 最大得分 disp(['田忌最多能得' num2str(max_score) '分']); ``` 该程序首先随机生成马的速度,然后根据上述方法构造比赛得分矩阵,最后使用Matlab内置函数linprog求解线性规划问题,得到田忌最多能得多少分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值