#include <bits/stdc++.h> #define dbg(x) #x << " = " << x << " " using ld = long double; using ll = long long; using namespace std; using State = vector<vector<int>>; int n, m; int start_par, end_par; ll dp[2][64][2][1 << 8]; ll cnts[2][64][2][1 << 8]; int pawn_cnt; ll sum_somsiady[2]; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int max_cnt; int dp_id = 0; void transition(int height, int width, int cnt, int parity, int mask) { // Jeśli biorę kafelek do którego teraz rozszerzam. int delta_horizontal = 0; int take = (mask >> width & 1); dp[dp_id][cnt][parity][mask] = -1; cnts[dp_id][cnt][parity][mask] = -1; if (cnt < __builtin_popcount(mask)) { return; } if (width > 0) delta_horizontal = take ^ (mask >> (width - 1) & 1); int old_parity = (parity + take * (height + width)) % 2; // Case 1: W poprzednim separatorze nade mną nie było kafelka. // Case 2: W poprzednik separatorze nade mną jest kafelek int prev_mask = mask - take * (1 << width); ll delta = delta_horizontal + take; for (int i = 0; i < 2; i++) { ll prev_dp = dp[1 - dp_id][cnt - take][old_parity][prev_mask]; if (prev_dp >= 0) { if (dp[dp_id][cnt][parity][mask] < 0) dp[dp_id][cnt][parity][mask] = 0; if (cnts[dp_id][cnt][parity][mask] < 0) cnts[dp_id][cnt][parity][mask] = 0; cnts[dp_id][cnt][parity][mask] += cnts[1 - dp_id][cnt - take][old_parity][prev_mask]; ll change = prev_dp + delta * cnts[1 - dp_id][cnt - take][old_parity][prev_mask]; dp[dp_id][cnt][parity][mask] += change; // cerr << "add " << i << "->" << change << "\n"; } // Przejdź na drugi case. prev_mask ^= (1 << width); delta = delta_horizontal + (1 - take); } // cerr << dbg(max_cnt) << dbg(cnt) << dbg(parity) << dbg(mask) << dbg(old_parity) // << dbg(dp[dp_id][cnt][parity][mask]) << dbg(cnts[dp_id][cnt][parity][mask]) << "\n"; } int32_t main() { cin >> n >> m; State start_state(n + 1, vector<int>(m + 1, 0)), end_state(n + 1, vector<int>(m + 1, 0)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { start_state[i][j] = (s[j] == 'O'); if (start_state[i][j]) { pawn_cnt++; start_par = (start_par + i + j) % 2; } } } for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { end_state[i][j] = (s[j] == 'O'); if (end_state[i][j]) { end_par = (end_par + i + j) % 2; } } } if (start_par != end_par) { cout << "0\n"; return 0; } max_cnt = n; for (int cnt = 0; cnt < 64; cnt++) { for (int parity = 0; parity < 2; parity++) { for (int mask = 0; mask < (1 << n); mask++) { dp[0][cnt][parity][mask] = dp[1][cnt][parity][mask] = -1; cnts[0][cnt][parity][mask] = cnts[1][cnt][parity][mask] = -1; } } } // Wyliczamy przypadki bazowe for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0, parity = 0; for (int width = 0; width < n; width++) { int take = (mask >> width & 1); if (take) cnt++; parity = (parity + take * width) % 2; } dp[dp_id][cnt][parity][mask] = 0; cnts[dp_id][cnt][parity][mask] = 1; for (int width = 0; width < n; width++) { int take = (mask >> width & 1); if (!take) continue; if (width > 0) { dp[dp_id][cnt][parity][mask] += (take ^ (mask >> (width - 1) & 1)); } if (width + 1 < n) { dp[dp_id][cnt][parity][mask] += (take ^ (mask >> (width + 1) & 1)); } } // cerr << dbg(mask) << dbg(cnt) << dbg(parity) << dbg(dp[dp_id][cnt][parity][mask]) << // "\n"; } // Tu chyba sie ma zaczac od height=1, a pierwszy wiersz pałka. for (int height = 1; height < m; height++) { for (int width = 0; width < n; width++) { max_cnt++; dp_id = 1 - dp_id; for (int cnt = 0; cnt <= min(max_cnt, pawn_cnt); cnt++) { for (int parity = 0; parity < 2; parity++) { for (int mask = 0; mask < (1 << n); mask++) { transition(height, width, cnt, parity, mask); } } } } } for (int mask = 0; mask < (1 << n); mask++) { for (int parity = 0; parity < 2; parity++) { sum_somsiady[parity] += max(0LL, dp[dp_id][pawn_cnt][parity][mask]); } } ll somsiady_end = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int dir = 0; dir < 4; dir++) { int nx = i + dx[dir]; int ny = j + dy[dir]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) { continue; } somsiady_end += (end_state[i][j] && !end_state[nx][ny]); } } } // cerr << dbg(somsiady_end) << dbg(sum_somsiady[end_par]) << "\n"; cout << fixed << setprecision(15) << (ld)somsiady_end / sum_somsiady[end_par] << "\n"; }
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <bits/stdc++.h> #define dbg(x) #x << " = " << x << " " using ld = long double; using ll = long long; using namespace std; using State = vector<vector<int>>; int n, m; int start_par, end_par; ll dp[2][64][2][1 << 8]; ll cnts[2][64][2][1 << 8]; int pawn_cnt; ll sum_somsiady[2]; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int max_cnt; int dp_id = 0; void transition(int height, int width, int cnt, int parity, int mask) { // Jeśli biorę kafelek do którego teraz rozszerzam. int delta_horizontal = 0; int take = (mask >> width & 1); dp[dp_id][cnt][parity][mask] = -1; cnts[dp_id][cnt][parity][mask] = -1; if (cnt < __builtin_popcount(mask)) { return; } if (width > 0) delta_horizontal = take ^ (mask >> (width - 1) & 1); int old_parity = (parity + take * (height + width)) % 2; // Case 1: W poprzednim separatorze nade mną nie było kafelka. // Case 2: W poprzednik separatorze nade mną jest kafelek int prev_mask = mask - take * (1 << width); ll delta = delta_horizontal + take; for (int i = 0; i < 2; i++) { ll prev_dp = dp[1 - dp_id][cnt - take][old_parity][prev_mask]; if (prev_dp >= 0) { if (dp[dp_id][cnt][parity][mask] < 0) dp[dp_id][cnt][parity][mask] = 0; if (cnts[dp_id][cnt][parity][mask] < 0) cnts[dp_id][cnt][parity][mask] = 0; cnts[dp_id][cnt][parity][mask] += cnts[1 - dp_id][cnt - take][old_parity][prev_mask]; ll change = prev_dp + delta * cnts[1 - dp_id][cnt - take][old_parity][prev_mask]; dp[dp_id][cnt][parity][mask] += change; // cerr << "add " << i << "->" << change << "\n"; } // Przejdź na drugi case. prev_mask ^= (1 << width); delta = delta_horizontal + (1 - take); } // cerr << dbg(max_cnt) << dbg(cnt) << dbg(parity) << dbg(mask) << dbg(old_parity) // << dbg(dp[dp_id][cnt][parity][mask]) << dbg(cnts[dp_id][cnt][parity][mask]) << "\n"; } int32_t main() { cin >> n >> m; State start_state(n + 1, vector<int>(m + 1, 0)), end_state(n + 1, vector<int>(m + 1, 0)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { start_state[i][j] = (s[j] == 'O'); if (start_state[i][j]) { pawn_cnt++; start_par = (start_par + i + j) % 2; } } } for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { end_state[i][j] = (s[j] == 'O'); if (end_state[i][j]) { end_par = (end_par + i + j) % 2; } } } if (start_par != end_par) { cout << "0\n"; return 0; } max_cnt = n; for (int cnt = 0; cnt < 64; cnt++) { for (int parity = 0; parity < 2; parity++) { for (int mask = 0; mask < (1 << n); mask++) { dp[0][cnt][parity][mask] = dp[1][cnt][parity][mask] = -1; cnts[0][cnt][parity][mask] = cnts[1][cnt][parity][mask] = -1; } } } // Wyliczamy przypadki bazowe for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0, parity = 0; for (int width = 0; width < n; width++) { int take = (mask >> width & 1); if (take) cnt++; parity = (parity + take * width) % 2; } dp[dp_id][cnt][parity][mask] = 0; cnts[dp_id][cnt][parity][mask] = 1; for (int width = 0; width < n; width++) { int take = (mask >> width & 1); if (!take) continue; if (width > 0) { dp[dp_id][cnt][parity][mask] += (take ^ (mask >> (width - 1) & 1)); } if (width + 1 < n) { dp[dp_id][cnt][parity][mask] += (take ^ (mask >> (width + 1) & 1)); } } // cerr << dbg(mask) << dbg(cnt) << dbg(parity) << dbg(dp[dp_id][cnt][parity][mask]) << // "\n"; } // Tu chyba sie ma zaczac od height=1, a pierwszy wiersz pałka. for (int height = 1; height < m; height++) { for (int width = 0; width < n; width++) { max_cnt++; dp_id = 1 - dp_id; for (int cnt = 0; cnt <= min(max_cnt, pawn_cnt); cnt++) { for (int parity = 0; parity < 2; parity++) { for (int mask = 0; mask < (1 << n); mask++) { transition(height, width, cnt, parity, mask); } } } } } for (int mask = 0; mask < (1 << n); mask++) { for (int parity = 0; parity < 2; parity++) { sum_somsiady[parity] += max(0LL, dp[dp_id][pawn_cnt][parity][mask]); } } ll somsiady_end = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int dir = 0; dir < 4; dir++) { int nx = i + dx[dir]; int ny = j + dy[dir]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) { continue; } somsiady_end += (end_state[i][j] && !end_state[nx][ny]); } } } // cerr << dbg(somsiady_end) << dbg(sum_somsiady[end_par]) << "\n"; cout << fixed << setprecision(15) << (ld)somsiady_end / sum_somsiady[end_par] << "\n"; } |