#include #include #include #include #include #include #include #include #include #include #include #include #include //#define DEBUG 1 #define for0(i,n) for (int i=0; i=0; i--) #define fori(i,a,b) for (int i=a; i<=b; i++) #define iter(c) for(auto it=c.begin(); it!=c.end(); it++) #define vec(x) vector< x > #define pb push_back #define ms(a,z) memset(a,z,sizeof(a)); #define mp make_pair #define X first #define Y second #define sqr(x) 1LL*(x)*(x) #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() #define read(x) int x; scanf("%d",&x); #ifdef DEBUG #define nl cout<<"\n"; #define pr(x) cout<<(x)<<" "; #define prl(x) cout<<#x " = "< tunnel[25][25]; int cnt_free(int x, int y) { int cnt = 0; for0(i,4) { int xx = x+dx[i]; int yy = y+dy[i]; if (grid[yy][xx] != '#') cnt++; } return cnt; } int main() { #ifdef DEBUG freopen("D.txt","r",stdin); //freopen("","w",stdout); #endif read(h); read(w); read(num_tunnels); for (int y=0; y<=h+1; y++) grid[y][0] = grid[y][w+1] = '#'; for (int x=0; x<=w+1; x++) grid[0][x] = grid[h+1][x] = '#'; for0(y,h) { for0(x,w) { cin >> grid[y+1][x+1]; } } int startx,starty; for (int y=1; y<=h; y++) { for (int x=1; x<=w; x++) { if (grid[y][x] == 'A') { startx = x; starty = y; break; } } } for (int y=1; y<=h; y++) { for (int x=1; x<=w; x++) { cnt_adj[y][x] = cnt_free(x,y); } } for (int y=0; y<=h+1; y++) { for (int x=0; x<=w+1; x++) { tunnel[y][x] = mp(-1,-1); } } for0(i,num_tunnels) { int y1,x1,y2,x2; cin >> y1 >> x1 >> y2 >> x2; if (cnt_adj[y1][x1] == 0) { grid[y2][x2] = '*'; } if (cnt_adj[y2][x2] == 0) { grid[y1][x1] = '*'; } else if (cnt_adj[y1][x1] != 0 && cnt_adj[y2][x2] != 0) { tunnel[y1][x1] = mp(x2,y2); tunnel[y2][x2] = mp(x1,y1); } } print2D(grid,h+2,w+2); print2D(cnt_adj,h+2,w+2); ms(prob_exit,0); int n=0; for (; n<100000; n++) { for (int y=1; y<=h; y++) { for (int x=1; x<=w; x++) { if (grid[y][x] == '*') prob_exit[n&1][y][x] = 0; else if (grid[y][x] == '%') prob_exit[n&1][y][x] = 1.0; else if (grid[y][x] != '#'){ double total = 0; for0(i,4) { int xx = x+dx[i]; int yy = y+dy[i]; if (tunnel[yy][xx] != mp(-1,-1)) { int xxx = tunnel[yy][xx].X; int yyy = tunnel[yy][xx].Y; xx = xxx; yy = yyy; } total += prob_exit[(n+1)&1][yy][xx]; } if (cnt_adj[y][x] == 0) prob_exit[n&1][y][x] = 0; else prob_exit[n&1][y][x] = total/cnt_adj[y][x]; } } } /*if (n%10 == 0) { print2D(prob_exit[n&1],h+2,w+2); }*/ } printf("%.9f\n",prob_exit[(n+1)&1][starty][startx]); return 0; }