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 ...
随机推荐
- Ubuntu 12.4 下升级 Subversion 1.7
Ubuntu 12.04 默认使用的是Subversion 1.6,而Ubutnu12.10开始,就使用的是Subversion 1.7. 如果从别人的地方拷过来的SVN目录,在使用SVN命令时会报以 ...
- Hadoop以及其外围生态系统的安装参考
在研究Hadoop的过程中使用到的参考文档: 1.Hadoop2.2 参考文档 在CentOS上安装Hadoop 2.x 集群: http://cn.soulmachine.me/blog/201 ...
- struts2+hibernate+spring+jquery返回json List列表
1.引入包:struts2-json-plugin-2.1.8.1.jar json-lib-2.1.jar commons-collections-3.2.1.jar commons-beanuti ...
- linux源码阅读笔记 move_to_user_mode()解析
在linux 0.11版本源代码中,在文件linux/include/asm/system.h中有一个宏定义 move_to_user_mode() 1 #define move_to_user_m ...
- cf div2 235 D
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- C#图片上写文字
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- Oracle - 位图索引的适用条件
位图索引的适用条件 位图索引适合只有几个固定值的列,如性别.婚姻状况.行政区等等,而身份证号这种类型不适合用位图索引. 位图索引适合静态数据,而不适合索引频繁更新的列. 举个例子,有这样一个字段bus ...
- hdu1020 Encoding
http://acm.hdu.edu.cn/showproblem.php?pid=1020 过了的就是好孩子........ #include<stdio.h> #include< ...
- 220 DIV2 B. Inna and Nine
220 DIV2 B. Inna and Nine input 369727 output 2 input 123456789987654321 output 1 题意:比如例子1:369727--& ...
- [转]Java数组初始化详解
一维数组1) int[] a; //声明,没有初始化 2) int[] a=new int[5]; //初始化为默认值,int型为0 3) int[] a={1,2,3,4,5}; ...