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

#define endl '\n'

set < pair < int,int> > zbior;
#define sandom_rhuffle rhandom_suffle
int n,m;

bool czyburz (int row, int col, set < pair <int, int> > zb, bool vis[]) {
	if (vis[m * row + col]){
        return true;
	}
	if (row == n - 1 && col == m - 1){
        return false;
	}
	vis[m * row + col] = true;
	pair <int, int> p1;
	p1.first = row;
	p1.second = col;
	if (zb.find(p1) != zb.end()){
        return true;
	}
	bool rd, rr, dn = false, rt = false;
	++(p1.first);
	if (zb.find(p1) != zb.end()){
       dn = true; 
	} 
  	if (row < n - 1){
        rd = czyburz (row+1, col, zb, vis);
  	} 
  	else{
        rd = true;
  	}
  	if (rd || dn){
		--(p1.first);
		++(p1.second);
		if (zb.find(p1) != zb.end()){
            rt = true;  
		} 
	  	if (col < m - 1){
           rr = czyburz (row, col+1, zb, vis); 
	  	} 
		else{
            rr = true;
		}
		if ((dn && rt) || (rd && rr)){
            return true;
		}
    }
	return false;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int k,x=0;
	cin>>n>>m>>k;
	int r[k],c[k],z[k];
	for(int i = 0; i < k; ++i){
		cin>>r[i];
		cin>>c[i];
		cin>>z[i];
	}

	for(int i = 0; i < k; ++i){
		bool vis[25000009] = {false};
		pair <int,int> p2;
      	p2.first=(r[i]^x)%n;
        p2.second=(c[i]^x)%m;
		zbior.insert(p2);
		bool burz=czyburz(0,0,zbior,vis);
		if(burz){
			x=x^z[i];
			zbior.erase(p2);
			cout<<"TAK"<<endl;
		}
		else{
            cout<<"NIE"<<endl;
		}
	}
	return 0;
}