UVa1637 - Double Patience(离散概率)

用九元组表示当前状态,即每队牌剩的张数,状态总数为5^9=1953125.

设d[ i ]表示状态i对应的成功概率,则根据全概率公式,d[ i ]为后继成功概率的平均值,按照动态规划的写法计算即可。

既然求的是成功的平均概率。 拿 第一行来说  点数可能是 1 2 3 4 5 6 7 1  1. 那么 取走 第一位的1 和倒数第二位1的成功概率为p1 第一位与最后一位为p2 最后两位为p3

那么平均概率为 (p1+p2+p3) / 3;

自然后面每次情况都是这样。 递归求解。   用 九维数组来表示。 每一维 大小为4 代表剩余几张牌。 记忆化搜索。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
double dp[5][5][5][5][5][5][5][5][5] = {0};
int vis[5][5][5][5][5][5][5][5][5];
vector<char> q[10];
double d(int w1,int w2,int w3,int w4,int w5,int w6,int w7,int w8,int w9)
{
    if(vis[w1][w2][w3][w4][w5][w6][w7][w8][w9])
        return dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
    vis[w1][w2][w3][w4][w5][w6][w7][w8][w9]=1;
    int top[10]={w1,w2,w3,w4,w5,w6,w7,w8,w9};
    int flag=0;
    double & x = dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
    for(int i=0;i<9;i++){
        if(top[i]){
            flag=1;
            break;
        }
    }
    if(!flag){
        return x=1;
    }
    int hap=0;
    for(int i=0;i<8;i++){
        if(top[i]){
            for(int j=i+1;j<9;j++){
                if(top[j]&&q[i][top[i]-1]==q[j][top[j]-1]){
                    hap++;
                    top[i]--,top[j]--;
                    x += d(top[0],top[1],top[2],top[3],top[4],top[5],top[6],top[7],top[8]);
                    top[i]++,top[j]++;
                }
            }
        }
    }

    if(x==0) return x=0;
    else return x = x*1.0 / (hap*1.0);
}
int main()
{
    char s1[10],s2[10],s3[10],s4[10];
    while(scanf("%s%s%s%s",s1,s2,s3,s4)!=EOF){
        q[0].push_back(s1[0]);
        q[0].push_back(s2[0]);
        q[0].push_back(s3[0]);
        q[0].push_back(s4[0]);
        for(int i=1;i<=8;i++){
            scanf("%s%s%s%s",s1,s2,s3,s4);
            q[i].push_back(s1[0]);
            q[i].push_back(s2[0]);
            q[i].push_back(s3[0]);
            q[i].push_back(s4[0]);
        }
        printf("%lf\n",d(4,4,4,4,4,4,4,4,4));
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<10;i++)
            q[i].clear();
    }
    return 0;
}

时间: 2024-08-03 01:59:26

UVa1637 - Double Patience(离散概率)的相关文章

1637 - Double Patience (概率DP)

一道状态较多的概率DP,想要表示所有的状态显然要拓展几个维度表示九堆牌当前的状态 . 但是这么写太复杂,所以我们不妨用一个vector来储存状态,将dp数组用一个map来表示,即 map<vector<int> ,double> d; 利用vector可以作为函数参数传递这个优点,将大大节省代码量 . 概率很好求,在每一次迭代中,寻找所有可以转移的状态数tot,那么状态转移就是d[i] = sum(d[i-1])/tot . 也就是全概率公式 . 递归边界是当所有牌都被摸走了,返回

[Uva1637][DFS][记忆化] 纸牌游戏 Double Patience

写代码一定要注意!!!!!! 我因为i+1写成了1+1改了一晚上!!!!!!(菜都写脸上了) 题目: Double Patience是一种单人游戏,使用标准的36张牌组.这些牌在洗牌后放在一张桌子上,叠成9叠,每叠4张,面朝上. 牌放下后,玩家转身.每一次,他可以从任意两个牌堆中取出同一等级的顶级牌,然后将它们移除.如果有几种可能性,玩家可以选择任何一种.如果所有的牌都从桌上移除,玩家将赢得游戏,如果一些牌仍然在桌上,并且没有有效的移动,玩家将失败. 乔治喜欢这种游戏.但当有几种可能时,他不知道

UVA 1637 Double Patience 概率DP

Double Patience Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Double Patience is a single player game played with a standard 36-card deck. The cards are shuffled and laid down on a table in 9 pile

1637 - Double Patience(状态转移+求成功概率)

用九元组表示当前状态,即每队牌剩的张数,状态总数为5^9=1953125. 设d[ i ]表示状态i对应的成功概率,则根据全概率公式,d[ i ]为后继成功概率的平均值,按照动态规划的写法计算即可. 这个动态规划我不会写,帅哥思路真的很清晰很好啊. 但是学会还是很高兴的 #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; #d

UVa 1637 (概率) Double Patience

题意: 一共有9堆牌,每堆牌四张.每次可以取堆顶点数相同的两张牌,如果有多种方案则选取是随机的. 如果最后将所有牌取完,则视为游戏胜利,求胜利的概率. 分析: 用一个九元组表示状态,分别代表每堆牌剩余的牌数.根据全概率公式,d[i]为后继状态成功概率的平均值. 1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <vector> 5 using namespace std

uva 1637 Double Patience

https://vjudge.net/problem/UVA-1637 36张牌分成9堆,每堆4张牌.每次可以拿走某两堆顶部的牌,但需要点数相同. 如果有多种拿法则等概率的随机拿. 如果最后拿完所有牌则游戏成功.按顺序给出每堆牌的4张牌,求成功概率. #include<cstdio> using namespace std; char s[10][5]; char ss[5]; int left[10]; int v[2000001]; double dp[2000001]; int t; i

UVA11021 Tribles[离散概率 DP]

UVA - 11021 Tribles GRAVITATION, n. “The tendency of all bodies to approach one another with a strength proportion to the quantity of matter they contain – the quantity of matter they contain being ascertained by the strength of their tendency to app

UVA&amp;&amp;POJ离散概率练习[3]

POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问题 |00|+|01|=|0| 注意序列是环形 // // main.cpp // poj3869 // // Created by Candy on 25/10/2016. // Copyright © 2016 Candy. All rights reserved. // #include <i

●POJ 2794 Double Patience

题链: http://poj.org/problem?id=2794题解: 状压DP,概率 9元组表示每一堆还剩几张牌.可以用5进制状压,共5^9=1953124个状态. 令P(S)表示S这个状态被取完的概率. 假设当前状态为S,可以有三种取法,分别对应转移到_S1,_S2,_S3三个更小的状态. 那么由全概率公式: 如果_S1状态为前提条件,那么S就有1/3的概率转移到_S1 同理_S2,_S3.所以得出: P(S)=P(S|_S1)*P(_S1)+P(S|_S2)*P(_S2)+P(S|_S