Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /
  2. -S-
  3. /
  4.  
  5. 0 0 0 0
  6. 0 + 0 + 0
  7. 0 0 0 + + 0
  8. 0 + 0 + 0 + 0
  9. 0 0 + + 0 0 + 0
  10. 0 0 + 0 + 0 0 + 0
  11. E 0 + 0 0 + + 0
  12. + + 0 + 0 + 0
  13. 0 0 0 0 0 +
  14. + 0 + + +
  15. 0 S 0 0
  16.  
  17. >>>RFRFFLFLFRFFLFFFLFLFFRFLFLFRFRFRF
  18.  
  19. + 0 0 0 0 0 0
  20. 0 0 0 0 0 + + 0
  21. 0 0 E 0 + 0 0 + 0
  22. 0 0 0 0 0 0 0 +
  23. 0 + 0 0 + + +
  24. 0 0 + + 0 0
  25. S + 0 0 0
  26.  
  27. >>>Invalid maze!
  28.  
  29. 0 E S
  30.  
  31. >>>LF
  32.  
  33.  
  34. E + 0
  35. 0 + + +
  36. 0 0 S
  37. + +
  38.  
  39. >>>FFLF
  40.  
  41. E
  42. 0 +
  43. 0 + +
  44. 0 +
  45. S
  46.  
  47. >>>RFFLFF
  48.  
  49. 0 E + 0 0
  50. 0 + 0 0 + +
  51. + 0 + + + 0
  52. + 0 + 0 + 0
  53. + + + 0 S
  54.  
  55. >>>FFLFLFFRFRFFRFF
  56.  
  57. E 0 + + 0
  58. 0 + 0 + + 0
  59. + + + 0 + 0
  60. + 0 0 0 0 0
  61. + + + + 0
  62. + 0 S 0
  63.  
  64. >>>FLFFRFFRFLF
  65.  
  66. z=zip;d=z((-1,1,-2,2,-1,1),(-1,-1,0,0,1,1));E=enumerate;D={};t=tuple;o=list;b=o.index
  67. for y,i in E(M.split('n')):
  68. for x,j in E(o(i)):
  69. c=(x,y);D[c]=j
  70. if j=='S':s=c
  71. if j=='E':e=c
  72. def P(s,e,D,p):
  73. p=o(p);p.append(s);D=D.copy();D[s]=''
  74. for i in d:
  75. c=t(x+y for x,y in z(s,i))
  76. if c not in p and c in D:
  77. if D[c]=='E':L.append(p+[c])
  78. if D[c]=='+':P(c,e,D,p)
  79. def R(p):
  80. a=[0,1,3,5,4,2];h=d[0];x=p[0];s=''
  81. for c in p[1:]:
  82. r=t(x-y for x,y in z(c,x));n=0
  83. while h!=r:n+=1;h=d[a[(b(a,b(d,h))+1)%6]]
  84. s+=['L'*(6-n),'R'*n][n<3]+'F';x=t(x+y for x,y in z(x,h))
  85. return s
  86. L=[];P(s,e,D,[])
  87. try:l=len(min(L))
  88. except ValueError:print"Invalid maze!"
  89. else:print min([R(i)for i in L if len(i)==l],key=len)
  90.  
  91. maze = """
  92. 0 0 0 0
  93. 0 + 0 + 0
  94. 0 0 0 + + 0
  95. 0 + 0 + 0 + 0
  96. 0 0 + + 0 0 + 0
  97. 0 0 + 0 + 0 0 + 0
  98. E 0 + 0 0 + + 0
  99. + + 0 + 0 + 0
  100. 0 0 0 0 0 +
  101. + 0 + + +
  102. 0 S 0 0
  103. """
  104. directions = [(-1, -1), (1, -1),
  105. (-2, 0), (2, 0),
  106. (-1, 1), (1, 1)]
  107.  
  108.  
  109. maze_dict = {}
  110. maze_lines = maze.split('n')
  111. for y, row in enumerate(maze_lines):
  112. if row:
  113. for x, item in enumerate(list(row)):
  114. coordinates = (x, y)
  115. maze_dict[coordinates] = item
  116. if item == 'S':
  117. start = coordinates
  118. elif item == 'E':
  119. end = coordinates
  120.  
  121. list_of_paths = []
  122.  
  123.  
  124. def find_path(start, end, maze_dict, current_path=None):
  125. if current_path is None:
  126. current_path = []
  127. current_path = list(current_path)
  128. current_path.append(start)
  129. current_dict = maze_dict.copy()
  130. current_dict[start] = '0'
  131.  
  132. for direction in directions:
  133. new_coordinate = (start[0] + direction[0], start[1] + direction[1])
  134.  
  135. if new_coordinate in current_path:
  136. pass
  137.  
  138. elif new_coordinate in current_dict:
  139. if current_dict[new_coordinate] == 'E':
  140. list_of_paths.append(current_path + [new_coordinate])
  141. break
  142. elif current_dict[new_coordinate] == '+':
  143. find_path(new_coordinate, end, current_dict, current_path)
  144.  
  145.  
  146. find_path(start, end, maze_dict)
  147.  
  148.  
  149. def find_route(path):
  150.  
  151. heading_R = [0, 1, 3, 5, 4, 2]
  152. heading = (-1, -1)
  153. current_pos = path[0]
  154. current_heading = directions.index(heading)
  155. output_string = []
  156. for coordinate in path[1:]:
  157. required_heading = (coordinate[0] - current_pos[0], coordinate[1] - current_pos[1])
  158.  
  159. count_R = 0
  160. while heading != required_heading:
  161. count_R += 1
  162. heading_index = directions.index(heading)
  163. heading_order = (heading_R.index(heading_index) + 1) % len(heading_R)
  164. heading = directions[heading_R[heading_order]]
  165.  
  166. if count_R:
  167. if count_R > 3:
  168. output_string += ['L'] * (6 - count_R)
  169. else:
  170. output_string += ['R'] * count_R
  171.  
  172. output_string.append('F')
  173. current_pos = (current_pos[0] + heading[0], current_pos[1] + heading[1])
  174. return ''.join(output_string)
  175.  
  176.  
  177. routes = []
  178. try:
  179. min_len = len(min(list_of_paths))
  180. except ValueError:
  181. print "Invalid maze!"
  182. else:
  183. for i in list_of_paths:
  184. if len(i) == min_len:
  185. routes.append(find_route(i))
  186.  
  187. print 'Shortest route to end: {}'.format(min(routes, key=len))
Add Comment
Please, Sign In to add comment