gpt4 book ai didi

c - Alice Bob Coin 最优解

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:07 24 4
gpt4 key购买 nike

考虑我在 hackerrank 上发现的这个问题: Coins Problem

Alice and Bob were sitting in the sun; drinking orange juice; and watching some migrating ducks fly to Africa. “Look”, noted Alice, “one of the ducks left a trail of golden coins on the floor”. “Great!” exclaimed Bob, “let‟s play a game with this line of coins. We will take turns, where each one of us will flip one coin from „head‟ into „tail‟ state”. “Ok”, agreed Alice and added, “but when we flip a coin, we can also opt to flip the coin immediately after it, even if that coin is a „tail‟, in which case it becomes a „head‟”. “And whoever can not play - loses” cried both of them simultaneously. Cunning Bob knew that he could count on witty IEEEXtreme contestants to help him win. Can you help him do that? Task Your task is to write a program that given a string of H/T letters, computes a winning move for the flip-coin game, if there is one, or reports that there in no winning move, if this is the case. A winning move is a legal move such that either the player wins immediately (because there are no more coins to flip), or else, after any subsequent move by the opponent there is a winning move for the player.

例如,如果输入是 TTTT 那么 Bob 输了游戏(没有 “头”所以他不能玩,因此他输了)。对于输入 TTTTHTTTT, Bob 掷第五枚硬币获胜;对于输入 TTHHT,Bob 获胜 翻转两个“正面”(第三个和第四个硬币);对于输入 THHTTHHT,如果 Bob 掷硬币 2 和 3,他就赢了。

Input 要从控制台读取的输入文件包含一行 其中有一个完全由字母 H 和 T 组成的字符串, 代表代币的状态,如前所述。

Output 要在控制台写入的输出文件包含一个 行,有一个数字。正数 N 表示翻转第 N 个 硬币是一个获胜的举动。一个负数,写作-N,意味着 翻转第 N 和第 N+1 个硬币是获胜的一步。零,书面 0,表示没有必胜棋。请注意,一般情况下,有 对于给定的硬币列表,可以是几个获胜的 Action 。你的程序 可以输出其中任何一个。

我尝试了递归回溯解决方案,但是“C”由于 Stackoverflow 引发了段错误。这是我的代码:(部分有效)

    #include <stdio.h>

int main()

{

char a[100];

scanf("%s",a);

printf("%d",check(a));



}

int check(char a[])

{

//printf("%s\n",a);

int i=0;

for(i=0;i<strlen(a);i++){

if(a[i]=='H'){

a[i]='T';

a[i+1]=a[i+1]=='H'?'T':'H';

if(check(a)){

a[i+1]=a[i+1]=='H'?'T':'H';

if(check(a))a[i]='H';

else return (i+1);

}

else return -(i+1);

}

}

//printf("Lost\n");

return 0;

}

谁能帮帮我?

最佳答案

您没有正确撤消您尝试的所有 Action 。如果你不撤消这些 Action ,你就永远无法原路返回。

此外,您正在从用作 bool 函数的 check 返回数字。

这是一个有效的示例。

#include <stdio.h>
#include <string.h>

//#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define FLIP(x) ( ((x)=='H') ? 'T' : 'H' )


bool check(char* a) {
DEBUG("%s\n",a);

// Loop over the string.
for (int i=0; i<strlen(a); i++) {
// If this coin is a head, try flipping it.
if (a[i]=='H') {
// Try flipping just this coin.
a[i]=FLIP(a[i]);
// See if it is a win or a loss for the other player.
if (!check(a)) {
// A loss for the other player means a win for us!
// Undo our last move.
a[i]=FLIP(a[i]);
return true;
}
// A win for the other player.
// See if flipping the next coin makes a difference.
if (i+1 < strlen(a)) {
a[i+1]=FLIP(a[i+1]);
// See if it is a win or a loss for the other player.
if (!check(a)) {
// A loss for the other player means a win for us!
// Undo our last two moves.
a[i]=FLIP(a[i]);
a[i+1]=FLIP(a[i+1]);
return true;
}
// Still a loss. Undo this move.
a[i+1]=FLIP(a[i+1]);
}
// Still a loss. Undo this move.
a[i]=FLIP(a[i]);
} // if (a[i]=='H')
} // for (int i=0; i<strlen(a); i++)

// Loss.
return false;
}

int main() {
char a[100];
scanf("%s",a);
if (check(a)) {
printf("Winner!\n");
} else {
printf("Loser!\n");
}
}

关于c - Alice Bob Coin 最优解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21585587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com