В игре, описанной
Найдите такое значение S, при котором у Вани есть стратегия, позволяющая ему выиграть вторым ходом при любой игре Пети, но у Вани нет стратегии, которая позволяла бы ему гарантированно выиграть первым ходом.
Приведём другое решение на языке Python.
def f(x, y, h):
if (h == 2 or h == 4) and x + y >= 41:
return 1
if h == 4 and x + y < 41:
return 0
if (h == 1 or h == 3) and x + y >= 41:
return 0
else:
if h % 2 != 0:
if x > y:
return f(x + 1, y, h + 1) or f(x + 2, y, h + 1) or f(x + 3, y, h + 1) or f(x, y * 2, h + 1)
elif x < y:
return f(x, y + 1, h + 1) or f(x, y + 2, h + 1) or f(x, y + 3, h + 1) or f(x * 2, y, h + 1)
elif x == y:
return f(x + 1, y, h + 1) or f(x + 2, y, h + 1) or f(x + 3, y, h + 1) or f(x, y + 1, h + 1) or f(x, y + 2, h + 1) or f(x, y + 3, h + 1)
else:
if x > y:
return f(x + 1, y, h + 1) and f(x + 2, y, h + 1) and f(x + 3, y, h + 1) and f(x, y * 2, h + 1)
elif x < y:
return f(x, y + 1, h + 1) and f(x, y + 2, h + 1) and f(x, y + 3, h + 1) and f(x * 2, y, h + 1)
elif x == y:
return f(x + 1, y, h + 1) and f(x + 2, y, h + 1) and f(x + 3, y, h + 1) and f(x, y + 1, h + 1) and f(x, y + 2, h + 1) and f(x, y + 3, h + 1)
a = set()
for x in range(1, 23):
if f(17, x, 0) == 1:
print(x)
break
Ответ: 9.
Приведём решение Маргариты Фалько на языке Python.
def g(s1, s2, p, end):
if s1+s2 > 40: return p in end
if p > max(end): return False
moves = []
if s1 > s2:
moves = [g(s1+i, s2, p+1, end) for i in range(1,4)] + [g(s1, s2*2, p+1, end)]
if s1 < s2:
moves = [g(s1, s2+i, p+1, end) for i in range(1,4)] + [g(s1*2, s2, p+1, end)]
if s1 == s2:
moves = [g(s1, s2+i, p+1, end) for i in range(1,4)] + [g(s1+i, s2, p+1, end) for i in range(1,4)]
return any(moves) if ((p+1) % 2) == (end[0] % 2) else all(moves)
print('Задание 19:', min([s1+s2 for s1 in range(1, 100) for s2 in range(1, 100) if g(s1, s2, 0, [1])]))
print('Задание 20:', [s2 for s2 in range(1, 36) if g(5, s2, 0, [3])])
print('Задание 21:', [s2 for s2 in range(1, 24) if g(17, s2, 0, [2, 4]) and not g(17, s2, 0, [2])])

