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
#include <bits/stdc++.h>

using namespace std;

int n, m, k, X = 0;

vector<vector<int> > v;
vector<bool> visited;

bool dfs(const int& x)
{
    if (x == n * m - 1)
        return true;
    for (int& i : v[x])
        if (!visited[i] && dfs(i))
            return true;
    return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m >> k;
    v.resize(n * m);
    visited.assign(n * m, false);

    for (int i = 0; i < n * m; i++)
    {
        if (i % n != n - 1) v[i].push_back(i + 1);
        if (i < n * m - n) v[i].push_back(i + n);
    }

    for (int i = 0; i < k; i++)
    {
        int x, y, c; cin >> x >> y >> c;
        x = (x ^ X) % n;
        y = (y ^ X) % m;
        visited[x + n * y] = true;
        if (!dfs(0))
        {
            visited[x + n * y] = false;
            X ^= c;
            cout << "TAK\n";
        }
        else
            cout << "NIE\n";
    }
}