CF24D Broken robot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define re register
const int MAXn = 1e3;
const int MAXm = 1e3;

template <class T>
inline void read(T &a) {
register char c;while (c = getchar(), c < '0' || c > '9');register T x(c - '0');while (c = getchar(), c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);}a = x;
}

int n, m, x, y;
double mat[MAXm + 10][MAXm + 10], d[MAXn + 10][MAXm + 10];
signed main() {
read(n), read(m);
read(x), read(y);
if (m == 1) {
printf("%d\n", 2 * (n - x));
return 0;
}
for (re int i = n - 1; i >= x; --i) {
memset(mat, 0, sizeof(mat));
mat[1][1] = 2; mat[1][2] = -1;
mat[1][m + 1] = d[i + 1][1] + 3;
for (re int j = 2; j < m; ++j) {
mat[j][j - 1] = mat[j][j + 1] = -1; mat[j][j] = 3;
mat[j][m + 1] = d[i + 1][j] + 4;
}
mat[m][m] = 2; mat[m][m - 1] = -1;
mat[m][m + 1] = d[i + 1][m] + 3;
// ------- 高斯消元 begin -------
for (re int i = 1; i < m; ++i) {
double solve = mat[i + 1][i] / mat[i][i];
mat[i + 1][i] -= mat[i][i] * solve;
if (i != m) {
mat[i + 1][i + 1] -= mat[i][i + 1] * solve;
}
mat[i + 1][m + 1] -= mat[i][m + 1] * solve;
}
for (re int i = m; i > 1; --i) {
double solve = mat[i - 1][i] / mat[i][i];
mat[i - 1][i] -= mat[i][i] * solve;
mat[i - 1][m + 1] -= mat[i][m + 1] * solve;
}
// ------- 高斯消元 end -------
for (re int j = 1; j <= m; ++j) {
d[i][j] = mat[j][m + 1] / mat[j][j];
}
}
printf("%.4f\n", d[x][y]);
}