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
#include<bits/stdc++.h>
using namespace std;
long long n,m,k,r,c,z,x;
struct obj
{
    int x;
    int y;
    long long d;
};
vector<obj>v;
vector<int>test;
vector<vector<int> >used;
queue<obj>Q;
bool burz;

bool bfs()
{
    while(Q.empty()==0)
    {
        Q.pop();
    }
    for(int i=v.size()-1;i>=0;i--)
    {
        if(used[v[i].x][v[i].y]==1)
        {
            used[v[i].x][v[i].y]=0;
        }
        v.pop_back();
    }
    obj objekt;
    objekt.x=0;
    objekt.y=0;
    objekt.d=1;
    Q.push(objekt);
    long long maxu=1;
    while(Q.empty()==0)
    {
        obj V=Q.front();
        Q.pop();
        if(used[V.x][V.y]!=0)
        {
            continue;
        }
        maxu=max(maxu,V.d);
        if(V.x==n-1 && V.y==m-1)
        {
            if(maxu==n+m-1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        if(maxu>=n+m)
        {
            return false;
        }
        if(V.x+1<n)
        {
            if(used[V.x+1][V.y]==0)
            {
                obj obi;
                obi.x=V.x+1;
                obi.y=V.y;
                obi.d=V.d+1;
                Q.push(obi);
            }
        }
        if(V.y+1<m)
        {
            if(used[V.x][V.y+1]==0)
            {
                obj obi;
                obi.x=V.x;
                obi.y=V.y+1;
                obi.d=V.d+1;
                Q.push(obi);
            }
        }
        used[V.x][V.y]=1;
        v.push_back(V);
    }
    if(maxu<n+m-1)
    {
        return false;
    }
}

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

    cin>>n>>m>>k;
    test.assign(m+5,0);
    used.assign(n+5, test);
    burz=0;
    for(int i=0;i<k;i++)
    {
        cin>>r>>c>>z;
        used[(r^x)%n][(c^x)%m]=2;
        bool sc=bfs();
        if(sc==false)
        {
            used[(r^x)%n][(c^x)%m]=0;
            x=x^z;
            cout << "TAK" << endl;
        }
        else
        {
            cout << "NIE" << endl;
        }
    }
}