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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <cstdio> #include <cmath> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const double eps = 1e-10; const int N = 1e4 + 10, M = 30, dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; bool q[N][M]; int n, m, tot, mv[N * M], id[N][M]; double tA[N * M][M << 1], B[N * M]; double &A (int x, int y) { return tA[x][y - mv[x]]; } bool inside (int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; } void Gauss () { for (int k = 0; k < tot; k++) { if (fabs(A(k, k)) < eps) continue; int t = min(tot - 1, k + m); B[k] /= A(k, k); for (int i = t; i >= k; i--) A(k, i) /= A(k, k); for (int i = k + 1; i <= t; i++) { B[i] -= B[k] * A(i, k); for (int j = t; j >= k; j--) A(i, j) -= A(k, j) * A(i, k); } } for (int i = tot - 1; ~i; i--) for (int j = i + 1; j <= min(tot - 1, i + m); j++) B[i] -= A(i, j) * B[j]; } int main () { read(m), read(n); int p[4]; for (int i = 0; i < 4; i++) read(p[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c = getchar(); while (c != 'X' && c != 'T' && c != '.') c = getchar(); id[i][j] = c != 'X' ? tot++ : -1; c == 'T' && (q[i][j] = true); } for (int i = 0; i < tot; i++) mv[i] = max(0, i - m); double st = 0; for (int i = 0; i < m; i++) st += id[0][i] != -1; st = 1 / st; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (id[i][j] == -1) continue; int &t = id[i][j]; !i && (B[t] = st); A(t, t) = 1; if (q[i][j]) continue; int ps = 100; for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; if (!inside(nx, ny) || id[nx][ny] == -1) ps -= p[k]; } for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; if (inside(nx, ny) && ~id[nx][ny]) A(id[nx][ny], t) = -(double)p[k] / ps; } } Gauss(); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) q[i][j] && printf("%.9lf\n", B[id[i][j]]); return 0; }
|