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

int **M;
//int M[9][9];
int R, C;


int dfs(int r, int c, int m) {
    if (r==R-1 && c==C-1) return m+1;
    M[r][c] = m;
    if (r+1 < R && M[r+1][c] != m && M[r+1][c] != -1) {
        if (dfs(r+1, c, m)) {
            M[r][c] = m+1;
            return m+1;
        }
    }
    if (c+1 < C && M[r][c+1] != m && M[r][c+1] != -1) {
        if (dfs(r, c+1, m)) {
            M[r][c] = m+1;
            return m+1;
        }
    }
    return 0;
}


int main()
{
    int k, x = 0;
    scanf("%d %d %d", &R, &C, &k);
    int last = 0;

    M = (int **)malloc(R*sizeof(int *));
    for (int i=0; i<R; i++) {
        M[i] = (int *)malloc(C*sizeof(int));
        memset(M[i], 0, C*sizeof(int));
    }

    // memset(M, 0, sizeof(M));
    int m = 1;
    for (int i=1; i<=k; i++) {
        int r,c,z;
        scanf("%d %d %d", &r, &c, &z);
        r = (r ^ x) % R;
        c = (c ^ x) % C;
        int old = M[r][c], d;
        M[r][c] = -1;
        if (old != last) {
            d = last;
        } else {
            d = dfs(0, 0, m); m+=2;
        }
        if (d) { // jest przejscie
            last = d;
            // dodano twierdze
            printf("NIE\n");
        } else {
            // zburzyc twierdze
            M[r][c] = old;
            last = dfs(0, 0, m); m+=2;
            x ^= z;
            printf("TAK\n");
        }
    }

    return 0;
}