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

int n, m, k;
bool grid[5005][5005];
bool dp[5005][5005];

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m >> k;
	int x = 0;
	while (k--){
		int r, c, z;
		cin >> r >> c >> z;
		r ^= x;
		c ^= x;
		r %= n;
		c %= m;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j)
				dp[i][j] = false;

		grid[r][c] = true;

		dp[0][0] = true;
		for (int i = 0; i < n; ++i)
			for (int j = 0; j < m; ++j){
				if (grid[i][j])
					continue;
				if (i > 0 && dp[i-1][j] == true)
					dp[i][j] = true;
				if (j > 0 && dp[i][j-1] == true)
					dp[i][j] = true;
			}
		/*for (int i = 0; i < n; ++i){
			for (int j = 0; j < m; ++j)
				cout << dp[i][j] << " ";
			cout << "\n";
		}*/
		if (!dp[n-1][m-1]){
			cout << "TAK\n";
			grid[r][c] = false;
			x ^= z;
		}
		else
			cout << "NIE\n";
		
	}

}