fork download
  1. E=enumerate
  2. M=[(1,0),(-1,0),(0,-1),(0,1),(1,1),(-1,1),(-1,-1),(1,-1)]
  3. def f(L):
  4. d,t={(x,y):v for x,r in E(L)for y,v in E(r)},[]
  5. for i in d.values():
  6. q={j for j in d if(j in t)+d[j]==i}
  7. while q:
  8. v=[*q][0];Q,s={v},{v}
  9. while Q:x,y=e=[*Q][0];Q-={e};l={V for X,Y in M if(V:=(x+X,y+Y))in d and(d[v]>=d[V])*~-(V in s)};Q={*Q,*l};s={*s,*l}
  10. q-=s;t+=[*s]*all(d.get((x+X,y+Y),-1)>d[(x,y)]or(x+X,y+Y)in[*t,*s]for x,y in s for X,Y in M)
  11. for x,y in t:L[x][y]='*'
  12. return'\n'.join(''.join(map(str,i))for i in L)
  13.  
  14. s1 = """
  15. 8888888888
  16. 5664303498
  17. 6485322898
  18. 5675373666
  19. 7875555787
  20. """
  21. s2 = """
  22. 888
  23. 838
  24. 388
  25. """
  26. s3 = """
  27. 77777
  28. 75657
  29. 75757
  30. 77677
  31. 77477
  32. """
  33. s4 = """
  34. 599999
  35. 933339
  36. 936639
  37. 935539
  38. 932109
  39. 999999
  40. """
  41. s5 = """
  42. 88888888
  43. 84482288
  44. 84452233
  45. 84482288
  46. 88888888
  47. """
  48. def to_board(s):
  49. return [[*map(int,i)] for i in filter(None, s.split('\n'))]
  50.  
  51. print(f(to_board(s1)))
  52. print('-'*40)
  53. print(f(to_board(s2)))
  54. print('-'*40)
  55. print(f(to_board(s3)))
  56. print('-'*40)
  57. print(f(to_board(s4)))
  58. print('-'*40)
  59. print(f(to_board(s5)))
Success #stdin #stdout 0.13s 14044KB
stdin
Standard input is empty
stdout
8888888888
566*****98
6*85***898
5675*7*666
7875555787
----------------------------------------
888
838
388
----------------------------------------
77777
7*6*7
7*7*7
77677
77477
----------------------------------------
599999
9****9
9*66*9
9*55*9
9****9
999999
----------------------------------------
88888888
8**8**88
8**5**33
8**8**88
88888888