#include #include #include #include #include #include #include #include #include #include #include typedef long long ll; typedef unsigned int uint; using namespace std; template struct Matrix { vector > val; Matrix(int n = 1, int m = 1) { val.clear(); val.resize(n, vector(m)); } Matrix(int n, int m, T x) { val.clear(); val.resize(n, vector(m, x)); } void init(int n, int m, T x = 0) { val.clear(); val.resize(n, vector(m, x)); } void resize(int n, int m, T x = 0) { val.resize(n); for (int i = 0; i < n; ++i) val[i].resize(m, x); } int size() { return val.size(); } inline vector &operator[](int i) { return val[i]; } friend ostream &operator<<(ostream &s, Matrix M) { s << endl; for (int i = 0; i < M.val.size(); ++i) s << M[i] << endl; return s; } }; template Matrix operator * (Matrix A, Matrix B) { Matrix R(A.size(), B[0].size()); for (int i = 0; i < A.size(); ++i) for (int j = 0; j < B[0].size(); ++j) for (int k = 0; k < B.size(); ++k) R[i][j] += A[i][k] * B[k][j]; return R; } template vector operator * (Matrix A, vector B) { vector v(A.size()); for (int i = 0; i < A.size(); ++i) for (int k = 0; k < B.size(); ++k) v[i] += A[i][k] * B[k]; return v; } template Matrix pow(Matrix A, int n) { Matrix R(A.size(), A.size()); for (int i = 0; i < A.size(); ++i) R[i][i] = 1; while (n > 0) { if (n & 1) R = R * A; A = A * A; n >>= 1; } return R; } int main() { int r, c, t, sy, sx, gy, gx; cin >> r >> c >> t >> sy >> sx >> gy >> gx; string b[r]; for (int i = 0; i < r; i++) { cin >> b[i]; } int n = r * c; Matrix M(n, n, 0.0); for (int i = 0; i < n; i++) { int x = i / c, y = i % c; if (b[x][y] == '#') continue; int count = 0; if (b[x-1][y] == '.') count++; if (b[x][y-1] == '.') count++; if (b[x+1][y] == '.') count++; if (b[x][y+1] == '.') count++; if (count == 0) { M[i][i] = 1.0; } else { double prob = 1.0 / (double)count; if (b[x-1][y] == '.') M[i][i-c] = prob; if (b[x][y-1] == '.') M[i][i-1] = prob; if (b[x+1][y] == '.') M[i][i+c] = prob; if (b[x][y+1] == '.') M[i][i+1] = prob; } } vector vec(n, 0.0); vec[sy*c+sx] = 1.0; Matrix P = pow(M, t); vector V = P * vec; cout << fixed << setprecision(15) << V[gy*c+gx] << "\n"; return 0; }