棋盘游戏...但另一种

标签 c algorithm game-theory

https://www.hackerrank.com/challenges/chessboard-game-again-1

我已经按照以下方式尝试了上述问题,但答案被评估为错误。(我不是在要求解决方案,而是在要求方法中的缺陷);

我的代码(请忽略c99错误)

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int numofmov = 0;
int issafe(int a,int b){
    if(a>=0 && a<15 && b>=0 && b<15)
        return 1;
    return 0;
}
void move(int board[][15]){
    for(int i=0;i<15;i++){
        for(int j=0;j<15;j++){
            if(board[i][j]>0){
                board[i][j]--;
                if(issafe(board,i-2,j+1)==1) {
                    numofmov++;
                    board[i-2][j+1]++;
                }
                if(issafe(board,i-2,j-1)==1) {
                    numofmov++;
                    board[i-2][j-1]++;
                }                
                if(issafe(board,i+1,j-2)==1) {
                    numofmov++;
                    board[i+1][j-2]++;
                }
                if(issafe(board,i-1,j-2)==1) {
                    numofmov++;
                    board[i-1][j-2]++;
                }
            }
        }
    }
}
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int t;
    scanf("%d",&t);
    while(t--){
        int k;
        scanf("%d",&k);
        int board[15][15];
        for(int j=0;j<15;j++){
            for(int h=0;h<15;h++){
                board[j][h]=0;
            }
        }
        for(int i=0;i<k;i++){
            int x,y;
            scanf("%d %d",&x,&y);
            board[x-1][y-1]++;
        }
        int bro=0,mov=numofmov;
        while(bro==0){
            move(board);
            if(numofmov==mov){
                bro++;
                printf("Second\n");
                break;
            }
            mov = numofmov;
            move(board);
            if(numofmov==mov){
                bro++;
                printf("First\n");
                break;
            }
            mov = numofmov;
        }
    }
    return 0;
}

我的方法是继续使所有硬币的所有移动成为可能,直到我们到达无法移动的地步。但这在某些测试用例中给出了错误的答案。

最佳答案

您是在问这种方法有什么问题吗?

"My approach is to go on making all the moves possible for all the coins until we come to a point when no moves are possible. But this is giving wrong answers in some test cases."

我没有阅读你的代码,但我可以说主要问题是你的方法本身。您将这个问题视为暴力(制定所有可能的移动路径,然后看看谁获胜)。可能的移动数量可以是任意大,检查移动是否导致获胜是无限慢的。实际上,它要么是动态规划问题,要么是更相关的博弈论问题。 这样想吧。起始位置是否唯一确定了这场比赛的获胜者?如果我改变单个硬币的初始位置,获胜者也会改变吗?

解决此类问题的最佳方法是简化它。假设只有一 block 板,有一枚硬币,位于(x,y)。 。现在请注意,每次从位置 (x,y) 移动一枚硬币后至位置(a,b) ,以下说法正确 a+b<x+y 。所以如果 (x,y)(1,1),(1,2),(2,1),(2,2) 之一,采取行动的玩家已经输了。所以我的目标是确保我的对手会从已经失败的位置采取行动,如果我能做到这一点,我就处于获胜位置。如果您遵循相同的逻辑,您将意识到这种方法将唯一地识别仓位是赢还是输。因此,对于任何位置,我们都可以简单地从 (1,1) 向后构建答案网格来回答它是赢还是输。至(15,15)

现在如果板子的数量不止一个你会怎么做?你需要深入研究博弈论,特别是Grundy数字以及它们与 Nim 的关系游戏。我建议您查看以下链接以获取更多信息:

https://en.wikipedia.org/wiki/Nim

https://en.wikipedia.org/wiki/Nimber

https://www.topcoder.com/community/data-science/data-science-tutorials/algorithm-games/

关于棋盘游戏...但另一种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430502/

相关文章:

game-theory - 资讯不完整的游戏策略

algorithm - 博弈论 : MEX rule and Nimbers

c - 如何避免多线程

c++ - 编译用于高放射性环境的应用程序

algorithm - 找到所有 4 位吸血鬼数字

algorithm - 三胞胎的数量

python - 重复整数列表

python - 如何解决 python axelrod Lookerup 策略中的关键错误

c - 将 wchar 打印到 Linux 控制台?

检查 Windows 对象 DACL 中是否存在 ACE