#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Unhappy Python program to write a program, using recursion, that solves mazes.


    Can someone pls. help. I need to make the belwo program work. I need to write a program, using recursion, that solves mazes.

    I am not able to run this python program. Instead of values it returns "solution not found".Pls. tell what is wrong with this program.

    Below is the program:

    #this function reads a maze file, filename, and creates a maze, m.

    # Please declare "m" as a list before calling the function and then pass it in.

    m=[] # This declares the maze as an empty list

    def readMaze(m, filename):

    mazeFile = open(filename, "r")

    lines = mazeFile.readlines()

    for line in lines:

    line = line.strip()

    row = [c for c in line]

    m.append(row)







    readMaze(m, "sampleMaze.txt") # This reads the maze into the list

    print m # This prints the maze, showing it with the usual notation as a "list of lists"

    start = []







    def findStart(m):

    start = []

    mazeSolution = []

    for row in xrange(len(m)):

    for col in xrange(len(m[row])):

    if m[row][col]== "S":

    start = [row,col]

    mazeSolution = start

    return mazeSolution




    def solveMaze(m, mazeSolution):



    mazeSolution = findStart(m)

    row = mazeSolution[0]

    col = mazeSolution[1]

    return( solveMaze2(m, mazeSolution, row, col))













    ###############

    import copy

    def countMaze(m, row, col):




    memoMap = copy.deepcopy(m)

    return solveMaze2(m,memoMap,row,col)








    def solveMaze2(m,memoMap,row,col):

    memoMap = copy.deepcopy(m)

    if ( (row<0) or (col<0) ):

    return 0

    if ( (row >= len(m[0])) or (col >= len(m[1]))):

    return 0




    if (memoMap[row][col] == "F"):

    return 0




    if (m[row][col] != "P"):

    return 0




    memoMap[row][col] = "W"




    if(solveMaze2(m,memoMap,row-1,col)):

    mazeSolution.append(row-1,col)

    return True

    elif(solveMaze2(m,memoMap,row,col-1)):

    mazeSolution.append(row,col-1)

    return True

    elif(solveMaze2(m,memoMap,row,col+1)):

    mazeSolution.append(row,col+1)

    return True

    elif(solveMaze2(m,memoMap,row+1,col)):

    mazeSolution.append(row+1,col)

    return True




    else:

    return False




    ##################


    def mazeSolver2():

    mazeFile = "sampleMaze.txt"

    readMaze(m, mazeFile)

    mazeSolution = []

    # solution = findStart(m)

    print mazeSolution




    if (solveMaze(m, mazeSolution)== True):

    print "True"

    print mazeSolution

    else:

    print "No solution found."




    mazeSolver2()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Your program clearly has an indentation error. Please post your code according the instructions at my signature.
    Code:
    $ python p.py
      File "p.py", line 9
        mazeFile = open(filename, "r")
               ^
    IndentationError: expected an indented block
    Also, please post the input file.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo