python走迷宫,求出方法数并输出各种方法,可运行,含代码

[size=24px]此代码可以正确运行,我在网上见到了类似代码,但是存在不足:只适用于部分情况,我加以修改,得到自己的正确代码。[/size]
[align=center][color=#FF0000][size=18px]以下是有问题的源码:[/size][/color][/align]
[code=python]
maze=[[1,0,0,1,0,1],
[1,1,1,0,1,0],
[0,0,1,0,1,0],
[0,1,1,1,0,0],
[0,0,0,1,0,0],
[1,0,0,0,0,0]]

判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。

def valid(maze,x,y):
if (x>=0 and x<len(maze) and y>=0 and y<len(maze[0]) and maze[x][y]==1):
return True
else:
return False

移步函数实现

def walk(maze,x,y):
# 如果位置是迷宫的出口,说明成功走出迷宫
if(x0 and y0):
print(“successful!”)
return True
# 递归主体实现
if valid(maze,x,y):
print(x,y)
maze[x][y]=2 # 做标记,防止折回
# 针对四个方向依次试探,如果失败,撤销一步
if not walk(maze,x-1,y):
maze[x][y]=1
elif not walk(maze,x,y-1):# elif就是问题所在
maze[x][y]=1
elif not walk(maze,x+1,y):
maze[x][y]=1
elif not walk(maze,x,y+1):
maze[x][y]=1
else:
return False # 无路可走说明,没有解
return True

walk(maze,3,3)
[/code]
[size=24px][color=#FF0000]运行结果:[/color][/size]
Connected to pydev debugger (build 183.5912.18)
3 3
3 2
2 2
1 2
1 1
1 0
successful!
3 1
4 3
[color=#FF0000][size=24px]如果改变参数,就会体现出问题:[/size][/color]
[code=python]
maze = [[1, 0, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0]]

判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。

def valid(maze, x, y):
if x >= 0 and x < len(maze) and y >= 0 and y < len(maze[0]) and maze[x][y] == 1:
return True
else:
return False

移步函数实现

def walk(maze, x, y):
# 如果位置是迷宫的出口,说明成功走出迷宫
if x == 0 and y == 0:
print(“successful!”)
return True
# 递归主体实现
if valid(maze, x, y):
print(x, y)
maze[x][y] = 2 # 做标记,防止折回
# 针对四个方向依次试探,如果失败,撤销一步
if not walk(maze, x - 1, y):
maze[x][y] = 1
elif not walk(maze, x, y - 1):
maze[x][y] = 1
elif not walk(maze, x + 1, y):
maze[x][y] = 1
elif not walk(maze, x, y + 1):
maze[x][y] = 1
else:
return False # 无路可走说明,没有解
return True

walk(maze, 4, 2)

[/code]
[size=24px][color=#FF0000]输出:[/color][/size]
[code=python]
Connected to pydev debugger (build 183.5912.18)
4 2
4 3
3 3
3 4
2 4
1 4
1 3
1 2
0 2

Process finished with exit code 0
[/code]
[color=#FF0000][size=18px]以下代码显示步骤[/size][/color]
[code=python]

maze = [[1, 0, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 0]]

判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。

def valid(maze, x, y):
if x >= 0 and x < len(maze) and y >= 0 and y < len(maze[0]) and maze[x][y] == 1
and successful != 1: # 新加的
return True
else:
return False

移步函数实现

def walk(maze, x, y):
# 如果位置是迷宫的出口,说明成功走出迷宫
global successful
if x == 0 and y == 0:
print(“successful!”)
successful = 1 # 新加的
return True
# 递归主体实现
if valid(maze, x, y):
print(x, y)
maze[x][y] = 2 # 做标记,防止折回
# 针对四个方向依次试探,如果失败,撤销一步
if not walk(maze, x - 1, y):
maze[x][y] = 1
if not walk(maze, x, y - 1): # elif改为if
maze[x][y] = 1
if not walk(maze, x + 1, y): # elif改为if
maze[x][y] = 1
if not walk(maze, x, y + 1): # elif改为if
maze[x][y] = 1

    return False  # 无路可走说明,没有解
return True

successful = 0
walk(maze, 4, 2)

[/code]
[color=#FF0000]虽然步骤正确,但是没有输出sccess[size=24px][/size][/color]
下面是改进
[code=python]
maze = [[1, 0, 1, 1, 0, 1],
[1, 1, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 0]]

判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。

def valid(maze, x, y):
if x >= 0 and x < len(maze) and y >= 0 and y < len(maze[0]) and maze[y][x] == 1:
return True
else:
return False

移步函数实现

def walk(maze, x, y):
# 如果位置是迷宫的出口,说明成功走出迷宫
if x == 0 and y == 0:
print(“successful!”)
return True
# 递归主体实现
if valid(maze, x, y):
print(x, y)
maze[y][x] = 2 # 做标记,防止折回
# 针对四个方向依次试探,如果失败,撤销一步
if not walk(maze, x - 1, y):
maze[y][x] = 1
if not walk(maze, x, y - 1):
maze[y][x] = 1
if not walk(maze, x + 1, y):
maze[y][x] = 1
if not walk(maze, x, y + 1):
maze[y][x] = 1

    return False  # 无路可走说明,没有解
return True

walk(maze, 2, 4)

[/code]
[color=#FF0000][size=18px]以下代码显示走迷宫过程,并且x,y是真正意义上的x,y(x表示第几行,y表示第几列):[/size][/color]
[code=python]
maze = [[1, 0, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 0]]

判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。

def valid(x, y):
if y >= 0 and y < len(maze) and x >= 0 and x < len(maze[0]) and maze[y][x] == 1:
return True
else:
return False

def check(x, y):
if y >= 0 and y < len(maze) and x >= 0 and x < len(maze[0]) and maze[y][x] == 1:
return True
else:
return False

def walk(x, y):
global success,procedure_list
maze[y][x] = 0
procedure_list.append((x,y))
if x == 0 and y == 0:
print(“success”)
success = 1
return True
if success != 1:
over = False
for pos_y, pos_x in pos_ls:

        if check(x + pos_x, y + pos_y):
            over = True
            walk(x + pos_x, y + pos_y)
    if not over:
        maze[y][x] = 1
        procedure_list.remove((x,y))


return success

procedure_list=[]
success = 0
pos_ls = [(0, -1), (-1, 0), (0, 1), (1, 0)]
walk(2, 4)
print(procedure_list)
[/code]
[color=#FF0000][size=18px]以下代码求出走迷宫方法数和各种走法:[/size][/color]
[code=python]
maze = [[1, 0, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 0]]

判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。

def check(x, y):
if y >= 0 and y < len(maze) and x >= 0 and x < len(maze[0]) and maze[y][x] == 1:
return True
else:
return False

def walk(x, y):
global success, procedure_list, success_count
maze[y][x] = 0
procedure_list.append((x, y))
if x == 0 and y == 0:
print(“success”)
success = 1
success_count += 1
success_procedure_ls.append(list(procedure_list))
procedure_list.remove((0,0))
maze[y][x] = 1
return True
if success < 4:
over = False
for pos_y, pos_x in pos_ls:

        if check(x + pos_x, y + pos_y):
            over = True
            walk(x + pos_x, y + pos_y)

    maze[y][x] = 1
    procedure_list.remove((x, y))

return success

procedure_list = []
success = 0
success_procedure_ls = []
success_count = 0
pos_ls = [(0, -1), (-1, 0), (0, 1), (1, 0)]
walk(2, 4)
print(procedure_list)
print(“一共{}中方法”.format(success_count))
for way in success_procedure_ls:
print(way)

[/code]
[color=#FF0000][size=18px]运行结果:[/size][/color]
[code=python]
pydev debugger: process 1548 is connecting

Connected to pydev debugger (build 183.5912.18)
success
success
success
success
success
success
success
success
success
success
success
[]
一共11中方法
[(2, 4), (2, 3), (2, 2), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (2, 2), (3, 2), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (2, 2), (3, 2), (4, 2), (4, 1), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (2, 2), (3, 2), (3, 3), (4, 3), (4, 2), (4, 1), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (3, 2), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (3, 2), (4, 2), (4, 1), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (4, 3), (4, 2), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (4, 3), (4, 2), (3, 2), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (4, 3), (4, 2), (4, 1), (3, 1), (2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 4), (2, 3), (3, 3), (4, 3), (4, 2), (4, 1), (3, 1), (3, 2), (2, 2), (2, 1), (1, 1), (0, 1), (0, 0)]

Process finished with exit code 0

[/code]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值