#include <bits/stdc++.h> using namespace std; void read_board(int n, int m, vector<string> &board) { for (int i = 0; i < n; i++) { string s; cin >> s; board.push_back(s); } } int npawns(vector<string> &board) { int r = 0; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (board[i][j] == 'O') { r++; } } } return r; } int parity(vector<string> &board) { int r = 0; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (board[i][j] == 'O') { r += i + j; } } } return r % 2; } int degree(vector<string> &board) { int r = 0; int n = board.size(); int m = board[0].size(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i < n - 1 && board[i][j] != board[i + 1][j]) { r++; } if (j < m - 1 && board[i][j] != board[i][j + 1]) { r++; } } } return r; } long long int binom(long long int n, long long int k) { long long int ret = 1; for (long long int m = n; m > n - k; m--) { ret *= m; } for (long long int l = 1; l <= k; l++) { ret /= l; } return ret; } int main() { int n, m; scanf("%d%d", &n, &m); vector<string> start; vector<string> end; read_board(n, m, start); read_board(n, m, end); if (parity(start) != parity(end)) { puts("0"); return 0; } int k = npawns(end); int deg = degree(end); long long int nedges = binom(n * m - 2, k - 1) * (2 * n * m - n - m); long double out = (long double)deg / (long double)nedges; printf("%.15lf\n", (double)out); }
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 85 86 87 | #include <bits/stdc++.h> using namespace std; void read_board(int n, int m, vector<string> &board) { for (int i = 0; i < n; i++) { string s; cin >> s; board.push_back(s); } } int npawns(vector<string> &board) { int r = 0; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (board[i][j] == 'O') { r++; } } } return r; } int parity(vector<string> &board) { int r = 0; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (board[i][j] == 'O') { r += i + j; } } } return r % 2; } int degree(vector<string> &board) { int r = 0; int n = board.size(); int m = board[0].size(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i < n - 1 && board[i][j] != board[i + 1][j]) { r++; } if (j < m - 1 && board[i][j] != board[i][j + 1]) { r++; } } } return r; } long long int binom(long long int n, long long int k) { long long int ret = 1; for (long long int m = n; m > n - k; m--) { ret *= m; } for (long long int l = 1; l <= k; l++) { ret /= l; } return ret; } int main() { int n, m; scanf("%d%d", &n, &m); vector<string> start; vector<string> end; read_board(n, m, start); read_board(n, m, end); if (parity(start) != parity(end)) { puts("0"); return 0; } int k = npawns(end); int deg = degree(end); long long int nedges = binom(n * m - 2, k - 1) * (2 * n * m - n - m); long double out = (long double)deg / (long double)nedges; printf("%.15lf\n", (double)out); } |