题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。
1. 010000 2. 000100 3. 001001 4. 110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按 DRRURRDDDR
的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。
请注意在字典序中 D<L<R<U
1. 01010101001011001001010110010110100100001000101010 2. 00001000100000101010010000100000001001100110100101 3. 01111011010010001000001101001011100011000000010000 4. 01000000001010100011010000101000001010101011001011 5. 00011111000000101000010010100010100000101100000000 6. 11001000110101000010101100011010011010101011110111 7. 00011011010101001001001010000001000101001110000000 8. 10100000101000100110101010111110011000010000111010 9. 00111000001010100001100010000001000101001100001001 10. 11000110100001110010001001010101010101010001101000 11. 00010000100100000101001010101110100010101010000101 12. 11100100101001001000010000010101010100100100010100 13. 00000010000000101011001111010001100000101010100011 14. 10101010011100001000011000010110011110110100001000 15. 10101010100001101010100101000010100000111011101001 16. 10000000101100010000101100101101001011100000000100 17. 10101001000000010100100001000100000100011110101001 18. 00101001010101101001010100011010101101110000110101 19. 11001010000100001100000010100101000001000111000010 20. 00001000110000110101101000000100101001001000011101 21. 10100101000101000000001110110010110101101010100001 22. 00101000010000110101010000100010001001000100010101 23. 10100001000110010001000010101001010101011111010010 24. 00000100101000000110010100101001000001000000000010 25. 11010000001001110111001001000011101001011011101000 26. 00000110100010001000100000001000011101000000110011 27. 10101000101000100010001111100010101001010000001000 28. 10000010100101001010110000000100101010001011101000 29. 00111100001000010000000110111000000001000000001011 30. 10000001100111010111010001000110111010101101111000
思路
见注释
代码
1. from queue import Queue 2. 3. #设计结构体,存储当前位置和到达此位置的路径 4. class node: 5. def __init__(self,x,y,path): 6. self.x=x 7. self.y=y 8. self.path=path 9. 10. #输入地图 11. mp = [] 12. for i in range(0, 30): 13. mp.append(input()) 14. 15. #字典序,字典序小的优先存入路径 16. k=['D','L','R','U'] 17. #位置走向,对应字典序,D,L,R,U 18. dir=[(1,0),(0,-1),(0,1),(-1,0)] 19. #记录是否访问过 20. vis=[[0]*50 for i in range(30)] 21. 22. #宽度优先搜索 23. def bfs(): 24. start=node(0,0,"") 25. start.x,start.y,start.path=0,0,"" 26. vis[0][0]=1 27. q=[] 28. q.append(start) 29. 30. while len(q) != 0: 31. now=q[0] 32. q.pop(0) 33. #如果到达了终点 34. if now.x==29 and now.y==49: 35. print(now.path) 36. return 37. #向四个方向延伸 38. for i in range(4): 39. next = node(0,0,"") 40. next.x=now.x+dir[i][0] 41. next.y=now.y+dir[i][1] 42. #如果超出了边界,跳过 43. if next.x<0 or next.x>=30 or next.y<0 or next.y>=50: 44. continue 45. #如果已经访问过或者遇到了墙,跳过 46. if vis[next.x][next.y]==1 or mp[next.x][next.y]=='1': 47. continue 48. #标记该点 49. vis[next.x][next.y]=1 50. #记录路径 51. next.path=now.path+k[i] 52. #进入队列 53. q.append(next) 54. 55. bfs()