#include #include #include #include #include #include #include using namespace std; int N, M; int coord(int i, int j) { return i*M + j; } pair decom(int id) { return {id/M, (id%M)}; } int dr[] = {0, 0, -1, 1}; int dc[] = {-1, 1, 0, 0}; double ep = 1e-7; vector > matmul(vector > &A, vector > &B, int sz) { vector > C(1, vector(sz, 0)); for (int i = 0; i < 1; ++i) { for (int j = 0; j < sz; ++j) { for (int k = 0; k < sz; ++k) C[i][j] += A[i][k] * B[k][j]; } } return C; } int main() { int T; cin>>N>>M>>T; vector grid(N); for (int i = 0; i < N; ++i) cin>>grid[i]; vector tunnel(N*M); for (int i = 0; i < N*M; ++i) tunnel[i] = i; for (int i = 0; i < T; ++i) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; tunnel[coord(x1-1, y1-1)] = coord(x2-1, y2-1); tunnel[coord(x2-1, y2-1)] = coord(x1-1, y1-1); } vector > mat(N*M, vector(N*M, 0)); set exits; int alex; for (int i = 0; i < N*M; ++i) { pair temp = decom(i); int x = temp.first, y = temp.second; if (grid[x][y] == '#' || grid[x][y] == '*') continue; if (grid[x][y] == 'A') alex = i; else if (grid[x][y] == '%') exits.insert(i); int cnt = 0; for (int k = 0; k < 4; ++k) { int nx = x + dr[k]; int ny = y + dc[k]; if (nx < 0 || ny < 0 || nx == N || ny == M) continue; if (grid[nx][ny] != '#') cnt++; } for (int k = 0; k < 4; ++k) { int nx = x + dr[k]; int ny = y + dc[k]; if (nx < 0 || ny < 0 || nx == N || ny == M) continue; //cout< > res(1, vector(N*M, 0)); res[0][alex] = 1; res = matmul(res, mat, N*M); while (true) { bool cont = true; for (int exit : exits) { //cout< ep) { printf("%.8lf ", res[0][exit]); cont = false; break; } } if (!cont) break; //cout<<"\n"; res = matmul(res, mat, N*M); } return 0; }