这是一道已经在任务列表里吃了一年灰的题了
之前看的时候还挺懵逼的
现在来看难度还行……
考虑设(f[i][j])为从((i,j))走到最后一行的期望值
考虑逆推得
[f[i][j] = frac{1}{4} imes(f[i][j]+f[i][j+1]+f[i][j-1]+f[i+1][j]) +1
]
移项得
[-f[i][j-1]+3f[i][j]-f[i][j+1]=4+f[i+1][j]
]
直接裸上高斯消元即可(O(n^5))
考虑这个矩阵是个结构奇特的矩阵,高斯消元的复杂度可以降到(O(n))
总复杂度为(O(n^3))
注意:特判 (m=1) 的情况
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1ll<<30
#define db double
template<typename _T>
inline void read(_T &x)
{
x=0;char s=getchar();int f=1;
while(s<'0'||s>'9') {f=1;if(s=='-')f=-1;s=getchar();}
while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+s-'0';s=getchar();}
x*=f;
}
const int np = 1e3 + 5;
db G[np][np];
db la[np];
int m;
inline void Guass()
{
for(int i=1;i<m;i++)
{
double gcd = G[i + 1][i] / G[i][i];
G[i + 1][i] -= G[i][i] * gcd;
G[i + 1][i + 1] -= G[i][i + 1] * gcd;
G[i + 1][m + 1] -= G[i][m + 1] * gcd;
}
for(int i=m;i>1;i--)
{
la[i] = G[i][m + 1]/G[i][i];
G[i - 1][m + 1] = G[i - 1][m + 1] - la[i] * G[i-1][i];
}
la[1] = G[1][m + 1] / G[1][1];
}
signed main()
{
int n,x_,y_;
read(n);
read(m);
read(x_);
read(y_);
db d(0);
if(m == 1)
{
d = n - x_;
db xxx_ = d * 2;
printf("%.4lf",xxx_);
return 0;
}
db lp =0 ;
if(x_ == n)
{
printf("%.4lf",lp);
return 0;
}
for(int i=n - 1;i>=x_;i--)
{
G[1][1] = 2;G[1][2] = -1;
for(int j=2;j<m;j++)
G[j][j] = 3 , G[j][j-1] = -1 , G[j][j + 1] = -1;
G[m][m] = 2;G[m][m - 1] = -1;
G[1][m + 1] = la[1] + 3;
for(int j=2;j<m;j++) G[j][m + 1] = la[j] + 4;
G[m][m + 1] = la[m] + 3;
Guass();
}
printf("%.4lf",la[y_]);
}