Recent Posts
None | 7 sec ago
None | 10 sec ago
C | 24 sec ago
Erlang | 31 sec ago
Ruby | 34 sec ago
None | 57 sec ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Ruby | 1 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:26:39 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 = itertools.product(range(self.height), range(self.width))
  21.         for pos, val in zip(positions, image.getdata()):
  22.             r, g, b = val
  23.             s = sum(val)
  24.             if s < 50:
  25.                 v = 'wall'
  26.             elif s > 600:
  27.                 v = 'floor'
  28.             elif r > 100:
  29.                 v = 'end'
  30.             elif g > 100:
  31.                 v = 'start'
  32.             else:
  33.                 v = None
  34.             grid[(pos[1], pos[0])] = v
  35.         self.grid = grid
  36.        
  37.         start_pos = (k for k, v in grid.items() if v == 'start').next()
  38.         assert start_pos
  39.  
  40.         self.pixels = image.load()
  41.         self.bfs(start_pos)
  42.         image.save(out_path)
  43.  
  44.  
  45.     def bfs(self, pos):
  46.         grid   = self.grid
  47.         pixels = self.pixels
  48.         parent = {}
  49.  
  50.         # Push pos onto the queue, so we can start searching from there.
  51.         queue = deque([pos])
  52.         parent[pos] = None
  53.  
  54.         while queue:
  55.             pos = queue.popleft()
  56.            
  57.             if grid[pos] == 'end':
  58.                 break
  59.  
  60.             for p in self.neighbours(pos):
  61.                 if grid[p] == 'wall' or p in parent:
  62.                     continue
  63.  
  64.                 parent[p] = pos
  65.                 queue.append(p)
  66.  
  67.         while pos:
  68.             pixels[pos] = (0, 255, 0)
  69.             pos = parent[pos]
  70.  
  71.  
  72.     def neighbours(self, pos):
  73.         for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1):
  74.             x = pos[0] + dx
  75.             y = pos[1] + dy
  76.  
  77.             if 0 <= x < self.width and 0 <= y < self.height:
  78.                 yield (x, y)
  79.  
  80.  
  81. if __name__ == '__main__':
  82.     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: