#include <bits/stdc++.h>
using namespace std;
int n, m;
char board[10][10];
char res_board[10][10];
int strengths[10][10];
int parity[10][10];
int np_pawns; // nieparzyste piony
int p_pawns; // parzyste piony
int p_strength;
int np_strength;
int res_np_pawns;
int res_p_pawns;
int p;
void preprocess() {
cin >> n >> m;
for (int y = 0; y < 10; ++y) {
for (int x = 0; x < 10; ++x) {
board[y][x] = 'n';
res_board[y][x] = 'n';
}
}
for (int y = 1; y <= n; ++y)
for (int x = 1; x <= m; ++x)
cin >> board[y][x];
for (int y = 1; y <= n; ++y)
for (int x = 1; x <= m; ++x)
cin >> res_board[y][x];
p = 1;
for (int y = 1; y <= n; ++y) {
p = 1 - p;
for (int x = 1; x <= m; ++x) {
parity[y][x] = p;
if (board[y][x] == 'O') {
if (p == 1)
p_pawns++;
else
np_pawns++;
}
p = 1 - p;
}
}
p = 1;
for (int y = 1; y <= n; ++y) {
p = 1 - p;
for (int x = 1; x <= m; ++x) {
if (res_board[y][x] == 'O') {
if (p == 1)
res_p_pawns++;
else
res_np_pawns++;
}
p = 1 - p;
}
}
for (int y = 1; y <= n; y++) {
for (int x = 1; x <= m; x++) {
int c = 0;
if (board[y - 1][x] == 'O' || board[y - 1][x] == '.') c++;
if (board[y + 1][x] == 'O' || board[y + 1][x] == '.') c++;
if (board[y][x - 1] == 'O' || board[y][x - 1] == '.') c++;
if (board[y][x + 1] == 'O' || board[y][x + 1] == '.') c++;
strengths[y][x] = c;
}
}
for (int y = 1; y <= n; y++) {
for (int x = 1; x <= m; x++) {
if (parity[y][x] == 1 && p_pawns == 0) strengths[y][x] = 0;
if (parity[y][x] == 0 && np_pawns == 0) strengths[y][x] = 0;
if (parity[y][x] == 1) p_strength += strengths[y][x];
if (parity[y][x] == 0) np_strength += strengths[y][x];
}
}
}
void solve() {
if (p_pawns != res_p_pawns || np_pawns != res_np_pawns) {
cout << '0';
return;
}
long double result = 1;
for (int y = 1; y <= n; y++) {
for (int x = 1; x <= m; x++) {
if (res_board[y][x] == '.') continue;
if (parity[y][x] == 1) {
// parzyste
long double w = ((long double)(strengths[y][x] * p_pawns)) / (long double)(p_strength);
p_pawns--;
result *= w;
} else {
// nieparzyste
long double w = ((long double)(strengths[y][x] * np_pawns)) / (long double)(np_strength);
np_pawns--;
result *= w;
}
}
}
cout << fixed << setprecision(18) << result;
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
preprocess();
solve();
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <bits/stdc++.h> using namespace std; int n, m; char board[10][10]; char res_board[10][10]; int strengths[10][10]; int parity[10][10]; int np_pawns; // nieparzyste piony int p_pawns; // parzyste piony int p_strength; int np_strength; int res_np_pawns; int res_p_pawns; int p; void preprocess() { cin >> n >> m; for (int y = 0; y < 10; ++y) { for (int x = 0; x < 10; ++x) { board[y][x] = 'n'; res_board[y][x] = 'n'; } } for (int y = 1; y <= n; ++y) for (int x = 1; x <= m; ++x) cin >> board[y][x]; for (int y = 1; y <= n; ++y) for (int x = 1; x <= m; ++x) cin >> res_board[y][x]; p = 1; for (int y = 1; y <= n; ++y) { p = 1 - p; for (int x = 1; x <= m; ++x) { parity[y][x] = p; if (board[y][x] == 'O') { if (p == 1) p_pawns++; else np_pawns++; } p = 1 - p; } } p = 1; for (int y = 1; y <= n; ++y) { p = 1 - p; for (int x = 1; x <= m; ++x) { if (res_board[y][x] == 'O') { if (p == 1) res_p_pawns++; else res_np_pawns++; } p = 1 - p; } } for (int y = 1; y <= n; y++) { for (int x = 1; x <= m; x++) { int c = 0; if (board[y - 1][x] == 'O' || board[y - 1][x] == '.') c++; if (board[y + 1][x] == 'O' || board[y + 1][x] == '.') c++; if (board[y][x - 1] == 'O' || board[y][x - 1] == '.') c++; if (board[y][x + 1] == 'O' || board[y][x + 1] == '.') c++; strengths[y][x] = c; } } for (int y = 1; y <= n; y++) { for (int x = 1; x <= m; x++) { if (parity[y][x] == 1 && p_pawns == 0) strengths[y][x] = 0; if (parity[y][x] == 0 && np_pawns == 0) strengths[y][x] = 0; if (parity[y][x] == 1) p_strength += strengths[y][x]; if (parity[y][x] == 0) np_strength += strengths[y][x]; } } } void solve() { if (p_pawns != res_p_pawns || np_pawns != res_np_pawns) { cout << '0'; return; } long double result = 1; for (int y = 1; y <= n; y++) { for (int x = 1; x <= m; x++) { if (res_board[y][x] == '.') continue; if (parity[y][x] == 1) { // parzyste long double w = ((long double)(strengths[y][x] * p_pawns)) / (long double)(p_strength); p_pawns--; result *= w; } else { // nieparzyste long double w = ((long double)(strengths[y][x] * np_pawns)) / (long double)(np_strength); np_pawns--; result *= w; } } } cout << fixed << setprecision(18) << result; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); preprocess(); solve(); return 0; } |
English