UVA11489 Integer Game简单博弈

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11489 Integer Game简单博弈相关的知识,希望对你有一定的参考价值。

Two players, S and T, are playing a game where they make alternate moves. S plays first.
    In this game, they start with an integer N. In each move, a player removes one digit from the integer and passes the resulting number to the other player. The game continues in this fashion until a player finds he/she has no digit to remove when that player is declared as the loser.
    With this restriction, its obvious that if the number of digits in N is odd then S wins otherwise T wins. To make the game more interesting, we apply one additional constraint. A player can remove a particular digit if the sum of digits of the resulting number is a multiple of 3 or there are no digits left.
    Suppose N = 1234. S has 4 possible moves. That is, he can remove 1, 2, 3, or 4. Of these, two of them are valid moves.
• Removal of 4 results in 123 and the sum of digits = 1 + 2 + 3 = 6; 6 is a multiple of 3.
• Removal of 1 results in 234 and the sum of digits = 2 + 3 + 4 = 9; 9 is a multiple of 3.
The other two moves are invalid.
    If both players play perfectly, who wins?
Input
The first line of input is an integer T (T < 60) that determines the number of test cases. Each case is a line that contains a positive integer N. N has at most 1000 digits and does not contain any zeros.
Output
For each case, output the case number starting from 1. If S wins then output ‘S’ otherwise output ‘T’.
Sample Input
3
4
33
771
Sample Output
Case 1: S
Case 2: T
Case 3: T

问题链接UVA11489 Integer Game
问题简述:给定一个数字串,两人玩游戏从中轮流取出数字,要求每次取完后剩下数字之和为3的倍数,不能取者输。先手胜则输出S,否则输出T。
问题分析:简单博弈问题,看题解,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11489 Integer Game */

#include <bits/stdc++.h>

using namespace std;

const int L = 10;
const int N = 1000;
char s[N + 1];
int cnt[N];

int main()
{
    int t, caseno = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%s", s);

        int sum = 0;
        memset(cnt, 0, sizeof cnt);
        for (int i = 0; s[i]; i++) {
            cnt[s[i] - '0']++;
            sum += s[i] - '0';
        }

        int flag = 0, i;
        for (; ;) {
            for (i = sum % 3; i < L; i += 3) {
                if (cnt[i] != 0) {
                    cnt[i]--;
                    sum -= i;
                    break;
                }
            }
            if (i >= L) break;
            flag = 1 - flag;
        }

        printf("Case %d: %c\\n", ++caseno, flag ? 'S' : 'T');
    }

    return 0;
}

以上是关于UVA11489 Integer Game简单博弈的主要内容,如果未能解决你的问题,请参考以下文章

UVA 12293 - Box Game(博弈)

UVa11489

UVa 11489 整数游戏

uva 1378 - A Funny Stone Game sg博弈

题解 UVa11489

UVA10891 Game of Sum(区间dp)