Python Challenge[24]

[Level 24]


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]<img.size[0] and 0<=temp[1]<img.size[1] and img.getpixel(temp)!=white:
      next_p[temp] = pos
      queue.append(temp)

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容