#include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <stdio.h> #include <deque> #include <unordered_map> #include <cassert> #include <numeric> #include <queue> #include <list> #include <bitset> using namespace std; #define ULL unsigned long long static int ROWS, COLS; struct Fraction { int top,bottom; Fraction(int a,int b) : top(a), bottom(b) {}; double val() { return (double)top / (double)bottom; } }; struct Plansza { bitset<64> cell; Plansza() {} void set_row(int r, string s) { for (int i=0; i < s.size(); i++) { cell[r*8 + i] = s[i] == 'O' ? 1 : 0; } } ULL id() { return cell.to_ullong(); } bool operator==(const Plansza &other) { return this->cell == other.cell; } bool within_bounds(int r, int c) { if (r < 0 || r >= ROWS) return false; if (c < 0 || c >= COLS) return false; return true; } int idFrom(int r, int c) { return r * 8 + c; } vector<Plansza> getAdj() { vector<Plansza> res; const vector<pair<int, int> > DIR = {{1,0}, {-1, 0}, {0, 1}, {0,-1}}; for (int r=0; r < ROWS; r++) { for (int c=0; c < COLS; c++) { if (!cell[idFrom(r,c)]) continue; for (int dir=0; dir < 4; dir++) { int nr = r + DIR[dir].first; int nc = c + DIR[dir].second; if (!within_bounds(nr, nc)) continue; if (cell[idFrom(nr, nc)]) continue; Plansza next = *this; next.cell[idFrom(r,c)] = 0; next.cell[idFrom(nr, nc)] = 1; res.push_back(next); } } } return res; } void print() { for (int r=0; r < ROWS; r++) { for (int c=0; c < COLS; c++) { printf("%c", cell[r*8+c] ? 'O' : '.'); } printf("\n"); } } }; unordered_map<ULL, vector<Plansza> > bfs(Plansza p, Plansza dest) { queue<Plansza> Q; Q.push(p); unordered_map<ULL, bool> seen; unordered_map<ULL, vector<Plansza> > transtions; while (!Q.empty()) { Plansza current = Q.front(); Q.pop(); if (seen[current.id()]) continue; seen[current.id()] = true; //printf("current: %llu\n", current.id()); current.print(); vector<Plansza> adj = current.getAdj(); //printf("next: "); for (auto &next : adj) { //printf("%llu[%d/%d],", next.first.id()); Q.push(next); } transtions[current.id()] = adj; //printf("\n"); } //printf("All states: %d\n", transtions.size()); //printf("END\n"); return transtions; } double random_walk(Plansza start, unordered_map<ULL, vector<Plansza> > transitions, Plansza dest) { unordered_map<ULL, double> dpCurrent, dpNext; for (auto &it : transitions) { dpCurrent[it.first] = 0.0; } dpCurrent[start.id()] = 1.0; for (int step=1; step <= 1000; step++) { //printf("Step %d\n", step); for (auto &it : transitions) { ULL v = it.first; double dpNextV = 0.0; auto adj = it.second; //printf("%d adj size for v=%llu\n", adj.size(), v); for (Plansza &next : adj) { ULL u = next.id(); double dpU = dpCurrent[u]; int degU = transitions[u].size(); assert (degU > 0); //printf("dpNextV: u=%ld, dpU=%lf, degU=%lf,", u, dpU, degU); dpNextV += dpCurrent[u] / degU; } //printf("\n"); dpNext[v] = dpNextV; } dpCurrent = dpNext; //printf("Step: %d:", step); for (auto &it : dpCurrent) { printf("%lld->%.3lf: ", it.first, it.second); } printf("\n"); //printf("Step: %d Dest probability: %lf\n", step, dpCurrent[dest.id()]); } return dpCurrent[dest.id()]; } int main() { scanf("%d%d", &ROWS, &COLS); char buf[20]; Plansza p; for (int i=0; i < ROWS; i++) { scanf("%s", &buf); p.set_row(i, string(buf)); } Plansza dest; for (int i=0; i < ROWS; i++) { scanf("%s", &buf); dest.set_row(i, string(buf)); } auto transitions = bfs(p, dest); double res = random_walk(p, transitions, dest); //dest.print(); printf("%.13lf\n", res); 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 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <math.h> #include <stdio.h> #include <deque> #include <unordered_map> #include <cassert> #include <numeric> #include <queue> #include <list> #include <bitset> using namespace std; #define ULL unsigned long long static int ROWS, COLS; struct Fraction { int top,bottom; Fraction(int a,int b) : top(a), bottom(b) {}; double val() { return (double)top / (double)bottom; } }; struct Plansza { bitset<64> cell; Plansza() {} void set_row(int r, string s) { for (int i=0; i < s.size(); i++) { cell[r*8 + i] = s[i] == 'O' ? 1 : 0; } } ULL id() { return cell.to_ullong(); } bool operator==(const Plansza &other) { return this->cell == other.cell; } bool within_bounds(int r, int c) { if (r < 0 || r >= ROWS) return false; if (c < 0 || c >= COLS) return false; return true; } int idFrom(int r, int c) { return r * 8 + c; } vector<Plansza> getAdj() { vector<Plansza> res; const vector<pair<int, int> > DIR = {{1,0}, {-1, 0}, {0, 1}, {0,-1}}; for (int r=0; r < ROWS; r++) { for (int c=0; c < COLS; c++) { if (!cell[idFrom(r,c)]) continue; for (int dir=0; dir < 4; dir++) { int nr = r + DIR[dir].first; int nc = c + DIR[dir].second; if (!within_bounds(nr, nc)) continue; if (cell[idFrom(nr, nc)]) continue; Plansza next = *this; next.cell[idFrom(r,c)] = 0; next.cell[idFrom(nr, nc)] = 1; res.push_back(next); } } } return res; } void print() { for (int r=0; r < ROWS; r++) { for (int c=0; c < COLS; c++) { printf("%c", cell[r*8+c] ? 'O' : '.'); } printf("\n"); } } }; unordered_map<ULL, vector<Plansza> > bfs(Plansza p, Plansza dest) { queue<Plansza> Q; Q.push(p); unordered_map<ULL, bool> seen; unordered_map<ULL, vector<Plansza> > transtions; while (!Q.empty()) { Plansza current = Q.front(); Q.pop(); if (seen[current.id()]) continue; seen[current.id()] = true; //printf("current: %llu\n", current.id()); current.print(); vector<Plansza> adj = current.getAdj(); //printf("next: "); for (auto &next : adj) { //printf("%llu[%d/%d],", next.first.id()); Q.push(next); } transtions[current.id()] = adj; //printf("\n"); } //printf("All states: %d\n", transtions.size()); //printf("END\n"); return transtions; } double random_walk(Plansza start, unordered_map<ULL, vector<Plansza> > transitions, Plansza dest) { unordered_map<ULL, double> dpCurrent, dpNext; for (auto &it : transitions) { dpCurrent[it.first] = 0.0; } dpCurrent[start.id()] = 1.0; for (int step=1; step <= 1000; step++) { //printf("Step %d\n", step); for (auto &it : transitions) { ULL v = it.first; double dpNextV = 0.0; auto adj = it.second; //printf("%d adj size for v=%llu\n", adj.size(), v); for (Plansza &next : adj) { ULL u = next.id(); double dpU = dpCurrent[u]; int degU = transitions[u].size(); assert (degU > 0); //printf("dpNextV: u=%ld, dpU=%lf, degU=%lf,", u, dpU, degU); dpNextV += dpCurrent[u] / degU; } //printf("\n"); dpNext[v] = dpNextV; } dpCurrent = dpNext; //printf("Step: %d:", step); for (auto &it : dpCurrent) { printf("%lld->%.3lf: ", it.first, it.second); } printf("\n"); //printf("Step: %d Dest probability: %lf\n", step, dpCurrent[dest.id()]); } return dpCurrent[dest.id()]; } int main() { scanf("%d%d", &ROWS, &COLS); char buf[20]; Plansza p; for (int i=0; i < ROWS; i++) { scanf("%s", &buf); p.set_row(i, string(buf)); } Plansza dest; for (int i=0; i < ROWS; i++) { scanf("%s", &buf); dest.set_row(i, string(buf)); } auto transitions = bfs(p, dest); double res = random_walk(p, transitions, dest); //dest.print(); printf("%.13lf\n", res); return 0; } |