您好,登錄后才能下訂單哦!
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)) }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。