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
//
//  main.cpp
//  oiw
//
//  Created by Mikołaj Korulczyk on 15/12/2019.
//  Copyright © 2019 Mikołaj Korulczyk. All rights reserved.
//

#include <bits/stdc++.h>
#include <map>

#define x first
#define y second

using namespace std;

int mvs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
map<pair<int, int>, bool> field;
map<pair<int, int>, bool> visited;

int n, m, k;
int r, c, z;

int enemy;

int X, Y;

long long bfs() {
    queue<pair<int, int> > q1;
    queue<pair<int, int> > q2;
    q2.push(make_pair(0, 0));
    
    int dst = 0;
    while(q2.size()) {
        swap(q1, q2);
        while(q1.size()) {
            pair<int, int> crds = q1.front();
            q1.pop();
            
            if(visited[make_pair(crds.x, crds.y)] || field[make_pair(crds.x, crds.y)]) {
                continue;
            }
            
            //cout << crds.x << " " << crds.y << endl;
            
            if(crds.x == n-1 && crds.y == m-1) {
                return dst;
            }
            
            visited[make_pair(crds.x, crds.y)] = true;
            
            for(int i = 0; i < 4; i++) {
                int local_x = crds.x+mvs[i][0];
                int local_y = crds.y+mvs[i][1];
                
                if(local_x >= 0 && local_x < n && local_y >= 0 && local_y < m && !visited[make_pair(local_x, local_y)]) {
                    q2.push(make_pair(local_x, local_y));
                    //cout << "local " << local_x << " " << local_y << endl;
                }
                
            }
            
            //cout << "------" << endl;
        }
        dst++;
    }
        
    return 1e9;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n >> m >> k;
    
    enemy = 0;
    
    
    while(k--) {
        cin >> r >> c >> z;
        
        X = (r^enemy)%n;
        Y = (c^enemy)%m;
        
        field[make_pair(X, Y)] = true;
        visited.clear();
        //cout << visited.size() << endl;
        //cout << X << " " << Y << endl;
        
        
        
        if(bfs() != n+m-2) {
            cout << "TAK\n";
            field[make_pair(X, Y)] = false;
            //cout << enemy << " " << z << endl;
            enemy ^= z;
        } else {
            cout << "NIE\n";
        }
        
    }
    
    
    return 0;
}