Задача к ЕГЭ по информатике на тему «Теория игр» №15

Для игры, описанной в задании 19, укажите количество значений 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)
count = 0
for i in range(1,151):
    # если в данной позиции возможен выигрыш Вани вторым ходом
    if game(16,i) == 2:
        count += 1
print(count)

Ответ: 34
Оцените статью
Я решу все!