Прошу помощи.
Задание 21 ЕГЭ.
Решение на Питон
from functools import lru_cache
def moves(h):
a,b=h
return(a+1,b),(a*2,b),(a,b+1),(a,b*2)
@lru_cache(None)
def game(h):
if sum(h)>=62:
return "w"
if any(game(m)=="w" for m in moves(h)):
return "P1"
if all(game(m)=="P1" for m in moves(h)):
return "B1"
if any(game(m)=="B1" for m in moves(h)):
return "P2"
if all(game(m)=="P1" or game(m)=="P2" for m in moves(h)):
return "B2"
for s in range(1,52):
if game((10,s)) is not None:
print(s, game((10,s)) )
Переписал на PascalABC.NET как сумел:
function moves(h:(integer,integer)):((integer,integer),
(integer,integer),
(integer,integer),
(integer,integer));
var a,b:integer;
begin
(a,b):=h;
result:=((a+1,b),(a*2,b),(a,b+1),(a,b*2))
end;
[Cache]
function game(h:(integer,integer)):string ;
begin
result := 'N';
if (h[0]+h[1])>=62 then
begin
result:='w';
exit
end;
if (game(moves(h)[0])='w') or
(game(moves(h)[1])='w') or
(game(moves(h)[2])='w') or
(game(moves(h)[3])='w') then
begin
result := 'P1';
exit
end;
if (game(moves(h)[0])='P1') and
(game(moves(h)[1])='P1') and
(game(moves(h)[2])='P1') and
(game(moves(h)[3])='P1') then
begin
result := 'B1';
exit
end;
if (game(moves(h)[0])='B1') or
(game(moves(h)[1])='B1') or
(game(moves(h)[2])='B1') or
(game(moves(h)[3])='B1') then
begin
result := 'P2';
exit
end;
if ((game(moves(h)[0])='P1') or (game(moves(h)[0])='P2')) and
((game(moves(h)[1])='P1') or (game(moves(h)[1])='P2')) and
((game(moves(h)[2])='P1') or (game(moves(h)[2])='P2')) and
((game(moves(h)[3])='P1') or (game(moves(h)[3])='P2')) then
begin
result := 'B2';
exit
end;
end;
begin
for var s:=1 to 51 do
begin
if game((10,s)) <> 'N' then writeln(s, game((10,s)))
end
end.
Работает медленнее. Как ускорить или написать решение по другому (проще)?