#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n,m; cin >> n >> m; vector<string> a(n), b(n); int cnt = 0; for(int i = 0; i < n; i++) { cin >> a[i]; for(int j = 0; j < m; j++) { if(a[i][j] == 'O') cnt += (i & 1) == (j & 1); } } int count = 0; for(int i = 0; i < n; i++) { cin >> b[i]; for(int j = 0; j < m; j++) { if(b[i][j] == 'O') { cnt -= (i & 1) == (j & 1); count++; } } } if(abs(cnt) & 1) { cout << 0 << endl; return 0; } const vector<int> dx = {0, 0, -1, 1}; const vector<int> dy = {-1, 1, 0, 0}; int deg = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(b[i][j] == '.') continue; for(int k = 0; k < 4; k++) { if(i + dx[k] < 0 || i + dx[k] >= n || j + dy[k] < 0 || j + dy[k] >= m || b[i + dx[k]][j + dy[k]] == 'O') continue; deg++; } } } long long bin = 1; for(int i = 1; i <= count - 1; i++) { bin = bin * (n * m - 1 - i) / i; } cout << fixed << setprecision(18) << 1.0 * deg / (bin * (2 * n * m - n - m)) << endl; 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 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n,m; cin >> n >> m; vector<string> a(n), b(n); int cnt = 0; for(int i = 0; i < n; i++) { cin >> a[i]; for(int j = 0; j < m; j++) { if(a[i][j] == 'O') cnt += (i & 1) == (j & 1); } } int count = 0; for(int i = 0; i < n; i++) { cin >> b[i]; for(int j = 0; j < m; j++) { if(b[i][j] == 'O') { cnt -= (i & 1) == (j & 1); count++; } } } if(abs(cnt) & 1) { cout << 0 << endl; return 0; } const vector<int> dx = {0, 0, -1, 1}; const vector<int> dy = {-1, 1, 0, 0}; int deg = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(b[i][j] == '.') continue; for(int k = 0; k < 4; k++) { if(i + dx[k] < 0 || i + dx[k] >= n || j + dy[k] < 0 || j + dy[k] >= m || b[i + dx[k]][j + dy[k]] == 'O') continue; deg++; } } } long long bin = 1; for(int i = 1; i <= count - 1; i++) { bin = bin * (n * m - 1 - i) / i; } cout << fixed << setprecision(18) << 1.0 * deg / (bin * (2 * n * m - n - m)) << endl; return 0; } |