#include using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); i++) #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--) #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++) #define FILL(a,value) memset(a, value, sizeof(a)) #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define PB push_back #define MP make_pair typedef long long LL; typedef vector VI; typedef pair PII; const double PI = acos(-1.0); const int INF = 1000 * 1000 * 1000 + 7; const LL LINF = INF * (LL) INF; const int MAX = 500; string A[MAX]; int I[MAX][MAX]; int T[MAX][MAX]; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int n, m; double a[MAX][MAX];//n*m double b[MAX];//n double x[MAX];//m int ok; // ok = 0 -> 0 solutions; // ok = 1 -> 1 solution; // ok = 2 -> inf solutions; const double eps = 1e-8; const double EPS = 1e-5; int rpos[MAX];//n; void gauss() { int col = 0; int rng = 0; FOR (row,0,n) rpos[row] = -1; FOR (row,0,n) { if (col >= m) break; int pos = row; FOR (j,row+1,n) if (abs(a[pos][col]) < abs(a[j][col])) pos = j; if (abs(a[pos][col]) < eps) { row--; col++; continue; } FOR (c,col,m) swap(a[pos][c],a[row][c]); swap(b[pos],b[row]); FOR (j,row+1,n) { double koef = a[j][col]/a[row][col]; FOR (k,col,m) a[j][k] -= koef*a[row][k]; b[j] -= koef*b[row]; } rpos[row] = col; col++; rng++; } ok = 2; if (rng == m) ok = 1; RFOR (i,n,0) if (rpos[i] != -1) { int z = rpos[i]; x[z] = b[i]; FOR (ncol,z+1,m) x[z] -= x[ncol]*a[i][ncol]; x[z] /= a[i][z]; } FOR (i,0,n) { double delta = b[i]; FOR (j,0,m) delta -= a[i][j]*x[j]; if (abs(delta) > EPS) ok = 0; } } int main() { //freopen("in.txt", "r", stdin); //ios::sync_with_stdio(false); cin.tie(0); int n, m, k; cin>>n>>m>>k; FOR (i, 0, n) { cin>>A[i]; } int cnt = 0; FOR (i, 0, n) { FOR (j, 0, m) { if (A[i][j] == '#') continue; T[i][j] = I[i][j] = cnt++; } } FOR (i, 0, k) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; x1--; y1--; x2--; y2--; swap(T[x1][y1], T[x2][y2]); } ::n = ::m = cnt; FOR (i, 0, n) { FOR (j, 0, m) { if (A[i][j] == '#') continue; int r = I[i][j]; if (A[i][j] == '%') { a[r][r] = 1; b[r] = 1; continue; } int tot = 0; FOR (it, 0, 4) { int x = i + dx[it]; int y = j + dy[it]; if (x < 0 || y < 0 || x >= n || y >= m) continue; if (A[x][y] == '#') continue; tot++; } if (tot == 0 || A[i][j] == '*') { a[r][r] = 1; b[r] = 0; continue; } // cout<= n || y >= m) continue; if (A[x][y] == '#') continue; a[r][T[x][y]] = -1. / tot; } a[r][r] = 1; b[r] = 0; } } FOR (i, 0, ::n) { FOR (j, 0, ::n) { // printf("%.2f\t", a[i][j]); } // cout<<" "<