4

Hey, I'm trying to make a graph using dictionaries in Python. I'm using a txt file containing a maze (b for walls a for paths) and I'm trying to make a dictionary that lists all the possible moves to take in a maze (simple steps, not full paths). Any ideas on where I should start? I never worked with dictionaries.

Thank you so much for you help, that got me off on a great start. Just one more question, I'm starting at one valid house and checking all possible paths. after that ill have to move to another house and check the paths on that. How can i make sure i dont get an infinite loop or recheck a house ive already checked?

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
Daymor
  • 95
  • 2
  • 7

2 Answers2

4

Assuming that your maze looks like a grid, a position in the maze could be represented as a tuple (row,col). When you construct your dictionary, create an entry for every position in the maze, the initial value is an empty list. At every valid position (r,c) in the maze, figure out if you can get to (r-1,c), (r,c-1), (r+1,c), and (r,c+1). If you can, then add that tuple to the list. So, let's say that I can get to (r-1,c) and (r,c+1) from (r,c), the entry in the dictionary would look like

  maze_dict[(r,c)] = [(r-1,c), (r,c+1)]

To create an empty dictionary, you'd use:

maze_dict = {}

You should also take a look at the dictionaries section of the python tutorial

seggy
  • 434
  • 2
  • 7
  • Quick questions about dictonaries, can i get a list of the possible ways and add to the dictonary? And i cant "append" the value of a key? – Daymor Jan 18 '11 at 00:04
  • If you get the list of possible ways, then you can iteratively add those to the dictionary. In dictionaries, you can't modify the value of a key because it would break the hashing. – seggy Jan 18 '11 at 00:07
  • Daymor: you just write a key/value pair to the dictionary. You can put as many of those in it as you like. – Paul McMillan Jan 18 '11 at 00:10
  • Ive written the code on how to get the possible ways of movement of one house. My question is, how am i going to check All of them without repeating? – Daymor Jan 18 '11 at 00:58
  • Check all of them for what? Offhand, you could associate a flag with each of the moves. If it's set, then you've seen the position before, if not, then it's unique. – seggy Jan 18 '11 at 05:08
1

Thank you so much for you help, that got me off on a great start. Just one more question, I'm starting at one valid house and checking all possible paths. after that ill have to move to another house and check the paths on that. How can i make sure i dont get an infinite loop or recheck a house ive already checked?

Create a "House" class, with its grid coordinates:

class House(object):
    def __init__(self, pos):
        self.pos = pos # the coordinates (position) on the grid, a tuple
        self.paths = [] # Empty array to hold paths

Create some houses:

houses = [House((1,3)), House((3,3)), House((4,3))] # a list of houses

Now, go through each house, and caculate its path(s)

paths = {}
paths[(1,3)] = [(2,3), (4,3) ... ] # possible paths to the point (1,3)

for i in houses:
    try:
       i.paths = paths[(i.pos)]
    except KeyError:
       print "I don't know how to get to ", i.pos

Stepping through the list ensures that you check each house only once. Now you can find out the houses that are unreachable:

for i in houses:
   if not i.paths:
      print "I did not find a way to reach the house at ",i.pos
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284