#include <iostream> #include <iomanip> #include <cmath> #define PAWN 'O' using namespace std; unsigned long long get_sum_of_all_vertices(int n, int m, int k) { unsigned long long sum = 2 * n * m - n - m; for (int p = 2; p <= k; ++p) { sum = sum * (n * m - p) / (p - 1); } return sum; } unsigned long long get_degree(bool **tab, int n, int m) { unsigned long long d = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (tab[i][j]) { if (i > 0 && !tab[i - 1][j]) // move up available { ++d; } if (i < n - 1 && !tab[i + 1][j]) // move down available { ++d; } if (j > 0 && !tab[i][j - 1]) // move left available { ++d; } if (j < m - 1 && !tab[i][j + 1]) // move right available { ++d; } } } } return d; } int main() { int n, m; cin >> n >> m; char c; int initialCoordsSum = 0; int pawns = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> c; if (c == PAWN) { initialCoordsSum += i + j; ++pawns; } } } bool **tab = new bool *[n]; int endCoordsSum = 0; for (int i = 0; i < n; ++i) { tab[i] = new bool[m]; for (int j = 0; j < m; ++j) { cin >> c; tab[i][j] = c == PAWN; if (c == PAWN) { endCoordsSum += i + j; } } } if ((initialCoordsSum % 2) != (endCoordsSum % 2)) { cout << 0 << endl; for (int i = 0; i < n; ++i) { delete[] tab[i]; } delete[] tab; return 0; } unsigned long long dsum = get_degree(tab, n, m); unsigned long long sum_deg = get_sum_of_all_vertices(n, m, pawns); double prob = static_cast<double>(dsum) / static_cast<double>(sum_deg); cout << fixed << setprecision(16) << prob << endl; for (int i = 0; i < n; ++i) { delete[] tab[i]; } delete[] tab; 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include <iostream> #include <iomanip> #include <cmath> #define PAWN 'O' using namespace std; unsigned long long get_sum_of_all_vertices(int n, int m, int k) { unsigned long long sum = 2 * n * m - n - m; for (int p = 2; p <= k; ++p) { sum = sum * (n * m - p) / (p - 1); } return sum; } unsigned long long get_degree(bool **tab, int n, int m) { unsigned long long d = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (tab[i][j]) { if (i > 0 && !tab[i - 1][j]) // move up available { ++d; } if (i < n - 1 && !tab[i + 1][j]) // move down available { ++d; } if (j > 0 && !tab[i][j - 1]) // move left available { ++d; } if (j < m - 1 && !tab[i][j + 1]) // move right available { ++d; } } } } return d; } int main() { int n, m; cin >> n >> m; char c; int initialCoordsSum = 0; int pawns = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> c; if (c == PAWN) { initialCoordsSum += i + j; ++pawns; } } } bool **tab = new bool *[n]; int endCoordsSum = 0; for (int i = 0; i < n; ++i) { tab[i] = new bool[m]; for (int j = 0; j < m; ++j) { cin >> c; tab[i][j] = c == PAWN; if (c == PAWN) { endCoordsSum += i + j; } } } if ((initialCoordsSum % 2) != (endCoordsSum % 2)) { cout << 0 << endl; for (int i = 0; i < n; ++i) { delete[] tab[i]; } delete[] tab; return 0; } unsigned long long dsum = get_degree(tab, n, m); unsigned long long sum_deg = get_sum_of_all_vertices(n, m, pawns); double prob = static_cast<double>(dsum) / static_cast<double>(sum_deg); cout << fixed << setprecision(16) << prob << endl; for (int i = 0; i < n; ++i) { delete[] tab[i]; } delete[] tab; return 0; } |