Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. import Queue as queue
  2.  
  3. WORLD_SIZE = 25
  4.  
  5. world = open("grid1.txt").read().replace("S","-1").replace("E","-1").split("\n")[:WORLD_SIZE]
  6. map_state = []
  7.  
  8. for i in range(WORLD_SIZE):
  9. world[i] = world[i].split(",")
  10. world[i] = map(int, world[i])
  11.  
  12. map_state.append([0]*WORLD_SIZE)
  13. for j in range(WORLD_SIZE):
  14. map_state[i][j] = {}
  15.  
  16. #print world
  17.  
  18. def gen_new_state(st, nx, ny, pv):
  19. global world
  20.  
  21. ns = {
  22. "pos" : [ nx, ny ],
  23. "d" : st["d"] + world[ny][nx], # + st["m"],
  24. "m" : st["m"] + 1,
  25. "path" : st["path"] + pv
  26. }
  27.  
  28. rem_step = (WORLD_SIZE - 1 - ns["pos"][0] + WORLD_SIZE - 1 - ns["pos"][1])
  29. done_step = ns["m"]
  30. rem_dmg = (rem_step + done_step) * (rem_step + done_step + 1) / 2 - (done_step * (done_step + 1) / 2)
  31. exp_dmg = ns["d"] #+ rem_dmg
  32.  
  33. return (exp_dmg, 0, ns)
  34.  
  35. def worth_adding(st):
  36. ms = map_state[st["pos"][0]][st["pos"][1]]
  37.  
  38. for x in ms:
  39. if int(x) <= st["m"] and ms[x] <= st["d"]:
  40. return False
  41.  
  42. ms[str(st["m"])] = st["d"]
  43. return True
  44.  
  45. def eval_path(path):
  46. v = 0
  47. m = 0
  48. px, py = 0, 0
  49. for l in path:
  50. if l == "L":
  51. px -= 1
  52. v += world[py][px] + m
  53. if l == "R":
  54. px += 1
  55. v += world[py][px] + m
  56. if l == "U":
  57. py -= 1
  58. v += world[py][px] + m
  59. if l == "D":
  60. py += 1
  61. v += world[py][px] + m
  62. m += 1
  63. return v
  64.  
  65. #print eval_path("DRRDDDDDRRDDLLDDDRRDRRRRDDDDRRRRRRRRDRDRDDRRDRRDRDDR")
  66. #DRRDDDDDRRDDLDDDRRDDDDDRRRRRRRRRRRDRRDDDRRDRRDRDDR
  67. #exit()
  68.  
  69. state = queue.PriorityQueue()
  70. state.put((0, 0 ,{ "pos" : [ 0, 0 ], "d" : 0, "m" : 0, "path": "" }))
  71. map_state[0][0]["0"] = 0
  72.  
  73. smallest_val = 9999
  74. smallest_path = ""
  75.  
  76. while True:
  77. if state.qsize() == 0:
  78. break
  79.  
  80. st = state.get()[2]
  81. ms = map_state[st["pos"][0]][st["pos"][1]]
  82.  
  83. #print state.qsize()
  84. #print st
  85.  
  86. if not ms[str(st["m"])] == st["d"]:
  87. #print "Discard found better way !"
  88. continue
  89.  
  90. if st["pos"][0] > 0:
  91. new_x = st["pos"][0] - 1
  92. new_y = st["pos"][1]
  93. ns = gen_new_state(st, new_x, new_y, "L")
  94.  
  95. if worth_adding(ns[2]):
  96. state.put(ns)
  97.  
  98. if st["pos"][0] <= WORLD_SIZE - 2:
  99. new_x = st["pos"][0] + 1
  100. new_y = st["pos"][1]
  101. ns = gen_new_state(st, new_x, new_y, "R")
  102.  
  103. if world[new_y][new_x] == -1:
  104. tdmg = eval_path(st["path"])
  105.  
  106. if tdmg < smallest_val:
  107. smallest_val = tdmg
  108. smallest_path = st["path"] + "R"
  109.  
  110. #print "Found !"
  111. #print st["path"] + "R"
  112. #print st["d"]
  113. #print eval_path(st["path"])
  114.  
  115.  
  116. else:
  117. if worth_adding(ns[2]):
  118. state.put(ns)
  119.  
  120. if st["pos"][1] > 0:
  121. new_x = st["pos"][0]
  122. new_y = st["pos"][1] - 1
  123. ns = gen_new_state(st, new_x, new_y, "U")
  124.  
  125. if worth_adding(ns[2]):
  126. state.put(ns)
  127.  
  128. if st["pos"][1] <= WORLD_SIZE - 2:
  129. new_x = st["pos"][0]
  130. new_y = st["pos"][1] + 1
  131. ns = gen_new_state(st, new_x, new_y, "D")
  132.  
  133. if world[new_y][new_x] == -1:
  134. tdmg = eval_path(st["path"])
  135.  
  136. if tdmg < smallest_val:
  137. smallest_val = tdmg
  138. smallest_path = st["path"] + "D"
  139.  
  140. #print "Found !"
  141. #print st["path"] + "D"
  142. #print st["d"]
  143. #print eval_path(st["path"])
  144. else:
  145. if worth_adding(ns[2]):
  146. state.put(ns)
  147.  
  148. print smallest_path
  149. print smallest_val
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement