uva1637Double Patience
状态压缩,记忆化搜索。
用一个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的更多相关文章
- UVA1637Double Patience(概率 + 记忆化搜索)
训练指南P327 题意:36张牌分成9堆, 每堆4张牌.每次拿走某两堆顶部的牌,但需要点数相同.如果出现多种拿法则等概率的随机拿. 如果最后拿完所有的牌则游戏成功,求成功的概率. 开个9维数组表示每一 ...
- POJ2794 Double Patience[离散概率 状压DP]
Double Patience Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 694 Accepted: 368 Cas ...
- UVA127- "Accordian" Patience(模拟链表)
"Accordian" Patience You are to simulate the playing of games of ``Accordian'' patience, t ...
- [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...
- UVA127-"Accordian" Patience(模拟)
Problem UVA127-"Accordian" Patience Accept:3260 Submit:16060 Time Limit: 3000 mSec Proble ...
- 文本diff算法Patience Diff
一般在使用 Myers diff算法及其变体时, 对于下面这种例子工作不是很好, 让变化不易阅读, 并且容易导致合并冲突 void Chunk_copy(Chunk *src, size_t src_ ...
- ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- 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 ...
随机推荐
- UML: CIM & PIM
CIM-1:定义业务流程 定义及分析业务流程(Business Process)是为了尽快理清系统范围,以便估算开发成本及时间,可不是为了要改造业务流程.系统分析员千万别误解了此步骤的目的.所以,系统 ...
- 【DP/二分】BZOJ 1863:[Zjoi2006]trouble 皇帝的烦恼
863: [Zjoi2006]trouble 皇帝的烦恼 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 465 Solved: 240[Submit][ ...
- sampler2d
Here is the syntax for a sampler in Direct3D 9. sampler Name = SamplerType{ Texture = <texture_ ...
- Unity3D脚本中文系列教程(十一)
http://dong2008hong.blog.163.com/blog/static/4696882720140313058768/ BoxCollider 类,继承自Collider 一个盒状的 ...
- SOAP vs REST
Both methods are used by many of the large players. It's a matter of preference. My preference is RE ...
- HDU4725 The Shortest Path in Nya Graph SPFA最短路
典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...
- Linux下c++通过动态链接库调用类
http://hi.baidu.com/ablenavy/item/b498901c6826bbf587ad4e33 Linux下的动态链接库叫so,即Shared Object,共享对象.一些函数就 ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- 【转载】jxl操作excel 字体 背景色 合并单元格 列宽等 .
package com.email.jav; import java.io.File;import java.io.IOException;import java.net.URL; import jx ...
- 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...