I am working on a maze solution problem. After the code can find the Goal, I could not let the python print out a list of solution. But It's required for homework.

Can somebody help me? I just learned python for 3 weeks. I want to print out the every step python goes toward the Final Goal. Here is my code:

def mazeDetector(row,col):
    c= m[row][col]
    solution=[]

    if c =="W":
        print "Wall here: "+ str(row)+ ","+ str(col)
        return False
    elif c =="V":
        print "Visited: " + str(row)+ ","+ str(col)
        return False
    elif c=="F":
        print "Found: "+ str(row)+ ","+ str(col)
        print solution
        return True

    print "visiting:"+ str(row)+ ","+ str(col) 
    solution.append((row,col),)
    m[row][col]=="V"
    if (col>0 and mazeDetector(row,col-1)):
        return True
    elif (row< len(m)-1 and mazeDetector(row+1,col)):
        return True
    elif (row>0 and mazeDetector(row-1, col)):
        return True
    elif (col<=len(m)-1 and mazeDetector(row, col+1)):
        return True
    return False
mazeDetector(1,5)

And here is the maze, W means wall, P means place to go, S means start, F means final:

[['W', 'P', 'P', 'W', 'W', 'W'], 
 ['W', 'W', 'P', 'W', 'P', 'S'], 
 ['W', 'W', 'P', 'W', 'P', 'W'], 
 ['P', 'P', 'P', 'P', 'P', 'W'], 
 ['F', 'W', 'P', 'W', 'W', 'W'], 
 ['W', 'P', 'P', 'P', 'P', 'W']]
有帮助吗?

解决方案

you have to pass solution into your function and not create it each time:

def mazeDetector(row,col, solution):
    c= m[row][col]
    solution.append((row, col))
    if c =="W":
        print "Wall here: "+ str(row)+ ","+ str(col)
        return False
    elif c =="V":
        print "Visited: " + str(row)+ ","+ str(col)
        return False
    elif c=="F":
        print "Found: "+ str(row)+ ","+ str(col)
        print solution
        return True

    print "visiting:"+ str(row)+ ","+ str(col) 
    m[row][col]=="V"
    if (col>0 and mazeDetector(row,col-1, list(solution))):
        return True
    elif (row< len(m)-1 and mazeDetector(row+1,col, list(solution))):
        return True
    elif (row>0 and mazeDetector(row-1, col, list(solution))):
        return True
    elif (col<=len(m)-1 and mazeDetector(row, col+1, list(solution))):
        return True
    return False
mazeDetector(1,5, [])

here's code which returns path if it exists

def mazeDetector(row, col, solution):
    solution.append((row, col))
    if m[row][col] == "F": return True, solution
    m[row][col] = "V"
    neighbors = [(row, col - 1), (row + 1, col), (row - 1, col), (row, col + 1)]
    neighbors = filter(lambda (r, c): r >= 0 and c >= 0 and r < len(m) and c < len(m) and m[r][c] not in ("V", "W"), neighbors)
    for r, c in neighbors:
        t, sol = mazeDetector(r, c, list(solution))
        if t: return True, sol
    return False, []

print mazeDetector(1, 5, [])[1]

其他提示

You could simply replace the 'P's on your actual solved route with another symbol possibly 'g' for Go this way!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top