#include <iostream> #include <algorithm> #include <vector> #include <stack> #include <string> using namespace std; #define MAX 1000000 #define DB if(0) #define PR if(0) #define RND if (0) string in[8], out[8]; double markov[8][8], old[8][8], errComp[8][8]; int counts[8][8]; double maxErr = 1e-17; int n, m; void copy() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { old[i][j] = markov[i][j]; } } } void copyErr() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { errComp[i][j] = markov[i][j]; } } } void print() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { printf("%.2f ", markov[i][j]); } printf("\n"); } } void setCounts() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { for (int k=-1; k<=1; k++) { for (int l=-1; l<=1; l++) { if (k + l != 0 && k * l == 0 && i + k >= 0 && j + l >= 0 && i + k < n && j + l < m) { counts[i][j]++; } } } } } PR for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { printf("%d ", counts[i][j]); } printf("\n"); } } bool checkErr() { double error = 0; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { PR printf("err: %.2f, max: %.2f\n", markov[i][j] - errComp[i][j], error); error = max(error, abs(markov[i][j] - errComp[i][j])); } } PR printf("err: %.9f vs %.9f\n", error, maxErr); return error < maxErr; } void update() { PR printf("\n----------\n"); PR print(); copy(); for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { markov[i][j] = 0; for (int k=-1; k<=1; k++) { for (int l=-1; l<=1; l++) { if (k + l != 0 && k * l == 0 && i + k >= 0 && j + l >= 0 && i + k < n && j + l < m) { // printf("markov[%d][%d] += [%d][%d] (%.2f / %d = %.2f)\n", i, j, i+k, j+l, old[i+k][j+l], counts[i+k][j+l], old[i+k][j+l] / counts[i+k][j+l]); markov[i][j] += old[i+k][j+l] / counts[i+k][j+l]; } } } // markov[i][j] *= (1 - old[i][j]); // Case when pawn was on the position } } } int main() { cin >> n >> m; for (int i=0; i<n; i++) cin >> in[i]; for (int i=0; i<n; i++) cin >> out[i]; setCounts(); for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { markov[i][j] = in[i][j] == 'O'; } } int round = 0; while (true) { copyErr(); update(); update(); if (checkErr()) { PR printf("DONE"); break; } round++; if (round > MAX) break; } // print(); double result = 1; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { if (out[i][j] == 'O') result *= markov[i][j]; } } printf("%.17f", result); }
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 | #include <iostream> #include <algorithm> #include <vector> #include <stack> #include <string> using namespace std; #define MAX 1000000 #define DB if(0) #define PR if(0) #define RND if (0) string in[8], out[8]; double markov[8][8], old[8][8], errComp[8][8]; int counts[8][8]; double maxErr = 1e-17; int n, m; void copy() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { old[i][j] = markov[i][j]; } } } void copyErr() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { errComp[i][j] = markov[i][j]; } } } void print() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { printf("%.2f ", markov[i][j]); } printf("\n"); } } void setCounts() { for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { for (int k=-1; k<=1; k++) { for (int l=-1; l<=1; l++) { if (k + l != 0 && k * l == 0 && i + k >= 0 && j + l >= 0 && i + k < n && j + l < m) { counts[i][j]++; } } } } } PR for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { printf("%d ", counts[i][j]); } printf("\n"); } } bool checkErr() { double error = 0; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { PR printf("err: %.2f, max: %.2f\n", markov[i][j] - errComp[i][j], error); error = max(error, abs(markov[i][j] - errComp[i][j])); } } PR printf("err: %.9f vs %.9f\n", error, maxErr); return error < maxErr; } void update() { PR printf("\n----------\n"); PR print(); copy(); for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { markov[i][j] = 0; for (int k=-1; k<=1; k++) { for (int l=-1; l<=1; l++) { if (k + l != 0 && k * l == 0 && i + k >= 0 && j + l >= 0 && i + k < n && j + l < m) { // printf("markov[%d][%d] += [%d][%d] (%.2f / %d = %.2f)\n", i, j, i+k, j+l, old[i+k][j+l], counts[i+k][j+l], old[i+k][j+l] / counts[i+k][j+l]); markov[i][j] += old[i+k][j+l] / counts[i+k][j+l]; } } } // markov[i][j] *= (1 - old[i][j]); // Case when pawn was on the position } } } int main() { cin >> n >> m; for (int i=0; i<n; i++) cin >> in[i]; for (int i=0; i<n; i++) cin >> out[i]; setCounts(); for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { markov[i][j] = in[i][j] == 'O'; } } int round = 0; while (true) { copyErr(); update(); update(); if (checkErr()) { PR printf("DONE"); break; } round++; if (round > MAX) break; } // print(); double result = 1; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { if (out[i][j] == 'O') result *= markov[i][j]; } } printf("%.17f", result); } |