Advertisement
Pohuyumer

19-21 нисходящая

Mar 13th, 2022
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #рекурсия для задачи с камнями
  2. from icecream import ic
  3. from functools import lru_cache
  4.  
  5.  
  6. @lru_cache(None)
  7. def f(x, y):
  8.     # ic(x, y)
  9.     if x + y <= 20:
  10.         # ic('<= 20')
  11.         return 0
  12.         # ic('вернули 0')
  13.     if x == 0:
  14.         # ic()
  15.         # ic('x == 0')
  16.         moves = [f(x, y - 1), f(x, int(y / 2) + y % 2)]
  17.         return 0
  18.     if x == 1:
  19.         # ic()
  20.         # ic('x == 1')
  21.         moves = [f(x - 1, y), f(x, y - 1), f(x, int(y / 2) + y % 2)]
  22.         return 0
  23.     if y == 0:
  24.         # ic()
  25.         # ic('y == 0')
  26.         moves = [f(x - 1, y), f(int(x / 2) + x % 2, y)]
  27.         return 0
  28.     if y == 1:
  29.         # ic()
  30.         # ic('x == 1')
  31.         moves = [f(x - 1, y), f(x, y - 1), f(int(x / 2) + x % 2, y)]
  32.         return 0
  33.     else:
  34.         moves = [f(x - 1, y), f(x, y - 1), f(int(x / 2) + x % 2, y), f(x, int(y / 2) + y % 2)]
  35.     lose = [i for i in moves if i <= 0]
  36.     # ic(moves)
  37.     # ic(lose)
  38.     if lose:
  39.         result = -max(lose) + 1
  40.     else:
  41.         result = -max(moves)
  42.     return result
  43.  
  44.  
  45. ic.configureOutput(prefix='')
  46. for s in range(11, 50):
  47.     if f(10, s) == 2:
  48.         ic(s, f(10, s))
  49.         ic('=====================')
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement