#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define vec vector #define pb push_back using namespace std; #define N 8 int n; int m; int k; bool board[N][N][2]; ll chose(ll a, ll b){ if(b<0) return 0; ll ans=1; fors(i, a+1, a-b+1) ans*=i; fors(i, b+1, 1) ans/=i; return ans; } int main() { cin >> n >> m; foru(i, n) foru(j, m){ char c; cin >> c; board[i][j][0]=(c=='O'); } foru(i, n) foru(j, m){ char c; cin >> c; board[i][j][1]=(c=='O'); } k=0; foru(i, n) foru(j, m) k+=board[i][j][0]; bool nzmnk[2]; nzmnk[0]=0; nzmnk[1]=0; foru(b, 2) foru(i, n) foru(j, m){ if(board[i][j][b]) nzmnk[b]=(nzmnk[b]!=((i & 1)!=(j & 1))); } if(nzmnk[0]!=nzmnk[1]){ cout << "0"; return 0; } int edges=0; foru(i, n) foru(j, m){ if(board[i][j][1]){ int i_=i+1; int j_=j; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } i_=i-1; j_=j; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } i_=i; j_=j-1; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } i_=i; j_=j+1; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } } } ll total_edges=chose(n*m, 1)*chose(n*m-1, k-1)*4; ///all moves in all positions total_edges-=(2*n+2*m)*chose(n*m-1, k-1); ///delete going of board total_edges-= (2*n*(m-1)+2*(n-1)*m)*chose(n*m-2, k-2); ///delete overlapping cout << fixed << setprecision(17) << edges*2.0d/total_edges; return 0; }
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define vec vector #define pb push_back using namespace std; #define N 8 int n; int m; int k; bool board[N][N][2]; ll chose(ll a, ll b){ if(b<0) return 0; ll ans=1; fors(i, a+1, a-b+1) ans*=i; fors(i, b+1, 1) ans/=i; return ans; } int main() { cin >> n >> m; foru(i, n) foru(j, m){ char c; cin >> c; board[i][j][0]=(c=='O'); } foru(i, n) foru(j, m){ char c; cin >> c; board[i][j][1]=(c=='O'); } k=0; foru(i, n) foru(j, m) k+=board[i][j][0]; bool nzmnk[2]; nzmnk[0]=0; nzmnk[1]=0; foru(b, 2) foru(i, n) foru(j, m){ if(board[i][j][b]) nzmnk[b]=(nzmnk[b]!=((i & 1)!=(j & 1))); } if(nzmnk[0]!=nzmnk[1]){ cout << "0"; return 0; } int edges=0; foru(i, n) foru(j, m){ if(board[i][j][1]){ int i_=i+1; int j_=j; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } i_=i-1; j_=j; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } i_=i; j_=j-1; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } i_=i; j_=j+1; if(ir(0, n-1, i_) && ir(0, m-1, j_)) { edges+=!board[i_][j_][1]; } } } ll total_edges=chose(n*m, 1)*chose(n*m-1, k-1)*4; ///all moves in all positions total_edges-=(2*n+2*m)*chose(n*m-1, k-1); ///delete going of board total_edges-= (2*n*(m-1)+2*(n-1)*m)*chose(n*m-2, k-2); ///delete overlapping cout << fixed << setprecision(17) << edges*2.0d/total_edges; return 0; } |