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
#include <cstdio>
#include <list>
using namespace std;

int n,m,k,x;
int r,c,z;
bool** t;
list< pair<int, int> > history;

bool isClosing(int i, int j) {
	if (i == 0 && j==0) return true;
	if (i < 0 || j < 0 || t[i][j] == true) return false;

	history.push_back(make_pair(i,j));
	t[i][j] = true;
	bool result = false;
	
	if (i > 0 && t[i-1][j+1]) result = result || isClosing(i-1, j);
	if (j > 0 && t[i+1][j-1]) result = result || isClosing(i, j-1);
	
	return result;
}

void printT() {
	for(int i=0;i<=n;i++) {
		for( int j=0 ; j<=m; j++) {
			if (t[i][j]) printf("x");
			else printf(" ");
		} 
		printf("\n");
	}
	for(list< pair<int, int> >::iterator it = history.begin(); it!= history.end(); it++) {
		printf("(%d,%d) ", it -> first, it -> second);
	}
	printf("\n\n");
}

int main() {
	scanf("%d %d %d", &n, &m, &k);
	
	t = new bool*[n+1];
	for(int i=0;i<=n;i++) {
		t[i] = new bool[m+1];
	}
	x = 0;
	
	for (int i=0; i<=n;i++) t[i][m] = true;
	for (int j=0; j<=m;j++) t[n][j] = true;
		
	for (int i=0; i<k; i++) {
		scanf("%d %d %d", &r, &c, &z);
		
		r = (r^x) % n;
		c = (c^x) % m;
		bool shouldDestroy = isClosing(r, c);
		if (shouldDestroy) {
			for(list< pair<int, int> >::iterator it = history.begin(); it!= history.end(); it++) {
				t[it -> first][it -> second] = false;
			}
			x = x ^ z;
			printf("TAK\n");
		} else {
			printf("NIE\n");
		}
		// printT();
		history.clear();
	}
	
}