#include <iostream> #include <vector> #include <array> using namespace std; bool nextMove(int step, int position, vector<int> state, const vector<int>& target, int size) { if (step == 0) { return (state == target); } int i = 0; while (i < size) { if (state[i] > target[i]) return false; i++; } bool wynik = false; // Ruch w lewo (jeżeli to możliwe) if (position > 0) { state[position - 1]++; wynik |= nextMove( step - 1, position - 1, state, target, size ); state[position - 1]--; } // Ruch w prawo (jeżeli to możliwe) if (position < size-1) { state[position + 1]++; wynik |= nextMove( step - 1, position + 1, state, target, size ); state[position + 1]--; } return wynik; } bool isReachable(const vector<int>& target, int moves, int size) { bool wynik = false; vector<int> first; for (int i = 0; i < size; i++) first.push_back(0); for (int start = 0; start < size; start++) { for (int i = 0; i < size; i++) first[i] = 0; first[start] = 1; if (nextMove(moves - 1, start, first, target, size)) { wynik = true; break; } } return wynik; } int main() { int t; cin >> t; while (t-- > 0) { int n; cin >> n; vector<int> vec; int moves = 0; for (int i = 0; i < n; i++) { int a; cin >> a; moves += a; vec.push_back(a); } if (isReachable(vec, moves, n)) cout << "TAK"; else cout << "NIE"; cout << endl; } }
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 | #include <iostream> #include <vector> #include <array> using namespace std; bool nextMove(int step, int position, vector<int> state, const vector<int>& target, int size) { if (step == 0) { return (state == target); } int i = 0; while (i < size) { if (state[i] > target[i]) return false; i++; } bool wynik = false; // Ruch w lewo (jeżeli to możliwe) if (position > 0) { state[position - 1]++; wynik |= nextMove( step - 1, position - 1, state, target, size ); state[position - 1]--; } // Ruch w prawo (jeżeli to możliwe) if (position < size-1) { state[position + 1]++; wynik |= nextMove( step - 1, position + 1, state, target, size ); state[position + 1]--; } return wynik; } bool isReachable(const vector<int>& target, int moves, int size) { bool wynik = false; vector<int> first; for (int i = 0; i < size; i++) first.push_back(0); for (int start = 0; start < size; start++) { for (int i = 0; i < size; i++) first[i] = 0; first[start] = 1; if (nextMove(moves - 1, start, first, target, size)) { wynik = true; break; } } return wynik; } int main() { int t; cin >> t; while (t-- > 0) { int n; cin >> n; vector<int> vec; int moves = 0; for (int i = 0; i < n; i++) { int a; cin >> a; moves += a; vec.push_back(a); } if (isReachable(vec, moves, n)) cout << "TAK"; else cout << "NIE"; cout << endl; } } |