uva 1559 - Nim(記憶化+博弈)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   ar   line   

題目連結:uva 1559 - Nim

題目大意:有n個人,奇數的為一隊,偶數的為一對,兩隊分別從一堆石子個數為S的石子堆中取石子,取到最後一個石子一方則視為失敗。給出各個隊員每次可取石子的上限值,然後按照順序操作。

解題思路:dp[i][s]表示第i個選手操作時剩s個石子時為必勝還是必敗。因為是取到最後一個石子的為輸,所以最後遞迴結束的條件和不同的略有不同。
還嘗試過可以將石子數減1,然後普通處理也是可以。

/******************* * 取到最後一個石子的輸 * ****************/#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 30;const int maxs = 1<<14;int N, S, m[maxn];int dp[maxn][maxs];void init () {    memset(dp, -1, sizeof(dp));    scanf("%d", &S);    for (int i = 0; i < 2 * N; i++)        scanf("%d", &m[i]);}bool dfs (int d, int s) {    if (s <= 0)        return 1;    if (d >= 2 * N)        d -= 2 * N;    if (dp[d][s] != -1)        return dp[d][s];    dp[d][s] = 0;    for (int i = 1; i <= m[d]; i++) {        if (dfs(d + 1, s - i))            continue;        dp[d][s] = 1;    }    return dp[d][s];}int main () {    while (scanf("%d", &N) == 1 && N) {        init();        printf("%d\n", dfs(0, S) ? 1 : 0);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.