Python Challenge[24]

[Level 24]

Python Challenge[24]_第1张图片

Title: from top to bottom

困惑了很久,这真是迷宫,from top to bottom。但找出的路径图并没有提供有效信息,需要把像素中的R值用二进制写入文件。注意写入的姿势,前两位数据是PK

某大神使用BFS算法寻路。

from PIL import Image
img = Image.open('maze.png')
dire = [(0,1),(0,-1),(1,0),(-1,0)]
entrance, exit = (639,0), (1,640)
white = (255,255,255,255)
queue = [exit]
next_p = {}

while queue:
  pos = queue.pop(0)
  if pos==entrance:
    break
  for d in dire:
    temp=(pos[0]+d[0],pos[1]+d[1])
    if temp not in next_p and 0<=temp[0]

按顺序取出数据,再按正确的姿势写入文件:

path = []
while pos!=exit:
  path.append(img.getpixel(pos)[0])
  pos = next_p[pos]
open('maze.zip','wb').write(bytes(path[1::2]))

打开 maze.zip 里的 maze.jpg,涂有lake,[Level 25]

Python Challenge Wiki

你可能感兴趣的:(Python Challenge[24])