#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> #include <iomanip> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, q, d, r, g, b, z, p, a, c, m, k1, k2; char zn; vs vw, vwk; string wa, wb; vi vk, va, vk1, vk2; vii vkx,vkx2; vvi vvz; vector<double> vax, vax2; vector<vector<double> > vvax, vvbx; map<int, int> hz,hz2,hz0; class Stan; vector<Stan *> vgs; class Stan { vii vkx; int zob; double px; double px_new; bool emp(pii p0) { for (int i = 0; i < (int)vkx.size(); i++) { if (vkx[i] == p0) return false; } return true; } vii gil(pii p0) { vii vaw; vaw.clear(); if ((p0.first > 0) && (emp(MP(p0.first - 1, p0.second)))) { vaw.push_back(MP(p0.first - 1, p0.second)); } if ((p0.first <n-1) && (emp(MP(p0.first + 1, p0.second)))) { vaw.push_back(MP(p0.first + 1, p0.second)); } if ((p0.second > 0) && (emp(MP(p0.first, p0.second-1)))) { vaw.push_back(MP(p0.first, p0.second-1)); } if ((p0.second < m-1) && (emp(MP(p0.first, p0.second + 1)))) { vaw.push_back(MP(p0.first, p0.second+1)); } return vaw; } int checka() { int da = 0; for (int i = 0; i < vkx.size(); i++) { if ((int)gil(vkx[i]).size() == 0) da++; } return da; } double getPxNew() { return px_new; } public: Stan(vii vpx,double px0) { vkx = vpx; px_new = px0; px = 0.0; zob = 0; for (int i = 0; i < (int)vkx.size(); i++) { zob ^= vvz[vkx[i].first][vkx[i].second]; } } Stan(vii vpx, double px0,int zobn) { vkx = vpx; px_new = px0; px = 0.0; zob = zobn; } void addP(double px0) { px_new += px0; } void getNext() { int da = checka(); a -= da; for (int i = 0; i < (int)vkx.size(); i++) { vii vaw = gil(vkx[i]); int aw = (int)vaw.size(); for (int j = 0; j < (int)vaw.size(); j++) { int nzob = zob ^ vvz[vkx[i].first][vkx[i].second]; nzob^= vvz[vaw[j].first][vaw[j].second]; map<int,int>::iterator it = hz0.find(nzob); if (it != hz0.end()) { if (vgs[hz0[nzob]]->getPxNew() == 0.0) vk2.push_back(hz0[nzob]); vgs[hz0[nzob]]->addP((px / a) / aw); } else { pii p1 = vkx[i]; vkx[i] = vaw[j]; Stan* nst = new Stan(vkx, (px / a) / aw,nzob); vgs.push_back(nst); hz0[nzob] = (int)vgs.size()-1; vkx[i] = p1; vk2.push_back((int)vgs.size() - 1); } } } a += da; } int getZob() { return zob; } void updP() { px = px_new; px_new = 0.0; } double getP() { return px; } }; Stan* st; Stan* stk; int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\3.in", "rt", stdin); //d = 1000000007; srand(79); //while (1) { cin >> n >> m; vk.assign(m, 0); vvz.assign(n, vk); vkx.clear(); vkx2.clear(); wa.assign(m, '.'); vw.assign(n, wa); vwk.assign(n, wa); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { vvz[i][j] = rand(); } } a = 0; for (int i = 0; i < n; i++) { cin >> vw[i]; } for (int i = 0; i < n; i++) { cin >> vwk[i]; } k1 = 0; k2 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (vw[i][j] == 'O') { k1 += i + j; vkx.push_back(MP(i, j)); } if (vwk[i][j] == 'O') { k2 += i + j; vkx2.push_back(MP(i, j)); } } } if ((k1 % 2) != (k2 % 2)) { cout << 0 << endl; return 0; } // while (1) { // cin >> k; k = 1000; st = new Stan(vkx,1.0); vgs.clear(); vgs.push_back(st); hz0.clear(); hz0[st->getZob()] = 0; bool ts = true; for (int i = 0; i < (int)vkx.size(); i++) { if (vkx[i] != vkx2[i]) { ts = false; break; } } if (!ts) { stk = new Stan(vkx2, 0.0); hz0[stk->getZob()] = 1; vgs.push_back(stk); } a = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (vw[i][j] == 'O') { //vvax[i][j] = 1.0; a++; } } } vk1.clear(); vk2.clear(); vk1.push_back(0); vgs[0]->updP(); for (int cas = 0; cas < k; cas++) { for (int ix = 0; ix < (int)vk1.size(); ix++) { vgs[vk1[ix]]->getNext(); } for (int i = 0; i < (int)vk2.size(); i++) vgs[vk2[i]]->updP(); for (int i = 0; i < (int)vk1.size(); i++) vgs[vk1[i]]->updP(); vk1.swap(vk2); vk2.clear(); } double zx; if (!ts) zx = vgs[1]->getP(); else zx = vgs[0]->getP(); cout << setprecision(15) << zx << endl; //} // } 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> #include <iomanip> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, q, d, r, g, b, z, p, a, c, m, k1, k2; char zn; vs vw, vwk; string wa, wb; vi vk, va, vk1, vk2; vii vkx,vkx2; vvi vvz; vector<double> vax, vax2; vector<vector<double> > vvax, vvbx; map<int, int> hz,hz2,hz0; class Stan; vector<Stan *> vgs; class Stan { vii vkx; int zob; double px; double px_new; bool emp(pii p0) { for (int i = 0; i < (int)vkx.size(); i++) { if (vkx[i] == p0) return false; } return true; } vii gil(pii p0) { vii vaw; vaw.clear(); if ((p0.first > 0) && (emp(MP(p0.first - 1, p0.second)))) { vaw.push_back(MP(p0.first - 1, p0.second)); } if ((p0.first <n-1) && (emp(MP(p0.first + 1, p0.second)))) { vaw.push_back(MP(p0.first + 1, p0.second)); } if ((p0.second > 0) && (emp(MP(p0.first, p0.second-1)))) { vaw.push_back(MP(p0.first, p0.second-1)); } if ((p0.second < m-1) && (emp(MP(p0.first, p0.second + 1)))) { vaw.push_back(MP(p0.first, p0.second+1)); } return vaw; } int checka() { int da = 0; for (int i = 0; i < vkx.size(); i++) { if ((int)gil(vkx[i]).size() == 0) da++; } return da; } double getPxNew() { return px_new; } public: Stan(vii vpx,double px0) { vkx = vpx; px_new = px0; px = 0.0; zob = 0; for (int i = 0; i < (int)vkx.size(); i++) { zob ^= vvz[vkx[i].first][vkx[i].second]; } } Stan(vii vpx, double px0,int zobn) { vkx = vpx; px_new = px0; px = 0.0; zob = zobn; } void addP(double px0) { px_new += px0; } void getNext() { int da = checka(); a -= da; for (int i = 0; i < (int)vkx.size(); i++) { vii vaw = gil(vkx[i]); int aw = (int)vaw.size(); for (int j = 0; j < (int)vaw.size(); j++) { int nzob = zob ^ vvz[vkx[i].first][vkx[i].second]; nzob^= vvz[vaw[j].first][vaw[j].second]; map<int,int>::iterator it = hz0.find(nzob); if (it != hz0.end()) { if (vgs[hz0[nzob]]->getPxNew() == 0.0) vk2.push_back(hz0[nzob]); vgs[hz0[nzob]]->addP((px / a) / aw); } else { pii p1 = vkx[i]; vkx[i] = vaw[j]; Stan* nst = new Stan(vkx, (px / a) / aw,nzob); vgs.push_back(nst); hz0[nzob] = (int)vgs.size()-1; vkx[i] = p1; vk2.push_back((int)vgs.size() - 1); } } } a += da; } int getZob() { return zob; } void updP() { px = px_new; px_new = 0.0; } double getP() { return px; } }; Stan* st; Stan* stk; int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\3.in", "rt", stdin); //d = 1000000007; srand(79); //while (1) { cin >> n >> m; vk.assign(m, 0); vvz.assign(n, vk); vkx.clear(); vkx2.clear(); wa.assign(m, '.'); vw.assign(n, wa); vwk.assign(n, wa); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { vvz[i][j] = rand(); } } a = 0; for (int i = 0; i < n; i++) { cin >> vw[i]; } for (int i = 0; i < n; i++) { cin >> vwk[i]; } k1 = 0; k2 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (vw[i][j] == 'O') { k1 += i + j; vkx.push_back(MP(i, j)); } if (vwk[i][j] == 'O') { k2 += i + j; vkx2.push_back(MP(i, j)); } } } if ((k1 % 2) != (k2 % 2)) { cout << 0 << endl; return 0; } // while (1) { // cin >> k; k = 1000; st = new Stan(vkx,1.0); vgs.clear(); vgs.push_back(st); hz0.clear(); hz0[st->getZob()] = 0; bool ts = true; for (int i = 0; i < (int)vkx.size(); i++) { if (vkx[i] != vkx2[i]) { ts = false; break; } } if (!ts) { stk = new Stan(vkx2, 0.0); hz0[stk->getZob()] = 1; vgs.push_back(stk); } a = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (vw[i][j] == 'O') { //vvax[i][j] = 1.0; a++; } } } vk1.clear(); vk2.clear(); vk1.push_back(0); vgs[0]->updP(); for (int cas = 0; cas < k; cas++) { for (int ix = 0; ix < (int)vk1.size(); ix++) { vgs[vk1[ix]]->getNext(); } for (int i = 0; i < (int)vk2.size(); i++) vgs[vk2[i]]->updP(); for (int i = 0; i < (int)vk1.size(); i++) vgs[vk1[i]]->updP(); vk1.swap(vk2); vk2.clear(); } double zx; if (!ts) zx = vgs[1]->getP(); else zx = vgs[0]->getP(); cout << setprecision(15) << zx << endl; //} // } return 0; } |