首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让我的迷宫函数打印出解决方案

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

Stack Overflow用户
提问于 2013-07-22 03:37:25
回答 2查看 1.2K关注 0票数 0

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

有人能帮帮我吗?我刚学了3个星期的python。我想把python迈向最终目标的每一步都打印出来。下面是我的代码:

代码语言:javascript
复制
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)

这里是迷宫,W表示墙,P表示要去的地方,S表示开始,F表示最终:

代码语言:javascript
复制
[['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']]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-22 03:47:20

你必须将解决方案传递给你的函数,而不是每次都创建它:

代码语言:javascript
复制
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, [])

下面的代码返回path,如果它存在

代码语言:javascript
复制
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]
票数 1
EN

Stack Overflow用户

发布于 2013-07-22 03:44:34

你可以简单地用另一个符号来替换你实际求解的路线上的‘P’,可能是'g‘代表Go this way!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17776113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档