def valid(grid, x, y): if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[0]) and grid[x][y] == 1: return True else: return False def walk(grid, x, y): if x == len(grid)-1 and y == len(grid[0])-1: print'success' grid[x][y] = 2 return True if valid(grid, x, y): grid[x][y] = 2 if walk(grid, x, y+1) or walk(grid, x-1, y) or walk(grid, x, y-1) or walk(grid, x+1, y): return True else: grid[x][y] = 1 return False else: return False grid=[ [1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1], [0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1], [1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1], [1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] if __name__ == '__main__': walk(grid, 0, 0) for i in range(len(grid)): print(grid[i])
#!/usr/bin/env python # -*- coding: utf-8 -*- # Created by xiaoqin00 #迷宫逃亡 result='' def mazeValid(x,y,maze): if x>=0 and y>=0 and x<len(maze) and y<len(maze) and maze[x][y]=='O': return True return False def mazeCracker(x,y,Maze,MazeOut): if x==int(MazeOut[0])-1 and y==int(MazeOut[1])-1: return True if mazeValid(x,y,Maze): Maze[x][y]='X' if mazeCracker(x+1,y,Maze,MazeOut)or mazeCracker(x-1,y,Maze,MazeOut) or mazeCracker(x,y+1,Maze,MazeOut) or mazeCracker(x,y-1,Maze,MazeOut): return True else: Maze[x][y]='X' return False else: return False #read txt if __name__ == '__main__': result='' #从txt文件中读取迷宫参数 f = open('C:\\Users\\00\\Desktop\\1.txt', 'r') mazeLen = [] mazeIn = [] mazeOut = [] maze = [] i = 1 #将文件中的迷宫参数分类 for line in f: if line[0].isdigit(): i = i % 3 if i == 1: mazeLen.append(line.strip('\n')) elif i == 2: mazeIn.append(line.strip('\n')) else: mazeOut.append(line.strip('\n')) i += 1 if line[0].isalpha(): maze.append(line.strip('\n')) count = 0 Maze = [] MazeTmp = [] for i in range(len(mazeIn)): mazeIn[i] = mazeIn[i].split() for i in range(len(mazeOut)): mazeOut[i] = mazeOut[i].split() for i in mazeLen: for j in maze[0:int(i)]: for k in j: MazeTmp.append(k) Maze.append(MazeTmp) MazeTmp = [] if mazeCracker(int(mazeIn[count][0]) - 1, int(mazeIn[count][1]) - 1, Maze, mazeOut[count]): result+='1' else: result+='0' count += 1 Maze = [] print result
参考的golang代码 package main import ( "fmt" ) func valid(grid [][]int, row int, column int) bool { // 验证可以不可以通行 if row >= 0 && row < len(grid) && column >= 0 && column < len(grid[0]) && grid[row][column] == 1 { return true } return false } func walk(grid [][]int, x int, y int) bool { // 递归退出 if x == len(grid)-1 && y == len(grid[0])-1 { fmt.Println(grid) return true } // 递归部分 if valid(grid, x, y) { grid[x][y] = 2 if !walk(grid, x, y+1) { // 回溯到原位置 grid[x][y] = 1 } else if !walk(grid, x-1, y) { // 回溯到原位置 grid[x][y] = 1 } else if !walk(grid, x, y-1) { // 回溯到原位置 grid[x][y] = 1 } else if !walk(grid, x+1, y) { // 回溯到原位置 grid[x][y] = 1 } else { return false } } return true } func main() { // 迷宫:1表示通路、0是墙 grid := [][]int{ {1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1}, {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0}, {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1}, {1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, } fmt.Println(walk(grid, 0, 0)) }
转载于:https://blog.51cto.com/xiaoqin00/1916282