#include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() ostream& operator<<(ostream& os, vector<int>& v) { for(auto x:v) cout << x; return os; } ostream& operator<<(ostream& os, vector<vector<int>>& v) { for(auto x:v) cout << x << "\n"; return os; } int n, m; vector<pii> V; unordered_map<int, vector<int>> G; unordered_map<int, double> M1, M2; int suma; int hasz(vector<vector<int>> &v) { int ans = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(v[i][j] == 1) { ans += 1LL<<(i*m+j); } } } return ans; } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; void f(vector<vector<int>> &v) { int h = hasz(v); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(v[i][j]) { for(int k=0;k<4;k++) { int nx = i + dx[k]; int ny = j + dy[k]; if(nx>=0 and nx<n and ny>=0 and ny<m and v[nx][ny]==0) { v[nx][ny] = 1; v[i][j] = 0; G[h].pb(hasz(v)); suma++; v[nx][ny] = 0; v[i][j] = 1; } } } } } } void gen(vector<vector<int>> &v, int ile, int x, int y) { if(ile == sz(V)) { f(v); return; } if(x == n) return; gen(v, ile, y==m-1?x+1:x, y==m-1?0:y+1); v[x][y] = true; gen(v, ile+1, y==m-1?x+1:x, y==m-1?0:y+1); v[x][y] = false; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(15); cout << fixed; cin >> n >> m; vector<vector<int>> v1(n, vector<int>(m, 0)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin >> c; if(c == 'O') { v1[i][j] = 1; V.pb({i, j}); } } } vector<vector<int>> v(n, vector<int>(m, 0)); gen(v, 0, 0, 0); M1[hasz(v1)] = 1; int iter = 5e6/suma; if(iter%2) iter--; while(iter--) { M2.clear(); for(auto x:M1) { for(int y:G[x.st]) { M2[y] += x.nd/sz(G[x.st]); } } M1.clear(); for(auto x:M2) M1[x.st] = x.nd; } vector<vector<int>> v2(n, vector<int>(m, 0)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin >> c; if(c == 'O') { v2[i][j] = 1; } } } if(M1.count(hasz(v2))==0) { cout << "0\n"; return 0; } cout << M1[hasz(v2)] << "\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 | #include <bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define st first #define nd second #define pii pair<int, int> #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() ostream& operator<<(ostream& os, vector<int>& v) { for(auto x:v) cout << x; return os; } ostream& operator<<(ostream& os, vector<vector<int>>& v) { for(auto x:v) cout << x << "\n"; return os; } int n, m; vector<pii> V; unordered_map<int, vector<int>> G; unordered_map<int, double> M1, M2; int suma; int hasz(vector<vector<int>> &v) { int ans = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(v[i][j] == 1) { ans += 1LL<<(i*m+j); } } } return ans; } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; void f(vector<vector<int>> &v) { int h = hasz(v); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(v[i][j]) { for(int k=0;k<4;k++) { int nx = i + dx[k]; int ny = j + dy[k]; if(nx>=0 and nx<n and ny>=0 and ny<m and v[nx][ny]==0) { v[nx][ny] = 1; v[i][j] = 0; G[h].pb(hasz(v)); suma++; v[nx][ny] = 0; v[i][j] = 1; } } } } } } void gen(vector<vector<int>> &v, int ile, int x, int y) { if(ile == sz(V)) { f(v); return; } if(x == n) return; gen(v, ile, y==m-1?x+1:x, y==m-1?0:y+1); v[x][y] = true; gen(v, ile+1, y==m-1?x+1:x, y==m-1?0:y+1); v[x][y] = false; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(15); cout << fixed; cin >> n >> m; vector<vector<int>> v1(n, vector<int>(m, 0)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin >> c; if(c == 'O') { v1[i][j] = 1; V.pb({i, j}); } } } vector<vector<int>> v(n, vector<int>(m, 0)); gen(v, 0, 0, 0); M1[hasz(v1)] = 1; int iter = 5e6/suma; if(iter%2) iter--; while(iter--) { M2.clear(); for(auto x:M1) { for(int y:G[x.st]) { M2[y] += x.nd/sz(G[x.st]); } } M1.clear(); for(auto x:M2) M1[x.st] = x.nd; } vector<vector<int>> v2(n, vector<int>(m, 0)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { char c; cin >> c; if(c == 'O') { v2[i][j] = 1; } } } if(M1.count(hasz(v2))==0) { cout << "0\n"; return 0; } cout << M1[hasz(v2)] << "\n"; } |