uva 1541 - To Bet or Not To Bet(记忆化+概率)

题目链接: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;
}

uva 1541 - To Bet or Not To Bet(记忆化+概率),布布扣,bubuko.com

时间: 2024-10-22 05:00:36

uva 1541 - To Bet or Not To Bet(记忆化+概率)的相关文章

UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每个点的权值就是当前强连通分量点的个数. /* Tarjan算法求有向图的强连通分量set记录了强连通分量 Col记录了强连通分量的个数. */ #include <iostream> #include<cstring> #include<cstdio> #include<string> #include<algo

【UVA】10651-Pebble Solitaire(直接递归或者记忆化)

不知道这个题UVA的数据是怎么的,用2个方法交了,第一次直接递归,第二次记忆化剪枝,时间竟然一样!? 直接郁闷了,简单的二进制表示状态和二进制运算. 14145176 10651 Pebble Solitaire Accepted C++ 0.009 2014-09-04 09:18:21 #include<cstdio> #include<algorithm> #include<string> #include<cstring> #include<m

uva 10285 Longest Run on a Snowboard (记忆化搜索)

uva 10285 Longest Run on a Snowboard 题目大意:给出一张n*m的雪地地图,每格标注的是该点的高度.从地势高的地方可以滑到地势低的地方(只能上下左右滑),问最长的滑雪距离. 解题思路:逐一访问各点,若该点没有被访问过,则进行DFS找出该点为滑雪起始点的最长滑雪距离,用dp数组记录,若该点已被访问过,则返回其对应的dp数组记录的值. #include <cstdio> #include <cstring> #include <algorithm

UVA - 11762 - Race to 1 记忆化概率

Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bedivided by at least one prime number less than or equal to that number. So, he is now playing withthis property. He selects a number N. And he calls th

UVa 10285 Longest Run on a Snowboard【记忆化搜索】

题意:和最长滑雪路径一样, 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algori

UVA 1541 - To Bet or Not To Bet(概率递推)

UVA 1541 - To Bet or Not To Bet 题目链接 题意:这题题意真是神了- -,看半天,大概是玩一个游戏,开始在位置0,终点在位置m + 1,每次扔一个硬币,正面走一步,反面走两步,走到的步上有4种情况: 1.向前走n步 2.向后走n步 3.停止一回合 4.无影响 问能在t次机会内,走到终点m + 1(如果跃过也算走到了)的概率,大于0.5,等于0.5,小于0.5对应不同输出 思路:题意懂了就好办了,其实就是递推就可以了dp[i][j]表示第i次机会,落在j步的概率,然后

UVA 1541 - To Bet or Not To Bet 记忆化DP概率

Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a new game. It is played on a linear sequence of squares as shown below. A chip is initially placed on the Start square. The player then tries to move th

UVA - 1541 To Bet or Not To Bet (DP+概率)

Description Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a new game. It is played on a linear sequence of squares as shown below. A chip is initially placed on the Start square. The player then trie

POJ1644:To Bet or Not To Bet(概率DP)

Description Alexander Charles McMillan loves to gamble, and during his last trip to the casino he ran across a new game. It is played on a linear sequence of squares as shown below. A chip is initially placed on the Start square. The player then trie