Два игрока, Петя и Ваня, играют в следующую игру. Перед игроками лежат две кучи камней. Игроки ходят по очереди, первый ход делает Ваня. За один ход игрок может добавить в одну из куч два камня, пять камней, семь камней или увеличить количество камней в куче в три раза. Но нельзя повторять последний ход соперника, то есть нельзя в одну и ту же кучу добавлять столько же камней, что и противник своим последним ходом. Чтобы делать ходы, у каждого игрока есть неограниченное количество камней. Игра завершается в тот момент, когда суммарное количество камней в кучах становится не менее 201. Победителем считается игрок, сделавший последний ход. В начальный момент в первой куче было 16 камней, во второй куче – S камней, 1 S
150. Известно, что Петя выиграл своим первым ходом после неудачного хода Вани.
Назовите минимальное значение S, при котором это возможно.
Программное решение
from functools import lru_cache
@lru_cache(None)
def game(first_heap, second_heap,last_move = ""): # Функция игры.
# last_move - это переменная для хранения данных о том, в какую кучу какой ход был последний ход.
# Например, 1_+5 - это ход +5 в 1-ую кучу.
if first_heap + second_heap >= 201: # Если сумма камней в кучах стала больше 200
return 0 # Прекращаем игру
moves = [] # Список возможных ходов в партии
if last_move == "": # Если это первый ход в партии (то есть нет предыдущего хода)
# тогда мы можем использовать любой ход
moves = [game(first_heap+5,second_heap,"1_+5"),game(first_heap,second_heap+5,"2_+5"),
game(first_heap+7,second_heap,"1_+7"),game(first_heap,second_heap+7,"2_+7"),
game(first_heap+2,second_heap,"1_+2"),game(first_heap,second_heap+2,"2_+2"),
game(first_heap*3,second_heap,"1_*3"),game(first_heap,second_heap*3,"2_*3")] # Генерация всех возможных ходов
elif last_move == "1_+5": # если последний ход это +5 в 1-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap, second_heap + 5, "2_+5"),
game(first_heap + 7, second_heap, "1_+7"), game(first_heap, second_heap + 7, "2_+7"),
game(first_heap + 2, second_heap, "1_+2"), game(first_heap, second_heap + 2, "2_+2"),
game(first_heap * 3, second_heap, "1_*3"), game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "2_+5": # если последний ход это +5 в 2-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"),
game(first_heap + 7, second_heap, "1_+7"), game(first_heap, second_heap + 7, "2_+7"),
game(first_heap + 2, second_heap, "1_+2"), game(first_heap, second_heap + 2, "2_+2"),
game(first_heap * 3, second_heap, "1_*3"), game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "1_+7": # если последний ход это +7 в 1-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"), game(first_heap, second_heap + 5, "2_+5"),
game(first_heap, second_heap + 7, "2_+7"),
game(first_heap + 2, second_heap, "1_+2"), game(first_heap, second_heap + 2, "2_+2"),
game(first_heap * 3, second_heap, "1_*3"), game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "2_+7": # если последний ход это +7 в 2-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"), game(first_heap, second_heap + 5, "2_+5"),
game(first_heap + 7, second_heap, "1_+7"),
game(first_heap + 2, second_heap, "1_+2"), game(first_heap, second_heap + 2, "2_+2"),
game(first_heap * 3, second_heap, "1_*3"), game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "1_+2": # если последний ход это +2 в 1-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"), game(first_heap, second_heap + 5, "2_+5"),
game(first_heap + 7, second_heap, "1_+7"), game(first_heap, second_heap + 7, "2_+7"),
game(first_heap, second_heap + 2, "2_+2"),
game(first_heap * 3, second_heap, "1_*3"), game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "2_+2": # если последний ход это +2 в 2-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"), game(first_heap, second_heap + 5, "2_+5"),
game(first_heap + 7, second_heap, "1_+7"), game(first_heap, second_heap + 7, "2_+7"),
game(first_heap + 2, second_heap, "1_+2"),
game(first_heap * 3, second_heap, "1_*3"), game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "1_*3": # если последний ход это *3 в 1-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"), game(first_heap, second_heap + 5, "2_+5"),
game(first_heap + 7, second_heap, "1_+7"), game(first_heap, second_heap + 7, "2_+7"),
game(first_heap + 2, second_heap, "1_+2"), game(first_heap, second_heap + 2, "2_+2"),
game(first_heap, second_heap * 3, "2_*3")]
elif last_move == "2_*3": # если последний ход это *3 в 2-ую кучу
# тогда мы можем использовать любой ход, кроме этого
moves = [game(first_heap + 5, second_heap, "1_+5"), game(first_heap, second_heap + 5, "2_+5"),
game(first_heap + 7, second_heap, "1_+7"), game(first_heap, second_heap + 7, "2_+7"),
game(first_heap + 2, second_heap, "1_+2"), game(first_heap, second_heap + 2, "2_+2"),
game(first_heap * 3, second_heap, "1_*3")]
petya_win = [i for i in moves if i <= 0]
if petya_win: # если в данной позиции есть выигрыш Пети
return -max(petya_win) + 1
else: # если в данной позиции выигрыш Вани
return -max(moves)
for i in range(1,151):
# если в данной позиции после неудачного хода Вани возможен выигрыш Пети
if game(16*3,i,"1_*3") == 1 or game(16,i*3,"2_*3") == 1:
print(i)
break