UVALive - 6913 博弈 && dp or Search

There was an interesting game played on a popular Korean reality TV Show. 12 players in 3 teams
— 4 persons in each team — lined up in one row in random order. The game master approaches the
players one by one starting from the most front player, with a tray full of 31 cakes. Each player should
eat at least 1 and at most 5 cakes during his/her turn. The player who eats the last cake will lose
along with his/her group (this is a team game). It was more an entertainment show rather than a real
competition. The players were selected from “chubby” celebrities who like to eat, thus causing them
in dilemma whether they should win the game (which might require them to hold their urge to eat the
cakes), or just enjoy all 5 cakes ignoring their team member.
This problem is related to the game. There are 2 teams (A and B) each with N players lined up
in a row in random order. Initially there are M cakes in the tray, and each player (starting from the
most front) has to eat at least 1 and at most K cakes during his/her turn. The team whose player eat
the last cake win (note that this is different compared to the original game).
Your task is to determine which team will win the game, given both teams play optimally.
Input
The first line of input contains an integer T (T ≤ 100) denoting the number of cases. Each case begins
with three integers N, M, and K (1 ≤ N ≤ 1, 000; 1 ≤ K ≤ M ≤ 2 ∗ N) in a line denoting the number
of players in each team, the initial number of cakes, and the maximum number of cakes can be eaten
by each player in his/her turn respectively. The next line contains a string S representing the players
order from the front most to the last player. S consists of only character ‘A’ or ‘B’ representing which
team does the respective player belongs to. The length of S is exactly 2 ∗ N and the number of ‘A’ and
‘B’ will be equal.
Output
For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is ‘A’ if the
game will be won by team A, otherwise ‘B’. Assume both teams will play the game optimally to win
the game.
Explanation for 1st sample case:
The first two players of team A each needs to eat 2 cakes, while the third A player eats the last
cake.
Explanation for 2nd sample case:
No matter what team A do, the last cake will be eaten by one of team B’s player.
Explanation for 3rd sample case:
To ensure their win, the first player (B) should eat 2 cakes, leaving only 3 cakes to the next player
(A). This player (A) should eat at least 1 and at most 2 cakes. No matter how many cakes this player
eats, the next player (B) will eat the last cake.
Sample Input
4
3 5 2
AAABBB
4 7 2
AAABBBBA
4 5 2
BABABABA
4 6 3
BAABBABA
Sample Output
Case #1: A
Case #2: B
Case #3: B
Case #4: A

思路就是 dfs 搜索每一步的必败或者必胜态;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 2005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>
#define lson 2*x
#define rson 2*x+1

long long  qupower(ll a, ll b,ll mod) {
    long long  ans = 1;
    while (b) {
        if (b & 1)ans = ans * a%mod;
        b >>= 1;
        a = a * a%mod;
    }
    return ans;
}

inline int read() {
    int an = 0, x = 1; char c = getchar();
    while (c > '9' || c < '0') {
        if (c == '-') {
            x = -1; 
        }
        c = getchar();
    }
    while (c >= '0'&&c <= '9') {
        an = an * 10 + c - '0'; c = getchar();
    }
    return an * x;
}

int n, m, k;
int dp[maxn][maxn];
char ch[maxn];
int v[maxn];

int dfs(int who, int left) {
    if (dp[who][left] != -1)
        return dp[who][left];
    if (left <= v[who] * k) {
        return dp[who][left] = 1;
    }
    dp[who][left] = 0;
    for (int i = v[who]; i <= v[who] * k; i++) {
        if (dfs(who + 1, left - i) == 0)
            return dp[who][left] = 1;
    }
    return dp[who][left];
}

int main() {
    //ios::sync_with_stdio(false);
    int t;
    //t = read();
    scanf("%d", &t);
    int cnt = 0;
    while (t--) {
        cnt++;
        //n = read(); m = read(); k = read();
        scanf("%d%d%d", &n, &m, &k);
        //cin >> ch;
        scanf("%s", ch);
        int flag = 1;
        int tot = 0;
        ms(v); 
        memset(dp, -1, sizeof(dp));

        for (int i = 1; i < 2 * n; i++) {
            if (ch[i - 1] == ch[i])flag++;
            else {
                v[tot++] = flag;
                flag = 1;
            }
        }

        v[tot++] = flag;
        //cout << "Case #" << cnt << ": ";
        printf("Case #%d: ", cnt);
        if (dfs(0, m)) {
            if (ch[0] == 'A')printf("A\n");
            else printf("B\n");
        }
        else {
            if (ch[0] == 'B')printf("A\n");
            else printf("B\n");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值