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

using namespace std;

bool find_path(bool **island, int N, int M)
{
  queue<pair<int, int>> Q;
  Q.push({N - 1, M - 1});
  while (!Q.empty())
  {
    pair<int, int> current = Q.front();
    Q.pop();
    if (island[current.first][current.second] == false)
      continue;

    if (current.first == 0 && current.second == 0)
      return true;
    if (current.first > 0)
      Q.push({current.first - 1, current.second});
    if (current.second > 0)
      Q.push({current.first, current.second - 1});
  }
  return false;
}

void solve()
{
  int M, N, K;
  cin >> N >> M >> K;
  bool **island = new bool *[N];
  for (int i = 0; i < N; i++)
    island[i] = new bool[M];

  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      island[i][j] = true;

  int X = 0;
  for (int _ = 0; _ < K; _++)
  {
    int R, C, Z;
    cin >> R >> C >> Z;
    int i, j;
    i = (R ^ X) % N;
    j = (C ^ X) % M;
    island[i][j] = false;
    if (!find_path(island, N, M))
    {
      island[i][j] = true;
      X = X ^ Z;
      cout << "TAK\n";
    }
    else
      cout << "NIE\n";
  }

  for (int i = 0; i < N; i++)
    delete[] island[i];
  delete[] island;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}