Recent Posts
Java | 23 sec ago
None | 25 sec ago
None | 29 sec ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
C | 1 min ago
C# | 2 min ago
C# | 2 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By casted on the 4th of Nov 2009 09:39:07 PM Download | Raw | Embed | Report
  1. #!/usr/bin/env python
  2. # Keegan Carruthers-Smith 2009
  3.  
  4. import Image
  5. import itertools
  6. import sys
  7. from collections import deque
  8.  
  9. sys.setrecursionlimit(sys.maxint)
  10.  
  11. class MazeSolve(object):
  12.     def __init__(self, in_path, out_path):
  13.         image = Image.open(in_path)
  14.  
  15.         self.width  = image.size[0]
  16.         self.height = image.size[1]
  17.  
  18.         # Transform image into a grid
  19.         grid = {}
  20.         positions = ((x, y) for y in range(self.height)
  21.                             for x in range(self.width))
  22.         for pos, val in zip(positions, image.getdata()):
  23.             r, g, b = val
  24.             s = sum(val)
  25.             if s < 50:
  26.                 v = 'wall'
  27.             elif s > 600:
  28.                 v = 'floor'
  29.             elif r > 100:
  30.                 v = 'end'
  31.             elif g > 100:
  32.                 v = 'start'
  33.             else:
  34.                 v = None
  35.             grid[pos] = v
  36.         self.grid = grid
  37.  
  38.         start_pos = (k for k, v in grid.items() if v == 'start').next()
  39.         assert start_pos
  40.  
  41.         self.pixels = image.load()
  42.         self.bfs(start_pos)
  43.         image.save(out_path)
  44.  
  45.  
  46.     def bfs(self, pos):
  47.         grid   = self.grid
  48.         pixels = self.pixels
  49.         parent = {}
  50.  
  51.         # Push pos onto the queue, so we can start searching from there.
  52.         queue = deque([pos])
  53.         parent[pos] = None
  54.  
  55.         while queue:
  56.             pos = queue.popleft()
  57.  
  58.             if grid[pos] == 'end':
  59.                 break
  60.  
  61.             for p in self.neighbours(pos):
  62.                 if grid[p] == 'wall' or p in parent:
  63.                     continue
  64.  
  65.                 parent[p] = pos
  66.                 queue.append(p)
  67.  
  68.         while pos:
  69.             pixels[pos] = (0, 255, 0)
  70.             pos = parent[pos]
  71.  
  72.  
  73.     def neighbours(self, pos):
  74.         for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1):
  75.             x = pos[0] + dx
  76.             y = pos[1] + dy
  77.  
  78.             if 0 <= x < self.width and 0 <= y < self.height:
  79.                 yield (x, y)
  80.  
  81.  
  82. if __name__ == '__main__':
  83.     MazeSolve('maze4.png', 'maze4_solve.png')
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: