Advertisement
C3EQUALZ

Теория игр, где 1 куча, 19/20/21 задание ЕГЭ

Nov 15th, 2021 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.91 KB | None | 0 0
  1. Pascal:
  2. //Решение 19 задания нового формата
  3. function f(S, turn : integer) : boolean;
  4. begin
  5.   if (s>=40) and (turn <> 2) then
  6.     result := False
  7.   else if (s<40) and (turn = 2) then
  8.     result := False
  9.   else if (s>=40) and (turn = 2) then
  10.     result := True
  11.   else if (turn mod 2 = 0) then //Для решения задачи с неудачным ходом достаточно не проверять ходы на чётность
  12.                                 //И вызывать новых ход всегда с "or". Но для решения 21 задания полностью расписывать данную функцию.
  13.     result := f(S*2, turn+1) and f(S+1, turn+1) and f(S+4, turn+1)
  14.   else if (turn mod 2 <> 0) then
  15.     result := f(S*2, turn+1) or f(S+1, turn+1) or f(S+4, turn+1);
  16. end;
  17.  
  18. //Решение 20 задания
  19. function g(S, turn : integer) : boolean;
  20. begin
  21.   if (s>=40) and (turn <> 3) then
  22.     result := False
  23.   else if (s<40) and (turn = 3) then
  24.     result := False
  25.   else if (s>=40) and (turn = 3) then
  26.     result := True
  27.   else if (turn mod 2 <> 0) then
  28.     result := g(S*2, turn+1) and g(S+1, turn+1) and g(S+4, turn+1)
  29.   else if (turn mod 2 = 0) then
  30.     result := g(S*2, turn+1) or g(S+1, turn+1) or g(S+4, turn+1);
  31. end;
  32. //Решение 21 задания (лишь с той оговоркой, что нужно отсеять варианты условием not f(S,0))
  33. function h(S, turn : integer) : boolean;
  34. begin
  35.   if (s>=40) and ((turn = 4) or (turn = 2)) then
  36.     result := True
  37.   else if (s>=40) and (turn <> 4) then
  38.     result := False
  39.   else if (s<40) and (turn = 4) then
  40.     result := False
  41.   else if (turn mod 2 = 0) then
  42.     result := h(S*2, turn+1) and h(S+1, turn+1) and h(S+4, turn+1)
  43.   else if (turn mod 2 <> 0) then
  44.     result := h(S*2, turn+1) or h(S+1, turn+1) or h(S+4, turn+1);
  45. end;
  46.  
  47.  
  48. begin
  49.     for var s := 0 to 40 do
  50.     if f(S,0) then
  51.       print('19:', S);
  52.  
  53.     for var s := 0 to 40 do
  54.     if g(S,0) then
  55.       print('20:', S);
  56.  
  57.     for var s := 0 to 40 do
  58.     if h(S,0) and not f(S,0) then
  59.       print('21:', S);
  60. end.
  61.  
  62. ----------------------------------------------------------------------------------------------------------
  63. Pascal LINQ:
  64. ###
  65. function f(S,turn: int):boolean:=(S>=94) and (turn=2) ? True : (S<94) and (turn=2) ? False : (S>=94) and (turn<>2) ? False : turn.Divs(2) ? f(S*2,turn+1) and f(S+1,turn+1) : f(S*2,turn+1) or f(S+1,turn+1);
  66. function g(S,turn:int):boolean:=(S>=94) and (turn <> 3) ? False : (S<94) and (turn = 3) ? False : (S>=94) and (turn=3) ? True : turn.divs(2) ? g(S*2,turn+1) or g(S+1,turn+1) : g(S+1, turn+1) and g(S*2,turn+1);
  67. function h(S,turn:int):boolean:=(S>=94) and ((turn=2) or (turn=4)) ? True : (S<94) and (turn=4) ? False : (S>=94) and (turn<>4) ? False: turn.Divs(2) ? h(S*2,turn+1) and h(S+1,turn+1) : h(S*2,turn+1) or h(S+1,turn+1);
  68. begin
  69.   for var i:=1 to 93 do
  70.     if f(i,0) then begin print('19:',i);
  71.   end;
  72. end;
  73. begin
  74.   for var i:=1 to 93 do
  75.     if g(i,0) then begin print('20:',i);
  76. end;
  77. end;
  78. begin
  79.   for var i:=1 to 93 do
  80.     if h(i,0) and not f(i,0) then begin print('21:',i);
  81. end;
  82. end;
  83. ----------------------------------------------------------------------------------------------------------------
  84. Pascal, но через множества
  85. ##
  86. var w1 := new HashSet<integer>;
  87. for var i := 0 to 45 do
  88.   if ((i+1)>46) or ((i+2)>46) or((i3)>46) then w1.Add(i);
  89.  
  90. var L1 := new HashSet<integer>;
  91. for var i := 0 to 45 do
  92.   if ((i+1) in w1) and ((i+2) in w1) and ((i*3)in w1) then L1.Add(i);
  93.  
  94. var w2 := new HashSet<integer>;
  95. for var i := 0 to 45 do
  96.   if ((i+1) in L1) or ((i+2) in L1) or ((i*3) in L1) then w2.Add(i);
  97.  
  98. var L2 := new HashSet<integer>;
  99. for var i := 0 to 45 do
  100.   if (((i+1) in w1) or ((i+1) in w2)) and (((i+2) in w1) or ((i+2) in w2)) and (((i*3) in w1) or ((i*3) in w2)) then L2.Add(i);
  101. print(w1,l1,w2,l2);
  102. ----------------------------------------------------------------------------------------------------------------------------------
  103. Pyhton:
  104.  
  105. def f(stones : int, current_turn : int , limit_for_person : int) -> int:
  106.     #stones - кол-во камней с самого начала, current_turn - текущий ход,
  107.     #limit_for_person - ходы, за которые должен победить нужный нам игрок
  108.     if stones >= 34: return current_turn % 2 == limit_for_person % 2 #уникальная проверка для каждого задания.
  109.     #Например, если у нас игра должна завершиться за 2 хода (победа Вани), то и текущий ход должен иметь такую же четность
  110.     if current_turn == limit_for_person: return 0 #Если мы не набрали нужное кол-во камней, то нам такой расклад не по нраву.
  111.     #Все последующие ходы
  112.     next_turns = [f(stones + 1, current_turn + 1, limit_for_person),
  113.                   f(stones + 2, current_turn + 1, limit_for_person),
  114.                   f(stones + 3, current_turn + 1, limit_for_person),
  115.                   f(stones * 2, current_turn + 1, limit_for_person)]
  116.     # Если это ход нужного нам игрока, то достаточно победы в одном из вариантов, иначе победа должна быть во всех вариантах
  117.     return any(next_turns) if (current_turn + 1) % 2 == limit_for_person % 2 else all(next_turns)
  118.  
  119. for stones in range(1, 34):
  120.     for limit_for_person in range(1,5):
  121.         if f(stones, 0, limit_for_person) == True:
  122.             if limit_for_person == 2: #любое число ходов, которое указано в условии
  123.                 print(f'Старт. кол-во камней {stones} для победы опр. персоны за {limit_for_person} хода/ходов игры')
  124.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement