#include <iostream> #include <vector> #include <random> #include <iomanip> #define PAWN 'O' #define EMPTY '.' #define DOWN 0 #define UP 1 #define LEFT 2 #define RIGHT 3 using namespace std; void debug(const vector<vector<char>> &v) { for (int i = 0; i < v.size(); ++i) { for (int j = 0; j < v[0].size(); ++j) { cout << v[i][j]; } cout << '\n'; } cout << '\n'; } mt19937 rng(2137); int random_range(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } int random_element(const vector<int> &v) { return v[random_range(0, v.size() - 1)]; } bool can_move(const pair<int, int> &pawn, const vector<vector<char>> &pos, int n, int m) { int i = pawn.first; int j = pawn.second; return (i + 1 < n && pos[i + 1][j] == EMPTY) || (i - 1 >= 0 && pos[i - 1][j] == EMPTY) || (j + 1 < m && pos[i][j + 1] == EMPTY) || (j - 1 >= 0 && pos[i][j - 1] == EMPTY); } vector<int> pawn_directions(const pair<int, int> &pawn, const vector<vector<char>> &pos, int n, int m) { int i = pawn.first; int j = pawn.second; vector<int> ret; if (i + 1 < n && pos[i + 1][j] == EMPTY) { ret.push_back(DOWN); } if (i - 1 >= 0 && pos[i - 1][j] == EMPTY) { ret.push_back(UP); } if (j - 1 >= 0 && pos[i][j - 1] == EMPTY) { ret.push_back(LEFT); } if (j + 1 < m && pos[i][j + 1] == EMPTY) { ret.push_back(RIGHT); } return ret; } void make_move2(const pair<pair<int, int>, pair<int, int>> &move, vector<vector<char>> &init_pos, vector<pair<int, int>> &pawns) { swap(init_pos[move.first.first][move.first.second], init_pos[move.second.first][move.second.second]); int pawn_index_in_pawns = -1; for (int k = 0; k < pawns.size(); ++k) { if (pawns[k].first == move.first.first && pawns[k].second == move.first.second) { pawn_index_in_pawns = k; break; } } pawns[pawn_index_in_pawns] = move.second; } void make_move(const pair<int, int> &pawn, int direction, vector<vector<char>> &init_pos, vector<pair<int, int>> &pawns) { int i = pawn.first; int j = pawn.second; int pawn_index_in_pawns = -1; for (int k = 0; k < pawns.size(); ++k) { if (pawns[k].first == i && pawns[k].second == j) { pawn_index_in_pawns = k; break; } } pair<int, int> new_pawn_index = {i, j}; switch (direction) { case DOWN: ++new_pawn_index.first; break; case UP: --new_pawn_index.first; break; case LEFT: --new_pawn_index.second; break; case RIGHT: ++new_pawn_index.second; break; } pawns[pawn_index_in_pawns] = new_pawn_index; swap(init_pos[i][j], init_pos[new_pawn_index.first][new_pawn_index.second]); } long double solve(vector<vector<char>> init_pos, const vector<vector<char>> &end_pos) { int n = init_pos.size(); int m = init_pos[0].size(); vector<pair<int, int>> pawns; for (int i = 0; i < init_pos.size(); ++i) { for (int j = 0; j < init_pos[0].size(); ++j) { if (init_pos[i][j] == PAWN) { pawns.push_back({i, j}); } } } const int simulation_cnt = 1e6; int end_pos_cnt = 0; for (int i = 0; i < simulation_cnt; ++i) { /* vector<pair<int, int>> pawns_can_move; for (auto pawn : pawns) { if (can_move(pawn, init_pos, n, m)) { pawns_can_move.push_back(pawn); } } pair<int, int> pawn = pawns_can_move[random_range(0, pawns.size() - 1)]; vector<int> directions = pawn_directions(pawn, init_pos, n, m); int direction = random_element(directions); make_move(pawn, direction, init_pos, pawns); */ vector<pair<pair<int, int>, pair<int, int>>> moves; for (const auto &pawn : pawns) { vector<int> directions = pawn_directions(pawn, init_pos, n, m); for (const int &direction : directions) { switch (direction) { case UP: moves.push_back({pawn, {pawn.first - 1, pawn.second}}); break; case DOWN: moves.push_back({pawn, {pawn.first + 1, pawn.second}}); break; case LEFT: moves.push_back({pawn, {pawn.first, pawn.second - 1}}); break; case RIGHT: moves.push_back({pawn, {pawn.first, pawn.second + 1}}); break; } } } int move_numb = random_range(0, moves.size() - 1); make_move2(moves[move_numb], init_pos, pawns); // debug(init_pos); // debug(end_pos); if (i % 2 != 0) { // debug(init_pos); bool equal = true; for (int k = 0; k < n; ++k) { for (int l = 0; l < m; ++l) { if (init_pos[k][l] != end_pos[k][l]) { equal = false; break; } } } if (equal) { ++end_pos_cnt; } } } cout << end_pos_cnt << '\n'; cout << simulation_cnt << '\n'; return (long double)end_pos_cnt*2/simulation_cnt; } long double solve2(vector<vector<char>> init_pos, const vector<vector<char>> &end_pos) { int n = init_pos.size(); int m = init_pos[0].size(); const int simulation_cnt = 1e5; int end_pos_cnt = 0; for (int i = 0; i < simulation_cnt; ++i) { if (i % 2 == 0) { bool equal = true; for (int j = 0; j < n; ++j) { for (int k = 0; k < m; ++k) { if (init_pos[j][k] != end_pos[j][k]) { equal = false; break; } } } if (equal) { ++end_pos_cnt; } } vector<pair<pair<int, int>, pair<int, int>>> moves; for (int j = 0; j < n; ++j) { for (int k = 0; k < m; ++k) { if (init_pos[j][k] == EMPTY) { continue; } if (j + 1 < n && init_pos[j + 1][k] == EMPTY) { moves.push_back({{j, k}, {j + 1, k}}); } if (j - 1 >= 0 && init_pos[j - 1][k] == EMPTY) { moves.push_back({{j, k}, {j - 1, k}}); } if (k + 1 < m && init_pos[j][k + 1] == EMPTY) { moves.push_back({{j, k}, {j, k + 1}}); } if (k - 1 >= 0 && init_pos[j][k - 1] == EMPTY) { moves.push_back({{j, k}, {j, k - 1}}); } } } int move_numb = random_range(0, moves.size() - 1); auto move = moves[move_numb]; swap(init_pos[move.first.first][move.first.second], init_pos[move.second.first][move.second.second]); } return (long double)end_pos_cnt * 2.0 / (long double)simulation_cnt; } int main() { int n, m; cin >> n >> m; vector<vector<char>> init_pos(n, vector<char>(m)); for (auto &row : init_pos) { for (char &entry : row) { cin >> entry; } } string empty_line; getline(cin, empty_line); vector<vector<char>> end_pos(n, vector<char>(m)); for (auto &row : end_pos) { for (char &entry : row) { cin >> entry; } } long double answer = solve2(init_pos, end_pos); cout << fixed << setprecision(15) << answer << 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 273 274 | #include <iostream> #include <vector> #include <random> #include <iomanip> #define PAWN 'O' #define EMPTY '.' #define DOWN 0 #define UP 1 #define LEFT 2 #define RIGHT 3 using namespace std; void debug(const vector<vector<char>> &v) { for (int i = 0; i < v.size(); ++i) { for (int j = 0; j < v[0].size(); ++j) { cout << v[i][j]; } cout << '\n'; } cout << '\n'; } mt19937 rng(2137); int random_range(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } int random_element(const vector<int> &v) { return v[random_range(0, v.size() - 1)]; } bool can_move(const pair<int, int> &pawn, const vector<vector<char>> &pos, int n, int m) { int i = pawn.first; int j = pawn.second; return (i + 1 < n && pos[i + 1][j] == EMPTY) || (i - 1 >= 0 && pos[i - 1][j] == EMPTY) || (j + 1 < m && pos[i][j + 1] == EMPTY) || (j - 1 >= 0 && pos[i][j - 1] == EMPTY); } vector<int> pawn_directions(const pair<int, int> &pawn, const vector<vector<char>> &pos, int n, int m) { int i = pawn.first; int j = pawn.second; vector<int> ret; if (i + 1 < n && pos[i + 1][j] == EMPTY) { ret.push_back(DOWN); } if (i - 1 >= 0 && pos[i - 1][j] == EMPTY) { ret.push_back(UP); } if (j - 1 >= 0 && pos[i][j - 1] == EMPTY) { ret.push_back(LEFT); } if (j + 1 < m && pos[i][j + 1] == EMPTY) { ret.push_back(RIGHT); } return ret; } void make_move2(const pair<pair<int, int>, pair<int, int>> &move, vector<vector<char>> &init_pos, vector<pair<int, int>> &pawns) { swap(init_pos[move.first.first][move.first.second], init_pos[move.second.first][move.second.second]); int pawn_index_in_pawns = -1; for (int k = 0; k < pawns.size(); ++k) { if (pawns[k].first == move.first.first && pawns[k].second == move.first.second) { pawn_index_in_pawns = k; break; } } pawns[pawn_index_in_pawns] = move.second; } void make_move(const pair<int, int> &pawn, int direction, vector<vector<char>> &init_pos, vector<pair<int, int>> &pawns) { int i = pawn.first; int j = pawn.second; int pawn_index_in_pawns = -1; for (int k = 0; k < pawns.size(); ++k) { if (pawns[k].first == i && pawns[k].second == j) { pawn_index_in_pawns = k; break; } } pair<int, int> new_pawn_index = {i, j}; switch (direction) { case DOWN: ++new_pawn_index.first; break; case UP: --new_pawn_index.first; break; case LEFT: --new_pawn_index.second; break; case RIGHT: ++new_pawn_index.second; break; } pawns[pawn_index_in_pawns] = new_pawn_index; swap(init_pos[i][j], init_pos[new_pawn_index.first][new_pawn_index.second]); } long double solve(vector<vector<char>> init_pos, const vector<vector<char>> &end_pos) { int n = init_pos.size(); int m = init_pos[0].size(); vector<pair<int, int>> pawns; for (int i = 0; i < init_pos.size(); ++i) { for (int j = 0; j < init_pos[0].size(); ++j) { if (init_pos[i][j] == PAWN) { pawns.push_back({i, j}); } } } const int simulation_cnt = 1e6; int end_pos_cnt = 0; for (int i = 0; i < simulation_cnt; ++i) { /* vector<pair<int, int>> pawns_can_move; for (auto pawn : pawns) { if (can_move(pawn, init_pos, n, m)) { pawns_can_move.push_back(pawn); } } pair<int, int> pawn = pawns_can_move[random_range(0, pawns.size() - 1)]; vector<int> directions = pawn_directions(pawn, init_pos, n, m); int direction = random_element(directions); make_move(pawn, direction, init_pos, pawns); */ vector<pair<pair<int, int>, pair<int, int>>> moves; for (const auto &pawn : pawns) { vector<int> directions = pawn_directions(pawn, init_pos, n, m); for (const int &direction : directions) { switch (direction) { case UP: moves.push_back({pawn, {pawn.first - 1, pawn.second}}); break; case DOWN: moves.push_back({pawn, {pawn.first + 1, pawn.second}}); break; case LEFT: moves.push_back({pawn, {pawn.first, pawn.second - 1}}); break; case RIGHT: moves.push_back({pawn, {pawn.first, pawn.second + 1}}); break; } } } int move_numb = random_range(0, moves.size() - 1); make_move2(moves[move_numb], init_pos, pawns); // debug(init_pos); // debug(end_pos); if (i % 2 != 0) { // debug(init_pos); bool equal = true; for (int k = 0; k < n; ++k) { for (int l = 0; l < m; ++l) { if (init_pos[k][l] != end_pos[k][l]) { equal = false; break; } } } if (equal) { ++end_pos_cnt; } } } cout << end_pos_cnt << '\n'; cout << simulation_cnt << '\n'; return (long double)end_pos_cnt*2/simulation_cnt; } long double solve2(vector<vector<char>> init_pos, const vector<vector<char>> &end_pos) { int n = init_pos.size(); int m = init_pos[0].size(); const int simulation_cnt = 1e5; int end_pos_cnt = 0; for (int i = 0; i < simulation_cnt; ++i) { if (i % 2 == 0) { bool equal = true; for (int j = 0; j < n; ++j) { for (int k = 0; k < m; ++k) { if (init_pos[j][k] != end_pos[j][k]) { equal = false; break; } } } if (equal) { ++end_pos_cnt; } } vector<pair<pair<int, int>, pair<int, int>>> moves; for (int j = 0; j < n; ++j) { for (int k = 0; k < m; ++k) { if (init_pos[j][k] == EMPTY) { continue; } if (j + 1 < n && init_pos[j + 1][k] == EMPTY) { moves.push_back({{j, k}, {j + 1, k}}); } if (j - 1 >= 0 && init_pos[j - 1][k] == EMPTY) { moves.push_back({{j, k}, {j - 1, k}}); } if (k + 1 < m && init_pos[j][k + 1] == EMPTY) { moves.push_back({{j, k}, {j, k + 1}}); } if (k - 1 >= 0 && init_pos[j][k - 1] == EMPTY) { moves.push_back({{j, k}, {j, k - 1}}); } } } int move_numb = random_range(0, moves.size() - 1); auto move = moves[move_numb]; swap(init_pos[move.first.first][move.first.second], init_pos[move.second.first][move.second.second]); } return (long double)end_pos_cnt * 2.0 / (long double)simulation_cnt; } int main() { int n, m; cin >> n >> m; vector<vector<char>> init_pos(n, vector<char>(m)); for (auto &row : init_pos) { for (char &entry : row) { cin >> entry; } } string empty_line; getline(cin, empty_line); vector<vector<char>> end_pos(n, vector<char>(m)); for (auto &row : end_pos) { for (char &entry : row) { cin >> entry; } } long double answer = solve2(init_pos, end_pos); cout << fixed << setprecision(15) << answer << endl; return 0; } |