思路参考y总视频啦
想说的就是 f数组是不必要的
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1005;
double a[N][N];
int n, m, x, y;
void guass()
{
double r;
for (int i = 1; i <= m; i++)
{
r = a[i][i];
a[i][i] /= r, a[i][i + 1] /= r;
if (i == m) continue;//1.i = m时,a[i][m + 1]会被除两次 2.第m + 1行不用动
a[i][m + 1] /= r;
r = a[i + 1][i];
a[i + 1][i] -= a[i][i] * r, a[i + 1][i + 1] -= a[i][i + 1] * r;
a[i + 1][m + 1] -= a[i][m + 1] * r;
}
for (int i = m; i > 1; i--)
{
r = a[i - 1][i];
a[i - 1][i] -= a[i][i] * r;
a[i - 1][m + 1] -= a[i][m + 1] * r;
}
}
int main()
{
scanf("%d%d%d%d", &n, &m, &x, &y);
if (m == 1)//只有一列,在每一行只有两种选择,过一行答案+2
{
printf("%.4lf", (double)2 * (n - x));
return 0;
}
for (int i = n - 1; i >= x; i--)
{
a[1][1] = 2.0 / 3, a[1][2] = -1.0 / 3, a[1][m + 1] = a[1][m + 1] / 3 + 1;
a[m][m - 1] = -1.0 / 3, a[m][m] = 2.0 / 3, a[m][m + 1] = a[m][m + 1] / 3 + 1;
for (int j = 2; j < m; j++)
{
a[j][j - 1] = a[j][j + 1] = -1.0 / 4;
a[j][j] = 3.0 / 4;
a[j][m + 1] = a[j][m + 1] / 4 + 1;
}
guass();
}
printf("%.4lf", a[y][m + 1]);
return 0;
}