#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define pb push_back #define eb emplace_back #define mp make_pair #define f first #define s second #define tcT template<class T #define tcTU tcT, class U #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--) const db PI = acos(-1); mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; } tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; } tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; } tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } tcT> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) { return os << "(" << P.f << "," << P.s << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif using frac_int = ll; struct frac_t { frac_int a, b; frac_t(frac_int _a, frac_int _b) : a(_a), b(_b) { assert (b != 0); frac_int d = __gcd(a, b); a /= d; b /= d; } frac_t(frac_int _a) : a(_a), b(1) {} frac_t() : a(0), b(1) {} frac_t operator+(const frac_t &other) const { return frac_t(a*other.b + other.a*b, other.b * b); } frac_t operator-(const frac_t &other) const { return frac_t(a*other.b - other.a*b, other.b * b); } frac_t operator*(const frac_t &other) const { return frac_t(a*other.a, b*other.b); } frac_t operator/(const frac_t &other) const { assert (other.a != 0); return frac_t(a*other.b, b*other.a); } bool operator<(const frac_t &other) const { return a*other.b < b*other.a; } bool operator>(const frac_t &other) const { return a*other.b > b*other.a; } bool operator==(const frac_t &other) const { return a*other.b == b*other.a; } bool operator!=(const frac_t &other) const { return !(*this == other); } }; ostream& operator<<(ostream &os, const frac_t &frac) { return os << frac.a << "/" << frac.b; } int n, m; const int MAXN = 202; string s[MAXN]; frac_t alive[MAXN][MAXN]; ll deg[MAXN][MAXN]; constexpr pi moves[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; // boundaries excluded frac_t simulate(int x0, int y0, int x1, int y1) { rep(i,x0,x1) { rep(j,y0,y1) { if (s[i][j] == 'O') { alive[i][j] = 1; } else { alive[i][j] = 0; } } } auto valid = [&](int i, int j) -> bool { return x0 <= i && i < x1 && y0 <= j && j < y1; }; // wykonuje się max 11 razy while (true) { frac_t time = 2; rep(i,x0,x1) { rep(j,y0,y1) { if (alive[i][j] == 0) continue; deg[i][j] = 0; for (pi mv : moves) { int nxti = i + mv.f; int nxtj = j + mv.s; if (!valid(nxti, nxtj)) continue; if (alive[nxti][nxtj] != 0) { deg[i][j]++; } } if (deg[i][j] == 0) continue; ckmin(time, alive[i][j] / deg[i][j]); } } if (time == 2) break; rep(i,x0,x1) { rep(j,y0,y1) { if (alive[i][j] == 0) continue; alive[i][j] = alive[i][j] - time * deg[i][j]; }; } } frac_t res = 0; rep(i,x0,x1) { rep(j,y0,y1) { res = res + alive[i][j]; } } return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; rep(i,0,n) cin >> s[i]; vpi question_marks; rep(i,0,n) { rep(j,0,m) { if (s[i][j] == '?') { question_marks.eb(i, j); } } } frac_t res = 0; rep(mask,0,(1<<sz(question_marks))) { rep(i,0,sz(question_marks)) { s[question_marks[i].f][question_marks[i].s] = ((mask & (1 << i)) ? 'O' : '.'); } res = res + simulate(0,0,n,m); } res = res / frac_t((1 << sz(question_marks))); cout << res << "\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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define pb push_back #define eb emplace_back #define mp make_pair #define f first #define s second #define tcT template<class T #define tcTU tcT, class U #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--) const db PI = acos(-1); mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; } tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; } tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; } tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } tcT> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) { return os << "(" << P.f << "," << P.s << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif using frac_int = ll; struct frac_t { frac_int a, b; frac_t(frac_int _a, frac_int _b) : a(_a), b(_b) { assert (b != 0); frac_int d = __gcd(a, b); a /= d; b /= d; } frac_t(frac_int _a) : a(_a), b(1) {} frac_t() : a(0), b(1) {} frac_t operator+(const frac_t &other) const { return frac_t(a*other.b + other.a*b, other.b * b); } frac_t operator-(const frac_t &other) const { return frac_t(a*other.b - other.a*b, other.b * b); } frac_t operator*(const frac_t &other) const { return frac_t(a*other.a, b*other.b); } frac_t operator/(const frac_t &other) const { assert (other.a != 0); return frac_t(a*other.b, b*other.a); } bool operator<(const frac_t &other) const { return a*other.b < b*other.a; } bool operator>(const frac_t &other) const { return a*other.b > b*other.a; } bool operator==(const frac_t &other) const { return a*other.b == b*other.a; } bool operator!=(const frac_t &other) const { return !(*this == other); } }; ostream& operator<<(ostream &os, const frac_t &frac) { return os << frac.a << "/" << frac.b; } int n, m; const int MAXN = 202; string s[MAXN]; frac_t alive[MAXN][MAXN]; ll deg[MAXN][MAXN]; constexpr pi moves[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; // boundaries excluded frac_t simulate(int x0, int y0, int x1, int y1) { rep(i,x0,x1) { rep(j,y0,y1) { if (s[i][j] == 'O') { alive[i][j] = 1; } else { alive[i][j] = 0; } } } auto valid = [&](int i, int j) -> bool { return x0 <= i && i < x1 && y0 <= j && j < y1; }; // wykonuje się max 11 razy while (true) { frac_t time = 2; rep(i,x0,x1) { rep(j,y0,y1) { if (alive[i][j] == 0) continue; deg[i][j] = 0; for (pi mv : moves) { int nxti = i + mv.f; int nxtj = j + mv.s; if (!valid(nxti, nxtj)) continue; if (alive[nxti][nxtj] != 0) { deg[i][j]++; } } if (deg[i][j] == 0) continue; ckmin(time, alive[i][j] / deg[i][j]); } } if (time == 2) break; rep(i,x0,x1) { rep(j,y0,y1) { if (alive[i][j] == 0) continue; alive[i][j] = alive[i][j] - time * deg[i][j]; }; } } frac_t res = 0; rep(i,x0,x1) { rep(j,y0,y1) { res = res + alive[i][j]; } } return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; rep(i,0,n) cin >> s[i]; vpi question_marks; rep(i,0,n) { rep(j,0,m) { if (s[i][j] == '?') { question_marks.eb(i, j); } } } frac_t res = 0; rep(mask,0,(1<<sz(question_marks))) { rep(i,0,sz(question_marks)) { s[question_marks[i].f][question_marks[i].s] = ((mask & (1 << i)) ? 'O' : '.'); } res = res + simulate(0,0,n,m); } res = res / frac_t((1 << sz(question_marks))); cout << res << "\n"; return 0; } |