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
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
string c;
struct Para{
	int s;
	int t;
	Para(int s_i, int t_i){
		this->s = s_i;
		this->t = t_i;
	}
};

bool dfs(int s, int d, int n, int m, bool**t, bool** visited){
	bool tmp = false;
	if(visited[s][d]){
		return false;
	}
	if(s == (n-1) && d == (m-1)){
		tmp = true;
	}else{
		if((s+1)< n && t[s+1][d] == false){
			tmp = tmp || dfs(s+1,d,n, m, t, visited);
		}
		if((d+1)< m && t[s][d+1] == false){
			tmp = tmp || dfs(s,d+1,n, m,t, visited);
		}
	}
	return tmp;
}

int main(){
	int n = 0;
	int m = 0;
	int k = 0;
	int r = 0;
	int c = 0;
	int z = 0;
	int x = 0;
	int a = 0;
	int b = 0;
	
	
	cin >> n;
	cin >> m;
	cin >> k;
	bool **t;
	t = new bool *[n];
	for(int i = 0; i <n; i++)
	    t[i] = new bool[m];
	    
	bool **pomoc;
	pomoc = new bool *[n];
	for(int i = 0; i <n; i++)
	    pomoc[i] = new bool[m];
	    
	for(int i=0; i<n; ++i){
		for(int j=0; j<m; ++j){
			t[i][j]=false;
		}
	}
	
	for(int v=0; v<k; ++v){
		cin >> r;
		cin >> c;
		cin >> z;
		a = (r ^ x) % n;
		b = (c ^ x) % m;
		t[a][b]=true;
		for(int i=0; i<n; ++i){
			for(int j=0; j<m; ++j){
				pomoc[i][j]=false;
			}
		}
		bool isPath = dfs(0,0,n,m,t,pomoc);
		if(isPath){
			cout << "NIE" << "\n";
		}else{
			cout << "TAK" << "\n";
			t[a][b]=false;
			x = x ^ z;
		}
	}
	
	return 0;
}