fork download
  1. M=[((1,0),1,3),((0,1),0,2),((-1,0),1,3),((0,-1),0,2)]
  2. def A(c):
  3. q=[(c[0],i,1,[c[0]])for i,*_ in M]+[(c[0],[(i,2),(M[j][0],1)],0,[c[0]])for i,*J in M for j in J]+[(c[0],[(i,1),(M[j][0],2)],0,[c[0]])for i,*J in M for j in J]
  4. while q:
  5. (x,y),m,o,P=q.pop(0)
  6. if o:
  7. X,Y=m;T=(x+X,y+Y)
  8. if 8>x+X>=0==(T in P)<=y+Y<8:
  9. if T in c[1:3]:yield(1,T)
  10. elif T in c[-2:]:q+=([*{*c[-2:]}-{T}][0],m,o,P+[T]),
  11. else:yield(0,T);q+=(T,m,o,P+[T]),
  12. else:
  13. [(X,Y),C],*I=m;T=(x+X,y+Y)
  14. if 8>x+X>=0==(T in P)<=y+Y<8:
  15. if C==1and[]==I:yield(T in c[1:3],T)
  16. elif~-(T in c[-2:])or C==1:q+=([T,[*{*c[-2:]}-{T}][0]][T in c[-2:]],(C!=1)*[[(X,Y),C-1]]+I,0,P+[T]),
  17. def f(d):
  18. S,*c=[(8-int(b),ord(a)-97)for a,b in d];q,s=[S],[S]
  19. while q:
  20. S=q.pop(0)
  21. for x,y in A([S]+c):
  22. q+=y,
  23. if x<1:
  24. k=[[],[]]
  25. for X,Y in A([y]+c):k[X]+=Y,
  26. if{*k[1]}=={*([1]+c)[1:3]}:return chr(97+y[1])+str(8-y[0])
  27.  
  28. print(f(["b2", "d7", "f6", "b5", "d2"]))
  29. print(f(["c5", "g3", "h4", "f7", "g4"]))
  30. print(f(["f4", "b8", "d3", "f6", "a4"]))
  31. print(f(["h1", "a1", "a3", "c1", "f1"]))
  32. print(f(["c2", "a8", "h8", "d2", "d6"]))
  33. print(f(["d2", "c6", "g2", "b2", "g7"]))
  34. print(f(["d3", "f6", "h6", "d4", "e4"]))
  35. print(f(["c4", "f7", "h5", "a2", "f5"]))
  36. print(f(["h5", "a7", "e2", "a4", "g6"]))
  37. print(f(["h6", "d4", "g3", "a1", "a8"]))
Success #stdin #stdout 0.13s 14104KB
stdin
Standard input is empty
stdout
b6
f5
b4
b1
d8
c2
g4
f5
g3
g4