#include<cstdio> #include<vector> #include<stack> using namespace std; #define UNKNOWN 0 #define BLOCKED 1 #define MAYBE_BLOCKED 2 #define CANNOT_BE_BLOCKED 3 int main() { int n, m, forts_num; scanf("%d %d %d", &n, &m, &forts_num); vector<vector<int> > island; for (int i = 0; i < n; i++) { vector<int> row; for (int j = 0; j < m; j++) { row.push_back(UNKNOWN); } island.push_back(row); } island[0][0] = CANNOT_BE_BLOCKED; island[n-1][m-1] = CANNOT_BE_BLOCKED; long long x = 0; for (int i = 0; i < forts_num; i++) { long long r_i, c_i, z_i; scanf("%lld %lld %lld", &r_i, &c_i, &z_i); int x_i = (r_i^x) % n; int y_i = (c_i^x) % m; int target_state = UNKNOWN; if (island[x_i][y_i] == BLOCKED || island[x_i][y_i] == CANNOT_BE_BLOCKED) { target_state = island[x_i][y_i]; } else { island[x_i][y_i] = MAYBE_BLOCKED; bool found_unblockable = false; stack<int> x_stack; stack<int> y_stack; if (x_i > 0) { x_stack.push(x_i - 1); y_stack.push(y_i); } if (y_i > 0) { x_stack.push(x_i); y_stack.push(y_i - 1); } while (!x_stack.empty() && !y_stack.empty()) { int curr_x = x_stack.top(); x_stack.pop(); int curr_y = y_stack.top(); y_stack.pop(); if (island[curr_x][curr_y] != BLOCKED && island[curr_x][curr_y] != MAYBE_BLOCKED) { bool left_blocked = curr_x >= n - 1 || island[curr_x + 1][curr_y] == BLOCKED || island[curr_x + 1][curr_y] == MAYBE_BLOCKED; bool right_blocked = curr_y >= m - 1 || island[curr_x][curr_y + 1] == BLOCKED || island[curr_x][curr_y + 1] == MAYBE_BLOCKED; if (left_blocked && right_blocked) { if (island[curr_x][curr_y] == CANNOT_BE_BLOCKED) { found_unblockable = true; break; } island[curr_x][curr_y] = MAYBE_BLOCKED; if (curr_x > 0) { x_stack.push(curr_x - 1); y_stack.push(curr_y); } if (curr_y > 0) { x_stack.push(curr_x); y_stack.push(curr_y - 1); } } } } if (!found_unblockable) { stack<int> x_d_stack; stack<int> y_d_stack; if (x_i < n - 1) { x_d_stack.push(x_i + 1); y_d_stack.push(y_i); } if (y_i < m - 1) { x_d_stack.push(x_i); y_d_stack.push(y_i + 1); } while (!x_d_stack.empty() && !y_d_stack.empty()) { int curr_x = x_d_stack.top(); x_d_stack.pop(); int curr_y = y_d_stack.top(); y_d_stack.pop(); if (island[curr_x][curr_y] != BLOCKED && island[curr_x][curr_y] != MAYBE_BLOCKED) { bool left_blocked = curr_x == 0 || island[curr_x - 1][curr_y] == BLOCKED || island[curr_x - 1][curr_y] == MAYBE_BLOCKED; bool right_blocked = curr_y == 0 || island[curr_x][curr_y - 1] == BLOCKED || island[curr_x][curr_y - 1] == MAYBE_BLOCKED; if (left_blocked && right_blocked) { if (island[curr_x][curr_y] == CANNOT_BE_BLOCKED) { found_unblockable = true; break; } island[curr_x][curr_y] = MAYBE_BLOCKED; if (curr_x < n - 1) { x_d_stack.push(curr_x + 1); y_d_stack.push(curr_y); } if (curr_y < m - 1) { x_d_stack.push(curr_x); y_d_stack.push(curr_y + 1); } } } } } int mark_state; if (found_unblockable) { target_state = CANNOT_BE_BLOCKED; mark_state = UNKNOWN; } else { target_state = BLOCKED; mark_state = BLOCKED; } stack<int> x_mark_stack; stack<int> y_mark_stack; if (x_i > 0) { x_mark_stack.push(x_i - 1); y_mark_stack.push(y_i); } if (y_i > 0) { x_mark_stack.push(x_i); y_mark_stack.push(y_i - 1); } while (!x_mark_stack.empty() && !y_mark_stack.empty()) { int curr_x = x_mark_stack.top(); x_mark_stack.pop(); int curr_y = y_mark_stack.top(); y_mark_stack.pop(); if (island[curr_x][curr_y] == MAYBE_BLOCKED) { island[curr_x][curr_y] = mark_state; if (curr_x > 0) { x_mark_stack.push(curr_x - 1); y_mark_stack.push(curr_y); } if (curr_y > 0) { x_mark_stack.push(curr_x); y_mark_stack.push(curr_y - 1); } } } if (x_i < n - 1) { x_mark_stack.push(x_i + 1); y_mark_stack.push(y_i); } if (y_i < m - 1) { x_mark_stack.push(x_i); y_mark_stack.push(y_i + 1); } while (!x_mark_stack.empty() && !y_mark_stack.empty()) { int curr_x = x_mark_stack.top(); x_mark_stack.pop(); int curr_y = y_mark_stack.top(); y_mark_stack.pop(); if (island[curr_x][curr_y] == MAYBE_BLOCKED) { island[curr_x][curr_y] = mark_state; if (curr_x < n - 1) { x_stack.push(curr_x + 1); y_stack.push(curr_y); } if (curr_y < m - 1) { x_stack.push(curr_x); y_stack.push(curr_y + 1); } } } } island[x_i][y_i] = target_state; if (target_state == CANNOT_BE_BLOCKED) { x ^= z_i; printf("TAK\n"); } else { printf("NIE\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 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 | #include<cstdio> #include<vector> #include<stack> using namespace std; #define UNKNOWN 0 #define BLOCKED 1 #define MAYBE_BLOCKED 2 #define CANNOT_BE_BLOCKED 3 int main() { int n, m, forts_num; scanf("%d %d %d", &n, &m, &forts_num); vector<vector<int> > island; for (int i = 0; i < n; i++) { vector<int> row; for (int j = 0; j < m; j++) { row.push_back(UNKNOWN); } island.push_back(row); } island[0][0] = CANNOT_BE_BLOCKED; island[n-1][m-1] = CANNOT_BE_BLOCKED; long long x = 0; for (int i = 0; i < forts_num; i++) { long long r_i, c_i, z_i; scanf("%lld %lld %lld", &r_i, &c_i, &z_i); int x_i = (r_i^x) % n; int y_i = (c_i^x) % m; int target_state = UNKNOWN; if (island[x_i][y_i] == BLOCKED || island[x_i][y_i] == CANNOT_BE_BLOCKED) { target_state = island[x_i][y_i]; } else { island[x_i][y_i] = MAYBE_BLOCKED; bool found_unblockable = false; stack<int> x_stack; stack<int> y_stack; if (x_i > 0) { x_stack.push(x_i - 1); y_stack.push(y_i); } if (y_i > 0) { x_stack.push(x_i); y_stack.push(y_i - 1); } while (!x_stack.empty() && !y_stack.empty()) { int curr_x = x_stack.top(); x_stack.pop(); int curr_y = y_stack.top(); y_stack.pop(); if (island[curr_x][curr_y] != BLOCKED && island[curr_x][curr_y] != MAYBE_BLOCKED) { bool left_blocked = curr_x >= n - 1 || island[curr_x + 1][curr_y] == BLOCKED || island[curr_x + 1][curr_y] == MAYBE_BLOCKED; bool right_blocked = curr_y >= m - 1 || island[curr_x][curr_y + 1] == BLOCKED || island[curr_x][curr_y + 1] == MAYBE_BLOCKED; if (left_blocked && right_blocked) { if (island[curr_x][curr_y] == CANNOT_BE_BLOCKED) { found_unblockable = true; break; } island[curr_x][curr_y] = MAYBE_BLOCKED; if (curr_x > 0) { x_stack.push(curr_x - 1); y_stack.push(curr_y); } if (curr_y > 0) { x_stack.push(curr_x); y_stack.push(curr_y - 1); } } } } if (!found_unblockable) { stack<int> x_d_stack; stack<int> y_d_stack; if (x_i < n - 1) { x_d_stack.push(x_i + 1); y_d_stack.push(y_i); } if (y_i < m - 1) { x_d_stack.push(x_i); y_d_stack.push(y_i + 1); } while (!x_d_stack.empty() && !y_d_stack.empty()) { int curr_x = x_d_stack.top(); x_d_stack.pop(); int curr_y = y_d_stack.top(); y_d_stack.pop(); if (island[curr_x][curr_y] != BLOCKED && island[curr_x][curr_y] != MAYBE_BLOCKED) { bool left_blocked = curr_x == 0 || island[curr_x - 1][curr_y] == BLOCKED || island[curr_x - 1][curr_y] == MAYBE_BLOCKED; bool right_blocked = curr_y == 0 || island[curr_x][curr_y - 1] == BLOCKED || island[curr_x][curr_y - 1] == MAYBE_BLOCKED; if (left_blocked && right_blocked) { if (island[curr_x][curr_y] == CANNOT_BE_BLOCKED) { found_unblockable = true; break; } island[curr_x][curr_y] = MAYBE_BLOCKED; if (curr_x < n - 1) { x_d_stack.push(curr_x + 1); y_d_stack.push(curr_y); } if (curr_y < m - 1) { x_d_stack.push(curr_x); y_d_stack.push(curr_y + 1); } } } } } int mark_state; if (found_unblockable) { target_state = CANNOT_BE_BLOCKED; mark_state = UNKNOWN; } else { target_state = BLOCKED; mark_state = BLOCKED; } stack<int> x_mark_stack; stack<int> y_mark_stack; if (x_i > 0) { x_mark_stack.push(x_i - 1); y_mark_stack.push(y_i); } if (y_i > 0) { x_mark_stack.push(x_i); y_mark_stack.push(y_i - 1); } while (!x_mark_stack.empty() && !y_mark_stack.empty()) { int curr_x = x_mark_stack.top(); x_mark_stack.pop(); int curr_y = y_mark_stack.top(); y_mark_stack.pop(); if (island[curr_x][curr_y] == MAYBE_BLOCKED) { island[curr_x][curr_y] = mark_state; if (curr_x > 0) { x_mark_stack.push(curr_x - 1); y_mark_stack.push(curr_y); } if (curr_y > 0) { x_mark_stack.push(curr_x); y_mark_stack.push(curr_y - 1); } } } if (x_i < n - 1) { x_mark_stack.push(x_i + 1); y_mark_stack.push(y_i); } if (y_i < m - 1) { x_mark_stack.push(x_i); y_mark_stack.push(y_i + 1); } while (!x_mark_stack.empty() && !y_mark_stack.empty()) { int curr_x = x_mark_stack.top(); x_mark_stack.pop(); int curr_y = y_mark_stack.top(); y_mark_stack.pop(); if (island[curr_x][curr_y] == MAYBE_BLOCKED) { island[curr_x][curr_y] = mark_state; if (curr_x < n - 1) { x_stack.push(curr_x + 1); y_stack.push(curr_y); } if (curr_y < m - 1) { x_stack.push(curr_x); y_stack.push(curr_y + 1); } } } } island[x_i][y_i] = target_state; if (target_state == CANNOT_BE_BLOCKED) { x ^= z_i; printf("TAK\n"); } else { printf("NIE\n"); } } } |