UVA - 1637 概率dp(算是吧)
这道题map的用法和读入的技巧需要记录
题意 九堆牌,每堆四张,每次消去表面两张相同的,看能否全部消除,求概率
map可以存一个数组状态
#include<cstdio> #include<string.h> #include<cmath> #include<vector> #include<map> #include<stack> using namespace std; #define MST(vis,x) memset(vis,x,sizeof(vis)) #define eps 1e-6 map<vector<int>,double>ma; char s1[10][5][3]; double dp(vector<int> & vec,int C) { if(C==0)return 1.0; if(ma.count(vec)!=0)return ma[vec]; double sum=0; int all=0; for(int a=1;a<=9;a++)if(vec[a]>0) { for(int b=a+1;b<=9;b++)if(vec[b]>0) { char temp1=s1[a][vec[a]][0]; char temp2=s1[b][vec[b]][0]; // printf("%s %s\n",s1[a][vec[a]],s1[b][vec[b]]); // printf("%c %c\n",temp1,temp2); if(s1[a][vec[a]][0]==s1[b][vec[b]][0]) { vec[a]--;vec[b]--; all++; sum+=dp(vec,C-2); vec[a]++,vec[b]++; } } } if(all==0)return ma[vec]=0; return ma[vec]=sum/all; } bool read() { for(int a=1; a<=9; a++) for(int b=1; b<=4; b++) if(scanf("%s",s1[a][b])!=1) return false; return true; } int main() { while(read()) { vector<int> vec(10,4); ma.clear(); printf("%.6f\n",dp(vec,36)); } return 0; }