Hi, I'm having some issues with a maze solver that I'm coding for an assignment. Here's what I have so far:

def main():
    maze = open(raw_input("Please enter the filename of the maze:"),'r')
    with maze as maze_file:
        maze = [list(line.strip()) for line in maze_file]

    for i in maze: #print blank maze
        for a in i:
            print a,
    print maze[y][x]
    ##mazeSolve(maze,x,y) #call solver funcion

def findStart(maze,x,y):
    for x and y in range(0,len(maze)):
        if maze[y][x] == "S":
        print maze[y][x]
    return x,y

I do have the solver function coded already, but do not want to post it. My problem lies in the findStart function. I know how to find a single character in a string, but I have absolutely no clue how to do it with a list of lists. Other than that, the only thing I'm wondering is how to remove the spaces that are printed in between characters when I open my maze file.

Here's a sample maze to see the problem with. I threw it in a pastebin because the editor on here messed up the spacing.

7 Years
Discussion Span
Last Post by hackabusa

I have absolutely no clue how to do it with a list of lists

test_matrix = [ ["A", "b", "c"], \
                ["R", "S", "s"], \
                ["X", "Y", "Z"] ]
for sublist in test_matrix:
   print sublist
   for ch in sublist:
      if ch == "S":
         print "\nS found in", sublist, "\n"

So once i've found the start, how would i store it in a way that I could index it as such:


to easily work through the maze by adding and subtracting from x and y


using woooee's example, this becomes:

test_matrix = [ ["A", "b", "c"], \
                ["R", "S", "s"], \
                ["X", "Y", "Z"] ]

for x in range( len( test_matrix ) ):
   for y in range( len( test_matrix[ x ] ) ):
      if test_matrix[ x ][ y ] == "S":
         start = ( x, y )

# now you can use start here as a tuple.
print "\nS found in", start, "\n"
# output
# -> S found in (1 1)

hope this helps :)

Edited by masterofpuppets: Forgot code tags AGAIN


Thanks for the help, but I managed to solve it using the enumerate function.

for rowIndex,row in enumerate(maze): #find y coord

        for colIndex,col in enumerate(row): #find x coord

            if col == "S":

                y = rowIndex #set y coord

                x = colIndex #set x coord
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.