uva 1541,tda1541

來源:互聯網
上載者:User

uva 1541,tda1541

題目連結:uva 1541 - To Bet or Not To Bet

題目大意:在一個棋盤上進行遊戲,給定棋盤長度m,不算起始和終止,以及走的步數t。從起點開始,每輪可以丟一枚硬幣,正面移動2步,方面移動1步;中間的格子有寫操作,包括移動一定步數,停止一次操作。問說在t步內到達終點的機率。

解題思路:dp[i][j]表示走到第i格用掉j步的機率,然後記憶化搜尋,因為保證狀態重複,並且可以確定遞迴終止條件。

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 100;bool lose[maxn], vis[maxn][maxn];double p[maxn][maxn];int N, T, inst[maxn];void init () {    char str[maxn];    memset(inst, 0, sizeof(inst));    memset(vis, false, sizeof(vis));    memset(lose, false, sizeof(lose));    scanf("%d%d", &N, &T);    for (int i = 1; i <= N; i++) {        scanf("%s", str);        if (str[0] == 'L')            lose[i] = true;        else            sscanf(str, "%d", &inst[i]);    }}double dfs (int d, int k) {    if (vis[d][k])        return p[d][k];    vis[d][k] = true;    if (d == N + 1)        return p[d][k] = 1;    if (k <= 0)        return p[d][k] = 0;    double& ret = p[d][k];    ret = 0;    int next = d + 1;    if (lose[next])        ret += 0.5 * dfs(next, k-2);    else        ret += 0.5 * dfs(next + inst[next], k - 1);    next = min(d + 2, N + 1);    if (lose[next])        ret += 0.5 * dfs(next, k-2);    else        ret += 0.5 * dfs(next + inst[next], k - 1);    return ret;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        init();        double ans = dfs(0, T);        if (fabs(ans - 0.5) < 1e-9)            printf("Push. 0.5000\n");        else if (ans > 0.5)            printf("Bet for. %.4lf\n", ans);        else            printf("Bet against. %.4lf\n", ans);    }    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.