//思路:通过博弈的思想设计出状态,dp[w][b] 表示当公主面对 w 只白鼠和 b 只黑鼠时获胜的概率
1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 using namespace std; 6 double dp[1010][1010]; 7 int w, b; 8 9 int main() 10 { 11 int i, j; 12 scanf("%d%d", &w, &b); 13 for(i = 1; i <= w; ++i) 14 dp[i][0] = 1; 15 // for(i = 0; i <= b; ++i) 16 // dp[0][1] = 0; 17 for(i = 1; i <= w; ++i) { 18 for(j = 1; j <= b; ++j) { 19 dp[i][j] = (double)i / (i + j); 20 if(j >= 2) 21 dp[i][j] += dp[i - 1][j - 2] * (double)j / (i + j) * (double)(j - 1) / (i + j - 1) * (double)i / (i + j - 2); 22 if(j >= 3) 23 dp[i][j] += dp[i][j - 3] * (double)j / (i + j) * (double)(j - 1) / (i + j - 1) * (double)(j - 2) / (i + j - 2); 24 } 25 } 26 printf("%.9f\n", dp[w][b]); 27 }
//记忆化搜索直接在 100 100 的数据上 T 了...,是我写的太丑了吗......
1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 using namespace std; 6 double dp[1010][1010]; 7 bool done[1010][1010]; 8 int w, b; 9 10 double dfs(int w, int b) 11 { 12 if(w <= 0) 13 return 0; 14 if(b <= 0) 15 return 1; 16 if(done[1010][1010]) 17 return dp[w][b]; 18 done[w][b] = 1; 19 return dp[w][b] = (double) w / (w + b) + dfs(w - 1, b - 2) * (double) b / (w + b) * (b - 1) / (w + b - 1) * (double) w / (w + b - 2) + dfs(w, b - 3) * (double) b / (w + b) * (b - 1) / (w + b - 1) * (double) (b - 2) / (w + b - 2); 20 } 21 22 int main() 23 { 24 int i, j; 25 scanf("%d%d", &w, &b); 26 printf("%.9f\n", dfs(w, b)); 27 }