鱼C论坛

 找回密码
 立即注册
查看: 1192|回复: 0

爬虫递归解决二维数组迷宫问题

[复制链接]
发表于 2020-3-19 13:43:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 fbsfbsdb 于 2020-3-19 13:45 编辑

python maze solving answer (Recursion)

talk is cheap show me the code!

  1. #import part
  2. import time
  3. import sys
  4. import os

  5. #global values
  6. load = 0                                 # set initial loading value
  7. walk = [(1,0),(-1,0),(0,1),(0,-1)]       # set movements
  8. backtracker = []                         # set backtracker (remember that this is a stack)
  9. gmainmaze = []                           # set initial blank maze

  10. def initialize():   #loading
  11.     sx = 0 # start x value
  12.     sy = 0 # start y value
  13.     ex = 0 # end x value
  14.     ey = 0 # end y value
  15.     while load == 0:    #just some cool animation
  16.         sys.stdout.write("\rloading |")
  17.         time.sleep(0.1)
  18.         sys.stdout.write("\rloading /")
  19.         time.sleep(0.1)
  20.         sys.stdout.write("\rloading -")
  21.         time.sleep(0.1)
  22.         sys.stdout.write("\rloading \")
  23.         time.sleep(0.1)
  24.         sys.stdout.write("\r")
  25.         mainmaze = loadMaze("maze.txt") # IMPORTANT: load the maze under the same folder named "maze.txt"
  26.         print("Maze Loaded")
  27.         print(" ")
  28.         time.sleep(1)
  29.         print(" ")
  30.         print("Locating start point...")
  31.         sx, sy = spotter(5,mainmaze)
  32.         time.sleep(1)
  33.         print(" ")
  34.         print("Locating end point...")
  35.         ex, ey = spotter(3,mainmaze)
  36.         print(" ")
  37.         time.sleep(1)
  38.         return sx, sy, ex, ey, mainmaze;
  39.         break
  40.     print(" ")
  41.     sys.stdout.write("\rInitialized!")
  42.     print(" ")
  43.     time.sleep(3)
  44.     os.system("cls")

  45. def loadMaze(filename):
  46.     #load the data from the file into a 2D list
  47.     with open(filename) as i:
  48.         maze = []
  49.         for line in i:
  50.             line = line.strip()
  51.             spawnmaze = line.split(", ")
  52.             maze.append(spawnmaze)
  53.     return maze

  54. def spotter(num,maze):
  55.     #Locate the 'element' in the maze (this can either be "5" or "3")
  56.     num = str(num)
  57.     rowcounter = -1
  58.     linecounter = 0
  59.     for rows in maze:
  60.         rowcounter = rowcounter + 1
  61.         if num in rows:
  62.             for element in rows:
  63.                 if element == num:
  64.                     print("Tango Spotted, Grid:", rowcounter, linecounter)
  65.                     return rowcounter, linecounter;
  66.                     break
  67.                 linecounter = linecounter + 1

  68. def valid(maze,x,y): # check if valid
  69.     height = len(maze) - 1
  70.     width = len(maze[0]) - 1
  71.     if 0 <= x <= height and 0 <= y <= width:
  72.         return 1
  73.     else:
  74.         return 0

  75. def solveMaze(sx,sy,ex,ey): # solve maze
  76.     global gmainmaze
  77.     for i in walk:          #try every direction
  78.         x = sx + i[0]       #make the move
  79.         y = sy + i[1]
  80.         if valid(gmainmaze,x,y): # check if still in the maze
  81.             if x == ex and y == ey: #check if reached destination
  82.                 print("SITREP:Destination Arrived")
  83.                 print("The Logistic Path Is:\n",backtracker)
  84.                 print(" ")
  85.                 return 1
  86.             else:
  87.                 if gmainmaze[x][y] == "0" and (x,y) not in backtracker: #add to the stack
  88.                     backtracker.append((x,y))
  89.                 else:
  90.                     continue
  91.         else:
  92.             continue
  93.         if solveMaze(x,y,ex,ey) == 1: # Recursion (do the next step)
  94.             return 1
  95.         else:
  96.             continue
  97.     go = backtracker.pop(-1) #moveback while
  98.     return 0

  99. def printMaze(maze):
  100.     for x in range(len(maze)):
  101.         for y in range(len(maze[x])):
  102.             print(maze[x][y], end=' ')
  103.         print("")

  104. def visualize(maze):    # a cool function to visualize the maze
  105.     for x in range(len(maze)):
  106.         for y in range(len(maze[x])):
  107.             if maze[x][y] == "5":
  108.                 maze[x][y] = "╳"    # path
  109.             elif maze[x][y] == "0":
  110.                 maze[x][y] = "&#9617;"    # unbeen path
  111.             elif maze[x][y] == "1":
  112.                 maze[x][y] = "█"    # wall
  113.             elif maze[x][y] == "3":
  114.                 maze[x][y] = "╳"    # basically path as well
  115.             print(maze[x][y], end=' ')
  116.         print("")

  117.      
  118. def main():
  119.     #initialize the maze
  120.     sx, sy, ex, ey, mainmaze = initialize()
  121.     global gmainmaze
  122.     gmainmaze = mainmaze
  123.     load = 1
  124.     #solve the maze
  125.     gmainmaze[sx][sy] = "0"
  126.     gmainmaze[ex][ey] = "0" # change the start and end into "0" to make it a way
  127.     solveMaze(sx,sy,ex,ey)
  128.     for i in backtracker:
  129.         gmainmaze[i[0]][i[1]] = "5"
  130.     gmainmaze[sx][sy] = "5"
  131.     gmainmaze[ex][ey] = "3" # change the start and end back
  132.     #print the maze
  133.     width = len(gmainmaze[0]) - 1
  134.     print("Check Your Map...")
  135.     print("Map:")
  136.     print("--"*(width+1))
  137.     printMaze(gmainmaze)
  138.     print("--"*(width+1))
  139.     #visualize the maze uncomment to establish the function (btw check if your terminal can print these unicodes)
  140.     #visualize(gmainmaze)
  141.     time.sleep(5)

  142. main()
复制代码



迷宫长这样:
0, 0, 0, 0, 0, 0, 0, 0, 0
0, 1, 1, 1, 0, 1, 1, 1, 0
0, 1, 0, 0, 0, 0, 0, 1, 0
0, 1, 0, 1, 1, 1, 0, 0, 0
0, 0, 0, 1, 1, 3, 0, 1, 0
0, 1, 0, 0, 0, 0, 0, 1, 0
0, 1, 1, 1, 0, 1, 1, 1, 0
0, 0, 0, 0, 0, 0, 0, 0, 5        


结果:
&#9617; &#9617; &#9617; &#9617; &#9617; &#9617; &#9617; &#9617; &#9617;
&#9617; █ █ █ &#9617; █ █ █ &#9617;
&#9617; █ &#9617; &#9617; &#9617; &#9617; &#9617; █ &#9617;
&#9617; █ &#9617; █ █ █ &#9617; &#9617; &#9617;
&#9617; &#9617; &#9617; █ █ ╳ &#9617; █ &#9617;
&#9617; █ &#9617; &#9617; ╳ ╳ &#9617; █ &#9617;
&#9617; █ █ █ ╳ █ █ █ ╳
&#9617; &#9617; &#9617; &#9617; ╳ ╳ ╳ ╳ ╳

github(自古以来那些看ph站的看不起我们这些看gh站的):https://github.com/JK88-1337/MazeSolve
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 23:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表