Ultraviolet A 1541-to bet or not to bet (memory + probability)

Source: Internet
Author: User

Link to the question: Ultraviolet A 1541-to bet or not to bet

A game is played on a board. Given the length of the board m, it is not counted as the start and end, and the number of steps T. From the starting point, one coin can be lost in each round, and two steps can be moved on the front. One step is moved on the front. The middle lattice has a write operation, including moving a certain number of steps to stop an operation. The probability of reaching the end point in step T.

Solution: DP [I] [J] indicates the probability that step J is used to get to the I lattice, and then the memory is searched because the State is repeated and recursive termination conditions can be determined.

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.