codeforce gym 101726 problem B. Spy Duel

B. Spy Duel
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Alexey and Boris were two KGB agents that lived in Ekaterinburg in the 70's. The city was pretty small, and as nothing happened, to avoid dying of boredom the agents decided to create a dice game. In that game each of them started with VA and VB hit points, respectively. Each had in their disposal some attacks, and they alternated in attacking each other. Each attack is described by a number of dice. To determine the damage of the attack, roll those dice and their sum is the damage.

To play, they have available honest dice with 1 to 12 faces. That is, if a die with L faces is rolled it will show an integer between 1 and Lwith the same probability, and independent of any dice rolled before.

Both players know their attack and their opponents and choose how to attack each turn in a way that will maximize their probability of winning. Your task is to determine that probability.

Input

In the first line an integer T, the number of test cases.

In the first line of each case, four integers VAVBNA and NB. Each of the next NA lines describes an attack Alexey has, the other NBlines describe the attacks Boris has.

Each attack is described by an integer D followed by D integers L1, ..., LD, meaning that to attack the player will roll D dice, with faces L1, ... LD.

Limits

  • 1 ≤ T ≤ 5
  • 1 ≤ VA, VB ≤ 300
  • 1 ≤ NA, NB ≤ 10
  • 1 ≤ D ≤ 3
  • 1 ≤ Li ≤ 12
Output

For each case, print a single line with the probability that Alexey will win the duel, assuming he starts and both players play optimally. It will be considered correct if the absolute or relative error does not exceed 10 - 6.

Example
input
Copy
2
2 12 2 1
1 12
3 4 4 5
2 1 1
5 5 1 2
1 6
2 3 5
2 1 6
output
Copy
0.0833333333
0.5339917695
思路:记忆化搜索,存三个状态:两个人的血量以及当前轮到谁,值为对应赢的概率。
  1 #include <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <string>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <stack>
 12 #include <vector>
 13 #include <set>
 14 #include <map>
 15 #include <list>
 16 #include <iomanip>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <bitset>
 20 #include <ctime>
 21 
 22 using namespace std;
 23 
 24 #define pau system("pause")
 25 #define ll long long
 26 #define pii pair<int, int>
 27 #define pb push_back
 28 #define mp make_pair
 29 #define clr(a, x) memset(a, x, sizeof(a))
 30 
 31 const double pi = acos(-1.0);
 32 const int INF = 0x3f3f3f3f;
 33 const int MOD = 1e9 + 7;
 34 const double EPS = 1e-9;
 35 
 36 
 37 int t, v[2], n[2];
 38 struct fuck {
 39     int d;
 40     int a[4];
 41     double p[37];
 42 } f[2][11];
 43 double mmp[305][305][2];
 44 double solve(int v1, int v2, int s) {
 45     if (v1 <= 0 || v2 <= 0) return 0;
 46     if (-EPS < mmp[v1][v2][s]) return mmp[v1][v2][s];
 47     double res = 0;
 48     for (int i = 1; i <= n[s]; ++i) {
 49         double tres = 0;
 50         for (int j = 1; j <= 36; ++j) {
 51             if (!s) {
 52                 tres += f[0][i].p[j] * (1.0 - solve(v1, v2 - j, 1));
 53             } else {
 54                 tres += f[1][i].p[j] * (1.0 - solve(v1 - j, v2, 0));
 55             }
 56         }
 57         res = max(res, tres);
 58     }
 59     return mmp[v1][v2][s] = res;
 60 }
 61 int main() {
 62     scanf("%d", &t);
 63     while (t--) {
 64         scanf("%d%d%d%d", &v[0], &v[1], &n[0], &n[1]);
 65         for (int i = 1; i <= n[0]; ++i) {
 66             scanf("%d", &f[0][i].d);
 67             for (int j = 1; j <= f[0][i].d; ++j) {
 68                 scanf("%d", &f[0][i].a[j]);
 69             }
 70             f[0][i].p[0] = 1;
 71             for (int j = 1; j <= 36; ++j) {
 72                 f[0][i].p[j] = 0;
 73             }
 74             double tp[37];
 75             for (int j = 1; j <= f[0][i].d; ++j) {
 76                 int l = f[0][i].a[j];
 77                 memcpy(tp, f[0][i].p, sizeof(f[0][i].p));
 78                 for (int j = 0; j <= 36; ++j) {
 79                     f[0][i].p[j] = 0;
 80                 }
 81                 for (int k = 24; ~k; --k) {
 82                     for (int o = 1; o <= l; ++o) {
 83                         f[0][i].p[k + o] += 1.0 / l * tp[k];
 84                     }
 85                 }
 86             }
 87         }
 88         for (int i = 1; i <= n[1]; ++i) {
 89             scanf("%d", &f[1][i].d);
 90             for (int j = 1; j <= f[1][i].d; ++j) {
 91                 scanf("%d", &f[1][i].a[j]);
 92             }
 93             f[1][i].p[0] = 1;
 94             for (int j = 1; j <= 36; ++j) {
 95                 f[1][i].p[j] = 0;
 96             }
 97             double tp[37];
 98             for (int j = 1; j <= f[1][i].d; ++j) {
 99                 int l = f[1][i].a[j];
100                 memcpy(tp, f[1][i].p, sizeof(f[1][i].p));
101                 for (int j = 0; j <= 36; ++j) {
102                     f[1][i].p[j] = 0;
103                 }
104                 for (int k = 24; ~k; --k) {
105                     for (int o = 1; o <= l; ++o) {
106                         f[1][i].p[k + o] += 1.0 / l * tp[k];
107                     }
108                 }
109             }
110         }
111         for (int i = 0; i <= 300; ++i) {
112             for (int j = 0; j <= 300; ++j) {
113                 for (int s = 0; s < 2; ++s) {
114                     mmp[i][j][s] = -1;
115                 }
116             }
117         }
118         printf("%.9f\n", solve(v[0], v[1], 0));
119     }
120     return 0;
121 }
View Code

 

posted @ 2018-04-28 19:33  hit_yjl  阅读(331)  评论(0编辑  收藏  举报