#include <cstdio> #include <cassert> #include <cstring> #include <cstdint> #include <algorithm> #include <string> #include <cstdlib> #include <ctime> #include <vector> using namespace std; const int max_size = 10; char t[max_size][max_size]; int n, m; int k; unsigned long long move_done; unsigned long long final_pos_done; unsigned long long final_pos; unsigned long long cur_pos; int g_mv_cnt; bool in_final_pos() { // return (p.x == final_pos.x) && (p.y == final_pos.y); return (cur_pos == final_pos); } class pion { public: int nr; int x; int y; bool mv1; // x-1 bool mv2; // x+1 bool mv3; // y-1 bool mv4; // y+1 int mv_cnt; pion(int _nr, int _x, int _y) { nr = _nr; x = _x; y = _y; mv_cnt = 0; } void print(); void set_moves(); void do_move(int nr); }; void update_cur_pos(const pion *p) { cur_pos += (8*(p->x-1)+p->y-1) << p->nr*8; } vector<pion> pi; void pion::print() { printf("x=%d, y=%d, (%d, %d, %d, %d), %d\n", x, y, mv1 ? 1 : 0, mv2 ? 1 : 0, mv3 ? 1 : 0, mv4 ? 1 : 0, mv_cnt); } bool is_free(int x, int y) { for(int i = 0; i < pi.size(); ++i) { if (pi[i].x == x && pi[i].y == y) { return false; } } return true; } void pion::set_moves() { g_mv_cnt -= mv_cnt; mv1 = (x > 0 && is_free(x-1, y)); mv2 = (x < n-1 && is_free(x+1,y)); mv3 = (y > 0 && is_free(x, y-1)); mv4 = (y < m-1 && is_free(x,y+1)); mv_cnt = (mv1 ? 1 : 0) + (mv2 ? 1 : 0) + (mv3 ? 1 : 0) + (mv4 ? 1 : 0); g_mv_cnt += mv_cnt; } void update_pions_moves(int nr, int x, int y) { for(int i = 0; i < pi.size(); ++i) { if (i != nr) { if (pi[i].x == x && (pi[i].y-1 == y || pi[i].y+1 == y)) pi[i].set_moves(); if (pi[i].y == y && (pi[i].x-1 == x || pi[i].x+1 == x)) pi[i].set_moves(); } } } void pion::do_move(int move_nr) { cur_pos -= (8*(x-1)+y-1) << nr*8; update_pions_moves(nr,x,y); if (mv1) { if (move_nr == 0) { --x; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); return; } --move_nr; } if (mv2) { if (move_nr == 0) { ++x; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); return; } --move_nr; } if (mv3) { if (move_nr == 0) { --y; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); return; } --move_nr; } assert(mv4); assert(move_nr == 0); ++y; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); } void calc_initial_moves() { for(int i = 0; i < pi.size(); ++i) { pi[i].set_moves(); } } void do_calc() { int rand_nr; int i; calc_initial_moves(); // pi[0].print(); while (1) { // pi[0].print(); i = 0; rand_nr = rand() % g_mv_cnt; // printf("global mv cnt = %d, rand_nr = %d\n", g_mv_cnt, rand_nr); while( pi[i].mv_cnt <= rand_nr ) { rand_nr -= pi[i].mv_cnt; ++i; } pi[i].do_move(rand_nr); ++move_done; // if (in_final_pos(p1)) { // ++final_pos_done; // } if (move_done % 10 == 0) { if (in_final_pos()) { ++final_pos_done; } } if (move_done % 10000000 == 0) { // printf("%llu/ %llu = %.15f\n", final_pos_done, move_done, 10.0 * ((long double) final_pos_done)/((long double) move_done)); printf("%Lf\n", 10.0 * ((long double) final_pos_done)/((long double) move_done)); return; } } } int main() { char s[max_size]; srand(time(NULL)); scanf("%d %d\n", &n, &m); for(int i = 0; i < n; ++i) { scanf("%s\n", s); for(int j = 0; j < m; ++j) { if (s[j] == 'O') { pi.push_back( pion(k, i, j) ); cur_pos += (8*(i-1)+j-1) << k*8; ++k; } } } k = 0; for(int i = 0; i < n; ++i) { scanf("%s\n", s); for(int j = 0; j < m; ++j) { if (s[j] == 'O') { final_pos += (8*(i-1)+j-1) << k*8 ; ++k; } } } do_calc(); 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 | #include <cstdio> #include <cassert> #include <cstring> #include <cstdint> #include <algorithm> #include <string> #include <cstdlib> #include <ctime> #include <vector> using namespace std; const int max_size = 10; char t[max_size][max_size]; int n, m; int k; unsigned long long move_done; unsigned long long final_pos_done; unsigned long long final_pos; unsigned long long cur_pos; int g_mv_cnt; bool in_final_pos() { // return (p.x == final_pos.x) && (p.y == final_pos.y); return (cur_pos == final_pos); } class pion { public: int nr; int x; int y; bool mv1; // x-1 bool mv2; // x+1 bool mv3; // y-1 bool mv4; // y+1 int mv_cnt; pion(int _nr, int _x, int _y) { nr = _nr; x = _x; y = _y; mv_cnt = 0; } void print(); void set_moves(); void do_move(int nr); }; void update_cur_pos(const pion *p) { cur_pos += (8*(p->x-1)+p->y-1) << p->nr*8; } vector<pion> pi; void pion::print() { printf("x=%d, y=%d, (%d, %d, %d, %d), %d\n", x, y, mv1 ? 1 : 0, mv2 ? 1 : 0, mv3 ? 1 : 0, mv4 ? 1 : 0, mv_cnt); } bool is_free(int x, int y) { for(int i = 0; i < pi.size(); ++i) { if (pi[i].x == x && pi[i].y == y) { return false; } } return true; } void pion::set_moves() { g_mv_cnt -= mv_cnt; mv1 = (x > 0 && is_free(x-1, y)); mv2 = (x < n-1 && is_free(x+1,y)); mv3 = (y > 0 && is_free(x, y-1)); mv4 = (y < m-1 && is_free(x,y+1)); mv_cnt = (mv1 ? 1 : 0) + (mv2 ? 1 : 0) + (mv3 ? 1 : 0) + (mv4 ? 1 : 0); g_mv_cnt += mv_cnt; } void update_pions_moves(int nr, int x, int y) { for(int i = 0; i < pi.size(); ++i) { if (i != nr) { if (pi[i].x == x && (pi[i].y-1 == y || pi[i].y+1 == y)) pi[i].set_moves(); if (pi[i].y == y && (pi[i].x-1 == x || pi[i].x+1 == x)) pi[i].set_moves(); } } } void pion::do_move(int move_nr) { cur_pos -= (8*(x-1)+y-1) << nr*8; update_pions_moves(nr,x,y); if (mv1) { if (move_nr == 0) { --x; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); return; } --move_nr; } if (mv2) { if (move_nr == 0) { ++x; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); return; } --move_nr; } if (mv3) { if (move_nr == 0) { --y; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); return; } --move_nr; } assert(mv4); assert(move_nr == 0); ++y; this->set_moves(); update_pions_moves(nr,x,y); update_cur_pos(this); } void calc_initial_moves() { for(int i = 0; i < pi.size(); ++i) { pi[i].set_moves(); } } void do_calc() { int rand_nr; int i; calc_initial_moves(); // pi[0].print(); while (1) { // pi[0].print(); i = 0; rand_nr = rand() % g_mv_cnt; // printf("global mv cnt = %d, rand_nr = %d\n", g_mv_cnt, rand_nr); while( pi[i].mv_cnt <= rand_nr ) { rand_nr -= pi[i].mv_cnt; ++i; } pi[i].do_move(rand_nr); ++move_done; // if (in_final_pos(p1)) { // ++final_pos_done; // } if (move_done % 10 == 0) { if (in_final_pos()) { ++final_pos_done; } } if (move_done % 10000000 == 0) { // printf("%llu/ %llu = %.15f\n", final_pos_done, move_done, 10.0 * ((long double) final_pos_done)/((long double) move_done)); printf("%Lf\n", 10.0 * ((long double) final_pos_done)/((long double) move_done)); return; } } } int main() { char s[max_size]; srand(time(NULL)); scanf("%d %d\n", &n, &m); for(int i = 0; i < n; ++i) { scanf("%s\n", s); for(int j = 0; j < m; ++j) { if (s[j] == 'O') { pi.push_back( pion(k, i, j) ); cur_pos += (8*(i-1)+j-1) << k*8; ++k; } } } k = 0; for(int i = 0; i < n; ++i) { scanf("%s\n", s); for(int j = 0; j < m; ++j) { if (s[j] == 'O') { final_pos += (8*(i-1)+j-1) << k*8 ; ++k; } } } do_calc(); return 0; } |