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

using namespace std;

int n,m;
vector<vector<bool>> a;
vector<vector<bool>> path;

bool isPath(int x, int y) 
{
	if(x == n-1 && y == m-1) return 1;
	
	if(a[x][y])
	{
		if(path[x][y]) return 0;
		path[x][y] = 1;
		if(x+1 < n) if(isPath(x+1,y)) return 1;
		if(y+1 < m) if(isPath(x,y+1)) return 1;
	}
	return 0;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int k;
	cin >> n >> m >> k;
	
	a.resize(n,vector<bool>(m,1));
	path.resize(n, vector<bool>(m,0));
	
	int r, c, z;
	int x = 0;
	while(k--)
	{
		cin >> r >> c >> z;
		int l = (r^x)%n;
		int r = (c^x)%m;
		a[l][r] = 0;
		if(!isPath(0,0)) cout << "TAK\n", a[l][r] = 1, x = x^z;
		else cout << "NIE\n";
		fill(path.begin(), path.end(), vector<bool>(m, 0));
	}
	
	return 0;
}
/*
3 5 7
0 1 123
1 0 0
4 8 0
2 2 16
2 3 0
18 19 17
3 0 0
*/