#include <iostream> #include <vector> #include <queue> #include <array> #include <set> using namespace std; void input(int & n, vector<int> & toys) { cin >> n; toys.resize(n); for(int i = 0; i < n; i++) { cin >> toys[i]; } } inline void cond_push(queue<array<int, 4>> & que, array<int, 4> & prop, set<array<int, 4>> & visited) { if(prop[1] >= 0 && prop[2] >= 0) { if(!visited.count(prop)) { que.push(prop); visited.insert(prop); } } } bool get_result(int const n, vector<int> const & toys) { int first_non_zero = -1; for(int i = 0; i < n; i++) { if(toys[i] != 0) { first_non_zero = i; break; } } queue<array<int, 4>> que; que.push({first_non_zero, toys[first_non_zero], toys[first_non_zero], 0}); set<array<int, 4>> visited; visited.insert(que.front()); array<int, 4> finish = {n-1, 0, 0, 3}; array<int, 4> finish2 = {n-1, 0, 1, 1}; array<int, 4> finish3 = {n-1, 1, 0, 2}; array<int, 4> finish4 = {n-1, 1, 1, 0}; int last_id = first_non_zero; while(!que.empty()) { array<int, 4> cur_v = que.front(); que.pop(); if(cur_v == finish || cur_v == finish2 || cur_v == finish3 || cur_v == finish4) { return true; } if(last_id < cur_v[0]) { visited = set<array<int, 4>>(); visited.insert(cur_v); last_id = cur_v[0]; } int next_id = cur_v[0] + 1; if(next_id == n) { continue; } if(cur_v[3] == 0) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 0}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]) { cond_push(que, prop, visited); } } if(cur_v[1] > 0) { prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1]+1, 1}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1]-1 < cur_v[2]) { cond_push(que, prop, visited); } } } if(cur_v[2] > 0) { prop = {next_id, toys[next_id]-cur_v[2]+1, toys[next_id]-cur_v[1], 2}; if(cur_v[2]-1 != 0 || toys[next_id] == 0) { if(cur_v[1] > cur_v[2]-1) { cond_push(que, prop, visited); } } } if(cur_v[1] > 0 && cur_v[2] > 0) { prop = {next_id, toys[next_id]-cur_v[2]+1, toys[next_id]-cur_v[1]+1, 3}; if(cur_v[2]-1 != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]) { cond_push(que, prop, visited); } } } } else if(cur_v[3] == 1) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 1}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] < cur_v[2]) { cond_push(que, prop, visited); } } if(cur_v[2] > 0) { prop = {next_id, toys[next_id]-cur_v[2]+1, toys[next_id]-cur_v[1], 3}; if(cur_v[2]-1 != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]-1) { cond_push(que, prop, visited); } } } } else if(cur_v[3] == 2) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 2}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] > cur_v[2]) { cond_push(que, prop, visited); } } if(cur_v[1] > 0) { prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1]+1, 3}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1]-1 == cur_v[2]) { cond_push(que, prop, visited); } } } } else if(cur_v[3] == 3) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 3}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]) { cond_push(que, prop, visited); } } } } return false; } void solve() { int n; vector<int> toys; input(n, toys); if(get_result(n, toys)) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { solve(); } return 0; }
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | #include <iostream> #include <vector> #include <queue> #include <array> #include <set> using namespace std; void input(int & n, vector<int> & toys) { cin >> n; toys.resize(n); for(int i = 0; i < n; i++) { cin >> toys[i]; } } inline void cond_push(queue<array<int, 4>> & que, array<int, 4> & prop, set<array<int, 4>> & visited) { if(prop[1] >= 0 && prop[2] >= 0) { if(!visited.count(prop)) { que.push(prop); visited.insert(prop); } } } bool get_result(int const n, vector<int> const & toys) { int first_non_zero = -1; for(int i = 0; i < n; i++) { if(toys[i] != 0) { first_non_zero = i; break; } } queue<array<int, 4>> que; que.push({first_non_zero, toys[first_non_zero], toys[first_non_zero], 0}); set<array<int, 4>> visited; visited.insert(que.front()); array<int, 4> finish = {n-1, 0, 0, 3}; array<int, 4> finish2 = {n-1, 0, 1, 1}; array<int, 4> finish3 = {n-1, 1, 0, 2}; array<int, 4> finish4 = {n-1, 1, 1, 0}; int last_id = first_non_zero; while(!que.empty()) { array<int, 4> cur_v = que.front(); que.pop(); if(cur_v == finish || cur_v == finish2 || cur_v == finish3 || cur_v == finish4) { return true; } if(last_id < cur_v[0]) { visited = set<array<int, 4>>(); visited.insert(cur_v); last_id = cur_v[0]; } int next_id = cur_v[0] + 1; if(next_id == n) { continue; } if(cur_v[3] == 0) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 0}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]) { cond_push(que, prop, visited); } } if(cur_v[1] > 0) { prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1]+1, 1}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1]-1 < cur_v[2]) { cond_push(que, prop, visited); } } } if(cur_v[2] > 0) { prop = {next_id, toys[next_id]-cur_v[2]+1, toys[next_id]-cur_v[1], 2}; if(cur_v[2]-1 != 0 || toys[next_id] == 0) { if(cur_v[1] > cur_v[2]-1) { cond_push(que, prop, visited); } } } if(cur_v[1] > 0 && cur_v[2] > 0) { prop = {next_id, toys[next_id]-cur_v[2]+1, toys[next_id]-cur_v[1]+1, 3}; if(cur_v[2]-1 != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]) { cond_push(que, prop, visited); } } } } else if(cur_v[3] == 1) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 1}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] < cur_v[2]) { cond_push(que, prop, visited); } } if(cur_v[2] > 0) { prop = {next_id, toys[next_id]-cur_v[2]+1, toys[next_id]-cur_v[1], 3}; if(cur_v[2]-1 != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]-1) { cond_push(que, prop, visited); } } } } else if(cur_v[3] == 2) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 2}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] > cur_v[2]) { cond_push(que, prop, visited); } } if(cur_v[1] > 0) { prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1]+1, 3}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1]-1 == cur_v[2]) { cond_push(que, prop, visited); } } } } else if(cur_v[3] == 3) { array<int, 4> prop = {next_id, toys[next_id]-cur_v[2], toys[next_id]-cur_v[1], 3}; if(cur_v[2] != 0 || toys[next_id] == 0) { if(cur_v[1] == cur_v[2]) { cond_push(que, prop, visited); } } } } return false; } void solve() { int n; vector<int> toys; input(n, toys); if(get_result(n, toys)) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { solve(); } return 0; } |