一般情况下,动态规划转移方程是单向的。期望DP中,若状态转移不是单向的,即状态之间相互影响。则使用解方程的思想,将状态设为变量,将状态转移方程转化为变量方程,构造矩阵。通过高斯消元求解。
题意
人在0到n-1的数轴上来回移动,给定起始点x,终止点y,初始方向d,每步移动k个单位的概率pk。求到达终止点的期望路程。
分析
- 首先解决来回移动的问题,将数轴扩展到0到2n-2。大于n-1的节点i表示在原位置2n-2-i反向移动。
- 然后,用bfs选择从起始点可达的有效节点。
- 再然后,将从有效节点到达终止点的期望设为变量,并根据状态转移方程为每个节点构建一个方程,构成一个方阵。
- 最后,高斯消元求解变量值。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX_N = 100;
const double eps = 1e-10;
double p[MAX_N+10];
int tot; // 有意义的位置 | 0 ~ tot | tot代表常数项
int h[2*MAX_N+10]; // 映射到的位置
double res[2*MAX_N+10]; // 存放结果
double mat[2*MAX_N+10][2*MAX_N+10]; // 矩阵
int n, m, y, x, d;
// 可以抵达的位置
bool bfs()
{
queue<int> que;
que.push(x);
memset(h, -1, sizeof(h));
tot = 0;
h[x] = tot++;
bool flag = false;
while (!que.empty())
{
int pos = que.front(); que.pop();
for (int i = 1; i <= m; i++)
{
if (p[i] > eps)
{
int next_pos = (pos+i) % n;
if (next_pos == y || next_pos == n-y) flag = true;
else if (h[next_pos] < 0)
{
h[next_pos] = tot++;
que.push(next_pos);
}
}
}
}
return flag;
}
// 构建矩阵
void build()
{
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++)
{
if (h[i] >= 0)
{
mat[h[i]][h[i]] = 1;
for (int j = 1; j <= m; j++)
{
if (p[j] > eps)
{
if (h[(i+j)%n] >= 0) mat[h[i]][h[(i+j)%n]] -= p[j];
mat[h[i]][tot] += 1.0*j*p[j];
}
}
}
}
}
// 矩阵求解
void solve()
{
for (int i = 0; i < tot; i++)
{
for (int j = 0; j < tot; j++)
{
if (i == j) continue;
double mul = mat[j][i]/mat[i][i];
for (int k = 0; k <= tot; k++)
mat[j][k] -= mul * mat[i][k];
}
}
for (int i = 0; i < tot; i++)
res[i] = mat[i][tot] / mat[i][i];
}
int main()
{
int t;
scanf("%d", &t);
while (t-->0)
{
scanf("%d%d%d%d%d", &n, &m, &y, &x, &d);
// 处理成线性
n = 2*n - 2;
for (int i = 1; i <= m; i++)
{
scanf("%lf", &p[i]);
p[i] /= 100;
}
if (x == y)
{
printf("0.00\n");
continue;
}
else
{
if (d == 1) x = (n - x) % n;
if (!bfs()) printf("Impossible !\n");
else
{
build();
solve();
printf("%.2lf\n", res[h[x]]);
}
}
}
}