fork download
  1. E=enumerate
  2. U=lambda j,k,J,K,b,n,m:b[j][k]==b[J][K]if(n>j>-1<k<m>K>-1<J<n)else 0
  3. def f(b):
  4. M=min(n:=len(b),m:=len(b[0]));q,r=[i for x,u in E(b)for y,_ in E(u)for i in[(x,y,0,0,x,y)]*(x+2>M<=m-y)+[(x,y,1,0,x,y)]*(x>M-2<y)],[]
  5. while q:
  6. x,y,d,c,X,Y=q.pop(0);D=1-d-d
  7. if~abs(X-x)+min(n,m):q+=(x,y,d,c+U(X+1,Y+D,X-1,Y-D,b,n,m)+U(X,Y+D,X-D,Y,b,n,m),X-1,Y+D),
  8. else:r+=(*[[x,y],[X,Y]][d],d,c),
  9. return max(r,key=lambda x:x[-1])
  10.  
  11. s1="""
  12. 4 1 3 2 4 2
  13. 1 2 4 2 1 3
  14. 4 3 2 1 4 4
  15. """
  16. s2="""
  17. 3 6
  18. 4 7
  19. 5 8
  20. 1 2
  21. 2 1
  22. """
  23. s3="""
  24. 2 7 4 10 7 8 9 5 6 4 2 4 10 2 1 7 10 7 2 4 10 10 8 7
  25. 6 5 6 2 2 3 6 1 6 9 7 2 10 3 4 7 8 8 3 7 1 8 4 2
  26. 3 3 7 6 10 1 7 9 10 10 2 6 4 7 5 6 9 1 1 5 7 6 2 7
  27. 7 10 3 9 8 10 9 3 6 1 6 10 3 8 9 6 3 6 2 10 1 2 8 1
  28. 7 7 8 1 1 6 4 8 10 3 10 4 9 3 1 9 5 9 10 4 6 7 10 4
  29. 1 10 9 7 7 10 3 3 7 8 2 2 4 2 4 7 1 7 7 1 9 9 8 7
  30. 5 9 5 3 8 6 5 7 6 7 2 7 9 9 7 10 8 8 7 3 5 9 9 10
  31. 9 3 8 2 9 2 1 3 6 3 8 5 7 10 10 9 1 1 10 2 5 1 6 9
  32. 8 7 6 2 3 2 9 9 9 7 9 5 8 3 8 2 2 5 2 2 10 10 3 5
  33. 7 1 1 2 3 2 10 1 2 10 3 3 2 1 4 2 5 6 10 9 6 5 3 8
  34. 8 9 5 2 1 4 10 6 8 6 9 10 10 8 1 6 10 6 4 8 7 9 3 5
  35. 8 1 5 7 1 8 7 5 8 6 4 5 10 1 6 1 4 4 10 7 6 3 3 6
  36. """
  37. def to_board(s):
  38. return [[*map(int,i.split())]for i in filter(None, s.split('\n'))]
  39.  
  40. print(f(to_board(s1)))
  41. print(f(to_board(s2)))
  42. print(f(to_board(s3)))
Success #stdin #stdout 0.13s 14076KB
stdin
Standard input is empty
stdout
(2, 1, 0, 2)
(2, 0, 1, 1)
(11, 7, 0, 5)