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
#include <iostream>
#include <vector>

using namespace std;

int n, m, k;

bool Spr(vector< vector<bool> > p, int x, int y)
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            if (!p[i][j] || (i == 0 && j == 0))
                continue;
            if (!((i != 0 && p[i - 1][j]) || (j != 0 && p[i][j - 1])))
                p[i][j] = false;
        }
    }
    return p[n - 1][m - 1];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> k;
    int x = 0;
    vector< vector<bool> > p(n, vector<bool>(m, true));
    for (int i = 0; i < k; ++i)
    {
        int a, b, c;
        cin >> a >> b >> c;
        a = (a ^ x) % n;
        b = (b ^ x) % m;
        p[a][b] = false;
        if (Spr(p, a, b))
            cout << "NIE" << endl;
        else
        {
            cout << "TAK" << endl;
            x ^= c;
            p[a][b] = true;
        }
    }
    return 0;
}