#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif struct frac { LL nom, den; frac() { nom = 0; den = 1; } frac(LL a, LL b) : nom(a), den(b) {} friend bool operator<(frac p, frac q) { return p.nom * q.den < q.nom * p.den; } friend frac operator+(frac p, frac q) { frac ret(p.nom * q.den + q.nom * p.den, p.den * q.den); LL g = __gcd(ret.nom, ret.den); return frac(ret.nom / g, ret.den / g); } friend frac operator-(frac p, frac q) { frac ret(p.nom * q.den - q.nom * p.den, p.den * q.den); LL g = __gcd(ret.nom, ret.den); return frac(ret.nom / g, ret.den / g); } friend frac operator*(frac p, frac q) { p.nom *= q.nom; p.den *= q.den; LL g = __gcd(p.nom, p.den); return frac(p.nom / g, p.den / g); } friend frac operator/(frac p, frac q) { swap(q.nom, q.den); return p * q; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector t(n, vector (m, 'a')); REP (i, n) REP (j, m) cin >> t[i][j]; auto solve = [&](vector<vector<char>> p) { vector cur(n, vector (m, frac(1, 1))); vector odw(n, vector (m, 0)); vector zli(5, vector<pair<int, int>>()); int licz = 0; auto good = [&](int x, int y) { if (x < 0 || y < 0 || n <= x || m <= y) return false; return p[x][y] == 'O'; }; function<void(int, int)> dfs = [&](int x, int y) { odw[x][y] = licz; int deg = 0; if (good(x - 1, y)) { ++deg; if (odw[x - 1][y] < licz) dfs(x - 1, y); } if (good(x + 1, y)) { ++deg; if (odw[x + 1][y] < licz) dfs(x + 1, y); } if (good(x, y - 1)) { ++deg; if (odw[x][y - 1] < licz) dfs(x, y - 1); } if (good(x, y + 1)) { ++deg; if (odw[x][y + 1] < licz) dfs(x, y + 1); } zli[deg].emplace_back(x, y); }; bool ok = true; while (ok) { ok = false; ++licz; REP (i, n) { REP (j, m) { if (odw[i][j] < licz && p[i][j] == 'O') { REP (k, 5) zli[k].clear(); dfs(i, j); if (ssize(zli[0])) continue; ok = true; frac score(100, 0); FOR (k, 1, 4) { for (auto [a, b] : zli[k]) { score = min(score, cur[a][b] / frac(k, 1)); } } FOR (k, 1, 4) { for (auto [a, b] : zli[k]) { cur[a][b] = cur[a][b] - frac(k, 1) * score; if (cur[a][b].nom == 0) p[a][b] = '.'; } } } } } } frac sum; REP (i, n) REP (j, m) if (p[i][j] == 'O') sum = sum + cur[i][j]; return sum; }; vector<pair<int, int>> znaki; REP (i, n) { REP (j, m) { if (t[i][j] == '?') znaki.emplace_back(i, j); } } frac odp; int M = ssize(znaki); REP (mask, 1 << M) { REP (i, M) { if ((mask >> i) & 1) t[znaki[i].first][znaki[i].second] = 'O'; else t[znaki[i].first][znaki[i].second] = '.'; } auto ans = solve(t); odp = odp + ans; } odp = odp / frac(1 << M, 1); cout << odp.nom << '/' << odp.den << endl; }
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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif struct frac { LL nom, den; frac() { nom = 0; den = 1; } frac(LL a, LL b) : nom(a), den(b) {} friend bool operator<(frac p, frac q) { return p.nom * q.den < q.nom * p.den; } friend frac operator+(frac p, frac q) { frac ret(p.nom * q.den + q.nom * p.den, p.den * q.den); LL g = __gcd(ret.nom, ret.den); return frac(ret.nom / g, ret.den / g); } friend frac operator-(frac p, frac q) { frac ret(p.nom * q.den - q.nom * p.den, p.den * q.den); LL g = __gcd(ret.nom, ret.den); return frac(ret.nom / g, ret.den / g); } friend frac operator*(frac p, frac q) { p.nom *= q.nom; p.den *= q.den; LL g = __gcd(p.nom, p.den); return frac(p.nom / g, p.den / g); } friend frac operator/(frac p, frac q) { swap(q.nom, q.den); return p * q; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector t(n, vector (m, 'a')); REP (i, n) REP (j, m) cin >> t[i][j]; auto solve = [&](vector<vector<char>> p) { vector cur(n, vector (m, frac(1, 1))); vector odw(n, vector (m, 0)); vector zli(5, vector<pair<int, int>>()); int licz = 0; auto good = [&](int x, int y) { if (x < 0 || y < 0 || n <= x || m <= y) return false; return p[x][y] == 'O'; }; function<void(int, int)> dfs = [&](int x, int y) { odw[x][y] = licz; int deg = 0; if (good(x - 1, y)) { ++deg; if (odw[x - 1][y] < licz) dfs(x - 1, y); } if (good(x + 1, y)) { ++deg; if (odw[x + 1][y] < licz) dfs(x + 1, y); } if (good(x, y - 1)) { ++deg; if (odw[x][y - 1] < licz) dfs(x, y - 1); } if (good(x, y + 1)) { ++deg; if (odw[x][y + 1] < licz) dfs(x, y + 1); } zli[deg].emplace_back(x, y); }; bool ok = true; while (ok) { ok = false; ++licz; REP (i, n) { REP (j, m) { if (odw[i][j] < licz && p[i][j] == 'O') { REP (k, 5) zli[k].clear(); dfs(i, j); if (ssize(zli[0])) continue; ok = true; frac score(100, 0); FOR (k, 1, 4) { for (auto [a, b] : zli[k]) { score = min(score, cur[a][b] / frac(k, 1)); } } FOR (k, 1, 4) { for (auto [a, b] : zli[k]) { cur[a][b] = cur[a][b] - frac(k, 1) * score; if (cur[a][b].nom == 0) p[a][b] = '.'; } } } } } } frac sum; REP (i, n) REP (j, m) if (p[i][j] == 'O') sum = sum + cur[i][j]; return sum; }; vector<pair<int, int>> znaki; REP (i, n) { REP (j, m) { if (t[i][j] == '?') znaki.emplace_back(i, j); } } frac odp; int M = ssize(znaki); REP (mask, 1 << M) { REP (i, M) { if ((mask >> i) & 1) t[znaki[i].first][znaki[i].second] = 'O'; else t[znaki[i].first][znaki[i].second] = '.'; } auto ans = solve(t); odp = odp + ans; } odp = odp / frac(1 << M, 1); cout << odp.nom << '/' << odp.den << endl; } |