#include <bits/stdc++.h> bool ok(const int n, const int m, std::vector<std::vector<int>>& v) { for (auto& i : v) for (auto& j : i) if (j != -1) j = 0; v[0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i == 0 && j == 0) continue; if (v[i][j] == -1) continue; if (i == 0) { if (v[i][j - 1] != -1) v[i][j] = v[i][j - 1]; } else if (j == 0) { if (v[i - 1][j] != -1) v[i][j] = v[i - 1][j]; } else { if (v[i - 1][j] != -1) v[i][j] += v[i - 1][j]; if (v[i][j - 1] != -1) v[i][j] += v[i][j - 1]; } } } // for (auto& i : v) { // for (auto& j : i) // std::cout << std::setw(3) << j; // std::cout << std::endl; // } return v[n - 1][m - 1] != 0; } int main() { int n, m, k; int r, c, z; int x = 0; std::cin >> n >> m >> k; std::vector<std::vector<int>> v(n, std::vector<int>(m, 0)); v[0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i == 0 && j == 0) continue; if (i == 0) v[i][j] = v[i][j - 1]; else if (j == 0) v[i][j] = v[i - 1][j]; else v[i][j] = v[i - 1][j] + v[i][j - 1]; } } for (int i = 0; i < k; ++i) { std::cin >> r >> c >> z; r = (r ^ x) % n; c = (c ^ x) % m; v[r][c] = -1; if (!ok(n, m, v)) { std::cout << "TAK" << std::endl; x ^= z; v[r][c] = 0; } else { std::cout << "NIE" << std::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 | #include <bits/stdc++.h> bool ok(const int n, const int m, std::vector<std::vector<int>>& v) { for (auto& i : v) for (auto& j : i) if (j != -1) j = 0; v[0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i == 0 && j == 0) continue; if (v[i][j] == -1) continue; if (i == 0) { if (v[i][j - 1] != -1) v[i][j] = v[i][j - 1]; } else if (j == 0) { if (v[i - 1][j] != -1) v[i][j] = v[i - 1][j]; } else { if (v[i - 1][j] != -1) v[i][j] += v[i - 1][j]; if (v[i][j - 1] != -1) v[i][j] += v[i][j - 1]; } } } // for (auto& i : v) { // for (auto& j : i) // std::cout << std::setw(3) << j; // std::cout << std::endl; // } return v[n - 1][m - 1] != 0; } int main() { int n, m, k; int r, c, z; int x = 0; std::cin >> n >> m >> k; std::vector<std::vector<int>> v(n, std::vector<int>(m, 0)); v[0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i == 0 && j == 0) continue; if (i == 0) v[i][j] = v[i][j - 1]; else if (j == 0) v[i][j] = v[i - 1][j]; else v[i][j] = v[i - 1][j] + v[i][j - 1]; } } for (int i = 0; i < k; ++i) { std::cin >> r >> c >> z; r = (r ^ x) % n; c = (c ^ x) % m; v[r][c] = -1; if (!ok(n, m, v)) { std::cout << "TAK" << std::endl; x ^= z; v[r][c] = 0; } else { std::cout << "NIE" << std::endl; } } return 0; } |