#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() string A[8], B[8]; int ilea[2], ileb[2], dx[]={1, 0, -1, 0}, dy[]={0, 1, 0, -1}; ll C[100][100], n, m, kraw, lacznie; ll f(ll a, ll b, ll c, ll d) { if(c<0 || d<0 || a<c || b<d) return 0; return C[a][c]*C[b][d]; } ll licz(ll a, ll b, ll c, ll d) { ll ans=0; for(ll k=-10; k<=10; k+=2) ans+=f(a, b, c+k, d-k); return ans; } bool ok(int a, int b) { return 0<=a && a<n && 0<=b && b<m; } int main() { cin >> n >> m; rep(i, n) { cin >> A[i]; rep(j, m) if(A[i][j]=='O') ilea[(i+j)%2]^=1; } rep(i, n) { cin >> B[i]; rep(j, m) if(B[i][j]=='O') ileb[(i+j)%2]^=1; } rep(i, 2) if(ilea[i]!=ileb[i]) { cout << 0 << '\n'; return 0; } rep(i, 100) { C[i][0]=C[i][i]=1; for(int j=1; j<i; ++j) C[i][j]=C[i-1][j]+C[i-1][j-1]; } ll ileparz=0, ilenparz=0, specparz=0, specnparz=0; rep(i, n) rep(j, m) { if((i+j)%2==0) { ++ileparz; if(B[i][j]=='O') ++specparz; } else { ++ilenparz; if(B[i][j]=='O') ++specnparz; } } kraw=4*(specparz+specnparz)*licz(ileparz, ilenparz, specparz, specnparz); rep(i, n) rep(j, m) { ll zle=0; rep(l, 4) if(!ok(i+dx[l], j+dy[l])) ++zle; if((i+j)%2==0) kraw-=zle*licz(ileparz-1, ilenparz, specparz-1, specnparz); else kraw-=zle*licz(ileparz, ilenparz-1, specparz, specnparz-1); } if(specparz && specnparz) kraw-=2*((n-1)*m+n*(m-1))*licz(ileparz-1, ilenparz-1, specparz-1, specnparz-1); rep(i, n) rep(j, m) if(B[i][j]=='O') { rep(l, 4) if(ok(i+dx[l], j+dy[l]) && B[i+dx[l]][j+dy[l]]=='.') ++lacznie; } ld p=(ld)lacznie/(ld)kraw; cout << fixed << setprecision(15) << p << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() string A[8], B[8]; int ilea[2], ileb[2], dx[]={1, 0, -1, 0}, dy[]={0, 1, 0, -1}; ll C[100][100], n, m, kraw, lacznie; ll f(ll a, ll b, ll c, ll d) { if(c<0 || d<0 || a<c || b<d) return 0; return C[a][c]*C[b][d]; } ll licz(ll a, ll b, ll c, ll d) { ll ans=0; for(ll k=-10; k<=10; k+=2) ans+=f(a, b, c+k, d-k); return ans; } bool ok(int a, int b) { return 0<=a && a<n && 0<=b && b<m; } int main() { cin >> n >> m; rep(i, n) { cin >> A[i]; rep(j, m) if(A[i][j]=='O') ilea[(i+j)%2]^=1; } rep(i, n) { cin >> B[i]; rep(j, m) if(B[i][j]=='O') ileb[(i+j)%2]^=1; } rep(i, 2) if(ilea[i]!=ileb[i]) { cout << 0 << '\n'; return 0; } rep(i, 100) { C[i][0]=C[i][i]=1; for(int j=1; j<i; ++j) C[i][j]=C[i-1][j]+C[i-1][j-1]; } ll ileparz=0, ilenparz=0, specparz=0, specnparz=0; rep(i, n) rep(j, m) { if((i+j)%2==0) { ++ileparz; if(B[i][j]=='O') ++specparz; } else { ++ilenparz; if(B[i][j]=='O') ++specnparz; } } kraw=4*(specparz+specnparz)*licz(ileparz, ilenparz, specparz, specnparz); rep(i, n) rep(j, m) { ll zle=0; rep(l, 4) if(!ok(i+dx[l], j+dy[l])) ++zle; if((i+j)%2==0) kraw-=zle*licz(ileparz-1, ilenparz, specparz-1, specnparz); else kraw-=zle*licz(ileparz, ilenparz-1, specparz, specnparz-1); } if(specparz && specnparz) kraw-=2*((n-1)*m+n*(m-1))*licz(ileparz-1, ilenparz-1, specparz-1, specnparz-1); rep(i, n) rep(j, m) if(B[i][j]=='O') { rep(l, 4) if(ok(i+dx[l], j+dy[l]) && B[i+dx[l]][j+dy[l]]=='.') ++lacznie; } ld p=(ld)lacznie/(ld)kraw; cout << fixed << setprecision(15) << p << '\n'; } |