def rec_solve(tab, i): if tab[i] < 0: return False if all(x == 0 for x in tab): return True if i == 0: tab[0] -= 1 return rec_solve(tab, 1) elif i == n-1: tab[n-1] -= 1 return rec_solve(tab, n-2) else: if tab[i-1] == 0: if tab[i+1] == 0: if tab.count(1) == 1 and all(x in {0, 1} for x in tab) and tab[i]==1: return True return False else: tab[i] -= 1 return rec_solve(tab,i+1) elif tab[i+1] == 0: tab[i] -= 1 return rec_solve(tab,i-1) tab[i] -= 1 return rec_solve( [x for x in tab],i-1) or rec_solve( [x for x in tab],i+1) t = int(input()) for _ in range(t): solved = 'NIE' n = int(input()) tab = [int(x) for x in input().split(" ")] if n== 1: print('TAK' if tab[0] == 1 else 'NIE') continue for i in range(n): tab_c = [x for x in tab] if rec_solve(tab_c, i): solved = 'TAK' break print(solved)
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 | def rec_solve(tab, i): if tab[i] < 0: return False if all(x == 0 for x in tab): return True if i == 0: tab[0] -= 1 return rec_solve(tab, 1) elif i == n-1: tab[n-1] -= 1 return rec_solve(tab, n-2) else: if tab[i-1] == 0: if tab[i+1] == 0: if tab.count(1) == 1 and all(x in {0, 1} for x in tab) and tab[i]==1: return True return False else: tab[i] -= 1 return rec_solve(tab,i+1) elif tab[i+1] == 0: tab[i] -= 1 return rec_solve(tab,i-1) tab[i] -= 1 return rec_solve( [x for x in tab],i-1) or rec_solve( [x for x in tab],i+1) t = int(input()) for _ in range(t): solved = 'NIE' n = int(input()) tab = [int(x) for x in input().split(" ")] if n== 1: print('TAK' if tab[0] == 1 else 'NIE') continue for i in range(n): tab_c = [x for x in tab] if rec_solve(tab_c, i): solved = 'TAK' break print(solved) |