#include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> #include <iomanip> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 8; long long dp[2][(1 << maxN)][maxN + 1][maxN]; long long pp[2][(1 << maxN)][maxN + 1][maxN]; int mc[(1 << maxN)]; int mc2[(1 << maxN)][(1 << maxN)]; int par[2][(1 << maxN)]; bool has(int n, int k) { if (n & (1 << k)) return true; return false; } int32_t main() { fastio; int n, m; cin >> n >> m; int fir = 0; int firc = 0; rep(i, n) rep(j, m) { char c; cin >> c; if (c == 'O') { fir ^= ((i + j) & 1); ++ firc; } } int fin = 0; int finc = 0; long double findeg = 0; vector<string> s(n); rep(i, n) cin >> s[i]; rep(i, n) rep(j, m) { if (s[i][j] == 'O') { fin ^= ((i + j) & 1); ++ finc; if (i - 1 >= 0 && s[i - 1][j] == '.') findeg += 1; if (i + 1 < n && s[i + 1][j] == '.') findeg += 1; if (j - 1 >= 0 && s[i][j - 1] == '.') findeg += 1; if (j + 1 < m && s[i][j + 1] == '.') findeg += 1; } } if (fin != fir || firc != finc) { cout << setprecision(15) << fixed << 0.0 << '\n'; return 0; } rep(i, (1 << n)) { rep(j, (1 << n)) { int p = 0; rep(k, n) if (has(i, k) != has(j, k)) ++ p; mc2[i][j] = p; } int p = 0; int pr1 = 0; int pr2 = 0; rep(j, n) if (has(i, j)) { if (has(j, 0)) ++ pr1; else ++ pr2; } rep(j, n - 1) if (has(i, j) != has(i, j + 1)) ++ p; mc[i] = p; par[0][i] = pr1; par[1][i] = pr2; dp[pr1 & 1][i][__builtin_popcount(i)][0] = p; pp[pr1 & 1][i][__builtin_popcount(i)][0] = 1; } fwd(i, 1, m) rep(p1, 2) rep(M1, (1 << n)) rep(k1, firc + 1) rep(M2, (1 << n)) { int k = __builtin_popcount(M2); int p2 = (mc2[M1][M2] + mc[M2]); if (k + k1 <= firc) { pp[p1 ^ (par[i & 1][M2] & 1)][M2][k + k1][i] += pp[p1][M1][k1][i - 1]; dp[p1 ^ (par[i & 1][M2] & 1)][M2][k + k1][i] += dp[p1][M1][k1][i - 1]; dp[p1 ^ (par[i & 1][M2] & 1)][M2][k + k1][i] += pp[p1][M1][k1][i - 1] * p2; } } long double deg = 0; rep(i, (1 << n)) deg += dp[fin][i][finc][m - 1]; cout << setprecision(15) << fixed << findeg / deg << '\n'; 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 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 | #include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> #include <iomanip> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 8; long long dp[2][(1 << maxN)][maxN + 1][maxN]; long long pp[2][(1 << maxN)][maxN + 1][maxN]; int mc[(1 << maxN)]; int mc2[(1 << maxN)][(1 << maxN)]; int par[2][(1 << maxN)]; bool has(int n, int k) { if (n & (1 << k)) return true; return false; } int32_t main() { fastio; int n, m; cin >> n >> m; int fir = 0; int firc = 0; rep(i, n) rep(j, m) { char c; cin >> c; if (c == 'O') { fir ^= ((i + j) & 1); ++ firc; } } int fin = 0; int finc = 0; long double findeg = 0; vector<string> s(n); rep(i, n) cin >> s[i]; rep(i, n) rep(j, m) { if (s[i][j] == 'O') { fin ^= ((i + j) & 1); ++ finc; if (i - 1 >= 0 && s[i - 1][j] == '.') findeg += 1; if (i + 1 < n && s[i + 1][j] == '.') findeg += 1; if (j - 1 >= 0 && s[i][j - 1] == '.') findeg += 1; if (j + 1 < m && s[i][j + 1] == '.') findeg += 1; } } if (fin != fir || firc != finc) { cout << setprecision(15) << fixed << 0.0 << '\n'; return 0; } rep(i, (1 << n)) { rep(j, (1 << n)) { int p = 0; rep(k, n) if (has(i, k) != has(j, k)) ++ p; mc2[i][j] = p; } int p = 0; int pr1 = 0; int pr2 = 0; rep(j, n) if (has(i, j)) { if (has(j, 0)) ++ pr1; else ++ pr2; } rep(j, n - 1) if (has(i, j) != has(i, j + 1)) ++ p; mc[i] = p; par[0][i] = pr1; par[1][i] = pr2; dp[pr1 & 1][i][__builtin_popcount(i)][0] = p; pp[pr1 & 1][i][__builtin_popcount(i)][0] = 1; } fwd(i, 1, m) rep(p1, 2) rep(M1, (1 << n)) rep(k1, firc + 1) rep(M2, (1 << n)) { int k = __builtin_popcount(M2); int p2 = (mc2[M1][M2] + mc[M2]); if (k + k1 <= firc) { pp[p1 ^ (par[i & 1][M2] & 1)][M2][k + k1][i] += pp[p1][M1][k1][i - 1]; dp[p1 ^ (par[i & 1][M2] & 1)][M2][k + k1][i] += dp[p1][M1][k1][i - 1]; dp[p1 ^ (par[i & 1][M2] & 1)][M2][k + k1][i] += pp[p1][M1][k1][i - 1] * p2; } } long double deg = 0; rep(i, (1 << n)) deg += dp[fin][i][finc][m - 1]; cout << setprecision(15) << fixed << findeg / deg << '\n'; return 0; } |