Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让我的迷宫函数打印出解决方案_Python_Maze - Fatal编程技术网

Python 如何让我的迷宫函数打印出解决方案

Python 如何让我的迷宫函数打印出解决方案,python,maze,Python,Maze,我正在解决一个迷宫问题。在代码能够找到目标之后,我不能让python打印出解决方案列表。但这是家庭作业所必需的 有人能帮我吗?我刚学了3周python。我想打印python走向最终目标的每一步。这是我的密码: def mazeDetector(row,col): c= m[row][col] solution=[] if c =="W": print "Wall here: "+ str(row)+ ","+ str(col) retur

我正在解决一个迷宫问题。在代码能够找到目标之后,我不能让python打印出解决方案列表。但这是家庭作业所必需的

有人能帮我吗?我刚学了3周python。我想打印python走向最终目标的每一步。这是我的密码:

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)

您可以简单地用另一个符号替换实际解决的路线上的“p”,可能是“g”,表示向这边走

您必须将解决方案传递到函数中,而不是每次都创建它:

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, [])
def mazeDetector(行、列、解决方案):
c=m[行][col]
解决方案。追加((行,列))
如果c==“W”:
打印“此处墙:”+str(行)+“,”+str(列)
返回错误
elif c==“V”:
打印“已访问:”+str(行)+“,”+str(列)
返回错误
elif c==“F”:
打印“找到:”+str(行)+“,“+str(列)
打印溶液
返回真值
打印“访问:”+str(行)+“,”+str(列)
m[行][列]=“V”
如果(列>0和mazeDetector(行,列-1,列表(解决方案)):
返回真值
elif(行0和mazeDetector(行-1,列,列表(解决方案)):
返回真值

elif(coleh)我如何让mazeDectector返回解决方案?您可以使用当前结果和解决方案的元组我如何让mazeDectector返回解决方案?返回找到的解决方案或作为元组调用的结果。
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, [])
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]