状态压缩,记忆化搜索。

用一个5进制数来表示每堆排到了哪一个位置。和2进制是一样的,不过不能用位运算。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 2000000+10; double dp[maxn];
bool vis[maxn];
char s[10][10][5];
int v,v2; double dfs(int n) {
v++;
if(n==0) return 1.0;
if(vis[n]) {v2++; return dp[n];}
vis[n]=1; int tmp=n,cnt=0;
int a[10];
for(int i=1;i<=9;i++) {
a[i]=tmp%5;
tmp/=5;
}
for(int i=1;i<9;i++)
for(int j=i+1;j<=9;j++)
if(a[i] && a[j] && s[i][a[i]][0]==s[j][a[j]][0]) {
tmp=0;
for(int k=9;k>=1;k--) {
tmp*=5;
if(k==i || k==j) tmp+=a[k]-1;
else tmp+=a[k];
}
dp[n]+=dfs(tmp);
cnt++;
}
if(cnt) dp[n]=dp[n]/(1.0*cnt);
return dp[n];
} int main() {
while(scanf("%s",s[1][1])==1) {
for(int i=2;i<=4;i++) scanf("%s",s[1][i]);
for(int i=2;i<=9;i++)
for(int j=1;j<=4;j++)
scanf("%s",s[i][j]);
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
dfs(1953124);
printf("%.6lf\n",dfs(1953124));
}
return 0;
}

uva1637Double Patience的更多相关文章

  1. UVA1637Double Patience(概率 + 记忆化搜索)

    训练指南P327 题意:36张牌分成9堆, 每堆4张牌.每次拿走某两堆顶部的牌,但需要点数相同.如果出现多种拿法则等概率的随机拿. 如果最后拿完所有的牌则游戏成功,求成功的概率. 开个9维数组表示每一 ...

  2. POJ2794 Double Patience[离散概率 状压DP]

    Double Patience Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 694   Accepted: 368 Cas ...

  3. UVA127- "Accordian" Patience(模拟链表)

    "Accordian" Patience You are to simulate the playing of games of ``Accordian'' patience, t ...

  4. [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience

    题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...

  5. UVA127-"Accordian" Patience(模拟)

    Problem UVA127-"Accordian" Patience Accept:3260  Submit:16060 Time Limit: 3000 mSec Proble ...

  6. 文本diff算法Patience Diff

    一般在使用 Myers diff算法及其变体时, 对于下面这种例子工作不是很好, 让变化不易阅读, 并且容易导致合并冲突 void Chunk_copy(Chunk *src, size_t src_ ...

  7. ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  8. ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  9. Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟

    Input Input data to the program specifies the order in which cards are dealt from the pack. The inpu ...

随机推荐

  1. 升级iOS10之后调用摄像头/麦克风等硬件程序崩溃闪退的问题

    在升级到iOS10之后, 开发过程中难免会遇到很多的坑, 下面是一些常见的坑, 我做了一些整理, 希望对大家开发有帮助: &1. 调用视频,摄像头, 麦克风,等硬件程序崩溃闪退的问题: 要注意 ...

  2. bnuoj 33656 J. C.S.I.: P15(图形搜索题)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...

  3. [转载]C# FTP操作工具类

    本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...

  4. hdu 2196

    树形dp 本文出自   http://blog.csdn.net/shuangde800 题目传送门 题意: 给出一棵树,求离每个节点最远的点的距离 思路: 把无根树转化成有根树分析, 对于上面那棵树 ...

  5. NGINX的奇淫技巧 —— 5. NGINX实现金盾防火墙的功能(防CC)

    NGINX的奇淫技巧 —— 5. NGINX实现金盾防火墙的功能(防CC) ARGUS 1月13日 发布 推荐 0 推荐 收藏 2 收藏,1.1k 浏览 文章整理中...... 实现思路 当服务器接收 ...

  6. Maintainable HashCode and Equals Using Apache Commons

    Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs ...

  7. Lua print on the same line

    In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we h ...

  8. prim求MST

    PRIM==>>MST模板 #include <iostream> using namespace std; #define typec int #define V 3 con ...

  9. **使用 Git Hook 实现网站的自动部署

    http://www.tuicool.com/articles/3QRB7jU 自动化能解放人类的双手,而且更重要的是,因为按照规定的流程来走,也减少了很多误操作的产生.不知道大家平时都是怎么样更新自 ...

  10. lintcode:递归打印数字

    题目 用递归打印数字 用递归的方法找到从1到最大的N位整数. 样例 给出 N = 1, 返回[1,2,3,4,5,6,7,8,9]. 给出 N = 2, 返回[1,2,3,4,5,6,7,8,9,10 ...