我正在学习回溯法来解决迷宫中的老鼠问题 这是我的密码:
def Rat():
# Python3 program to solve Rat in a Maze
# problem using backracking
# The maze can be in any shape where 1 is the path and 0 is the wall
N = int(input("Which size of maze by dimension do you want"))
maze = [[int(randint(0, 1)) for i in range(N)] for i in range(N)]
print(maze)
print(len(maze))
if maze [0][0] == 0:
maze [0][0] = 1
# A utility function to print solution matrix sol
# A utility function to check if x, y is valid
def isSafe( maze, x, y ):
if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1:
print(x)
print(y)
return True
return False
def solveMaze( maze ):
global solveMazeUtil
# Creating a 4 * 4 2-D list
sol = [ [ 0 for j in range(N) ] for i in range(N) ]
if solveMazeUtil(maze, 0, 0, sol) == False:
print("Solution doesn't exist");
return False
elif solveMazeUtil(maze, 0, 0, sol) == False:
return sol
# A recursive utility function to solve Maze problem
def solveMazeUtil(maze, x, y, sol):
if x == N - 1 and y == N - 1 and maze[x][y]== 1:
sol[x][y] = 1
return True
# Check if maze[x][y] is valid
if isSafe(maze, x, y) == True:
# Check if the current block is already part of solution path.
if sol[x][y] == 1:
return False
# mark x, y as part of solution path
sol[x][y] = 1
# Move forward in x direction
if solveMazeUtil(maze, x + 1, y, sol) == True:
return True
# If moving in x direction doesn't give solution
# then Move down in y direction
if solveMazeUtil(maze, x, y + 1, sol) == True:
return True
# If moving in y direction doesn't give solution then
# Move back in x direction
if solveMazeUtil(maze, x - 1, y, sol) == True:
return True
# If moving in backwards in x direction doesn't give solution
# then Move upwards in y direction
if solveMazeUtil(maze, x, y - 1, sol) == True:
return True
# If none of the above movements work then
# BACKTRACK: unmark x, y as part of solution path
sol[x][y] = 0
return False
output = solveMaze(maze)
one_row = [x for l in maze for x in l]
one_row_2nd = [x for l in maze for x in l]
for i in range(len(one_row)):
if one_row[i] != one_row_2nd[i] and one_row_2nd[i] == 0:
one_row_2nd[i] = one_row[i]
else:
continue
fin_sol = np.reshape(one_row_2nd, (N, N))
#print(sol_1)
arr = np.array(fin_sol)
#print out raw and solution
if sol != False:
plt.figure()
f, axarr = plt.subplots(2,1,figsize= (10,10))
axarr[0].imshow(maze)
axarr[1].imshow(arr)
else:
plt.figure()
f, axarr = plt.subplots(figsize= (5,5))
axarr.imshow(maze)
# This code is taken idea and modified from the code of Shiv Shankar
Rat()
这是原来的一个:
# Python3 program to solve Rat in a Maze
# problem using backracking
# Maze size
N = 4
# A utility function to print solution matrix sol
def printSolution( sol ):
for i in sol:
for j in i:
print(str(j) + " ", end ="")
print("")
# A utility function to check if x, y is valid
# index for N * N Maze
def isSafe( maze, x, y ):
if x >= 0 and x < N and y >= 0 and y < N and maze[x][y] == 1:
return True
return False
""" This function solves the Maze problem using Backtracking.
It mainly uses solveMazeUtil() to solve the problem. It
returns false if no path is possible, otherwise return
true and prints the path in the form of 1s. Please note
that there may be more than one solutions, this function
prints one of the feasable solutions. """
def solveMaze( maze ):
# Creating a 4 * 4 2-D list
sol = [ [ 0 for j in range(4) ] for i in range(4) ]
if solveMazeUtil(maze, 0, 0, sol) == False:
print("Solution doesn't exist");
return False
printSolution(sol)
return True
# A recursive utility function to solve Maze problem
def solveMazeUtil(maze, x, y, sol):
# if (x, y is goal) return True
if x == N - 1 and y == N - 1 and maze[x][y]== 1:
sol[x][y] = 1
return True
# Check if maze[x][y] is valid
if isSafe(maze, x, y) == True:
# Check if the current block is already part of solution path.
if sol[x][y] == 1:
return False
# mark x, y as part of solution path
sol[x][y] = 1
# Move forward in x direction
if solveMazeUtil(maze, x + 1, y, sol) == True:
return True
# If moving in x direction doesn't give solution
# then Move down in y direction
if solveMazeUtil(maze, x, y + 1, sol) == True:
return True
# If moving in y direction doesn't give solution then
# Move back in x direction
if solveMazeUtil(maze, x - 1, y, sol) == True:
return True
# If moving in backwards in x direction doesn't give solution
# then Move upwards in y direction
if solveMazeUtil(maze, x, y - 1, sol) == True:
return True
# If none of the above movements work then
# BACKTRACK: unmark x, y as part of solution path
sol[x][y] = 0
return False
# Driver program to test above function
if __name__ == "__main__":
# Initialising the maze
maze = [ [1, 0, 0, 0],
[1, 1, 0, 1],
[0, 1, 0, 0],
[1, 1, 1, 1] ]
solveMaze(maze)
# This code is contributed by Shiv Shankar
首先,它警告说: UnboundLocalError:赋值前引用的局部变量“SolveMazutil” 所以我确实将SolveMazutil放在了全局类型中,但它随后注意到: 列表索引超出范围(只有在有解决方案时才会发生这种情况,如果没有,它可以顺利运行) 有人能帮我吗
您不必将
solveMazeUtil
作为全局函数。将solveMazeUtil
函数移到solveMaze
之外,这样应该可以工作。 当前,您的函数solveMaze
正在其定义之前调用solveMazeUtil
,并且无法找到该文件相关问题 更多 >
编程相关推荐