# 高斯消元解概率动态规划

@(E ACMer)

- 你有一个六面的刻有数字[1,6]$[1, 6]$的等概率骰子，每次抛出骰子，上面显示的数字就是你走的步数。
- 但是有一些类似(x,y)$(x, y)$的规则表示：你一旦跳到了x$x$号格子，你就会瞬间走回y$y$号格子，且这个跳跃不算做步数。(注意这里的x可能小于y)。

jump(i)$jump(i)$表示：从i$i$会立即跳跃到jump(i)$jump(i)$，对于没有跳跃规则的格子jum(i)==i$jum(i) == i$，令f(i)$f(i)$：表示从i$i$走到100$100$的期望步数，我们的目的就是求f(1)$f(1)$

f(i)=6+j<=6 and i+j<=100j=1f(jump(i+j)+tf(i)6
if (t>94) t=(6100i)  else  t=0

6xixi+1xi+2....xi+6=6

code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
#define pr(x) cout << #x << ": " << x << "  "
#define pl(x) cout << #x << ": " << x << endl;
const int maxn = 103;
const double eps = 1e-9;

double a[maxn][maxn], ans[maxn];
int jump[maxn << 1];

void gauss(void) {
int i, j, t, k;
for (i = 1; i <= 100; i++) {
t = i;
for (j = i + 1; j <= 100; j++) if (a[t][i] < a[j][i]) t = j;
if (t != i) for (j = 1; j <= 101; j++) swap(a[i][j], a[t][j]);
if (fabs(a[i][i]) < eps) continue;
for (j = i + 1; j <= 100; j++) {
if (fabs(a[j][i]) > eps) {
double tt = a[j][i] / a[i][i];
for (k = i; k <= 101; k++) a[j][k] -= a[i][k] * tt;
}
}
}
for (i = 100; i >= 1; i--) {
for (j = 100; j > i; j--) a[i][101] -= a[i][j] * ans[j];
if (fabs(a[i][i] > eps)) ans[i] = a[i][101] / a[i][i];
if (fabs(a[i][i]) < eps) ans[i] = 0.0;
}
}

void build_a(void) {
memset(ans, 0.0, sizeof(ans));
memset(a, 0.0, sizeof(a));
for (int i = 1; i <= 100; i++) {
a[i][i] = a[i][101] = 6;
for (int j = 1; j <= 6; j++) {
if (i + j > 100) a[i][i] -= 1;
else a[i][jump[i + j]] += -1;
}
}
}

int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int T, N;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++) {
scanf("%d", &N);
for (int i = 1; i <= 100; i++) jump[i] = i;
for (int i = 0; i < N; i++) {
int x, y;
scanf("%d%d", &x, &y);
jump[x] = y;
}
build_a();
gauss();
printf("Case %d: %.15f\n", cas, ans[1]);
}
return 0;
}


08-14 370

06-12 52

10-06 782

02-05 841

04-23 11

09-11 631

05-16 704

04-27 516

12-13 873