Advertisement
magnus0810

Untitled

Nov 27th, 2020
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. class field:
  2.     def __init__(self, file):
  3.         self.up = [[0 for i in range(7)] for j in range(12)]
  4.         self.down = [[0 for i in range(7)] for j in range(12)]
  5.         self.left = [[0 for i in range(7)] for j in range(12)]
  6.         self.right = [[0 for i in range(7)] for j in range(12)]
  7.         self.visited = [[0 for i in range(7)] for j in range(12)]
  8.         self.parent = [[[0 , 0] for i in range(7)] for j in range(12)]
  9.         with open(file,"rt") as infile:
  10.             self.map = [list(line.strip()) for line in infile.readlines()]
  11.         for x in range(12):
  12.             for y in range(7):
  13.                 if self.map[x][y] == 's':
  14.                     self.start = [x, y]
  15.                 if self.map[x][y] == 't':
  16.                     self.tomato = [x, y]
  17.                 if self.map[x][y] == 'g':
  18.                     self.goal = [x, y]
  19.                 try:
  20.                     if self.map[x-1][y] in ('f','t','g','s'):
  21.                         self.up[x][y] = 1
  22.                     else:
  23.                         self.up[x][y] = 0
  24.                 except:
  25.                     self.up[x][y] = 0
  26.  
  27.                 try:
  28.                     if self.map[x][y-1] in ('f','t','g','s'):
  29.                         self.left[x][y] = 1
  30.                     else:
  31.                         self.left[x][y] = 0
  32.                 except:
  33.                     self.left[x][y] = 0
  34.                
  35.                 try:
  36.                     if self.map[x][y+1] in ('f','t','g','s'):
  37.                         self.right[x][y] = 1
  38.                     else:
  39.                         self.right[x][y] = 0
  40.                 except:
  41.                     self.right[x][y] = 0
  42.  
  43.                 try:
  44.                     if self.map[x+1][y] in ('f','t','g','s'):
  45.                         self.down[x][y] = 1
  46.                     else:
  47.                         self.down[x][y] = 0
  48.                 except:
  49.                     self.down[x][y] = 0
  50.                 #print(x,y, self.left[x][y], self.up[x][y], self.down[x][y], self.right[x][y],sep=' ')
  51.  
  52.     def clearpath(self):
  53.         self.visited = [[0 for i in range(7)] for j in range(12)]
  54.         self.parent = [[[0 , 0] for i in range(7)] for j in range(12)]
  55.                        
  56.                    
  57.     def search(self, endx, endy, startx, starty):
  58.         print(startx, starty, endx, endy)
  59.         goalfound = 0
  60.         steps = 0
  61.         queue = [[startx, starty]]
  62.         solution = [[]]
  63.         solution.pop(0)
  64.         while not goalfound:
  65.             x, y = queue.pop(0)
  66.             self.visited[x][y] = 1
  67.             steps += 1
  68.             if self.up[x][y]:
  69.                 if not self.visited[x-1][y]:
  70.                     queue.append([x-1, y])
  71.                     self.parent[x-1][y] = x, y
  72.             if self.down[x][y]:
  73.                 if not self.visited[x+1][y]:
  74.                     queue.append([x+1, y])
  75.                     self.parent[x+1][y] = x, y
  76.             if self.right[x][y]:
  77.                 if not self.visited[x][y+1]:
  78.                     queue.append([x, y+1])
  79.                     self.parent[x][y+1] = x, y
  80.             if self.left[x][y]:
  81.                 if not self.visited[x][y-1]:
  82.                     queue.append([x, y-1])
  83.                     self.parent[x][y-1] = x, y
  84.             if [x, y] == [endx, endy]:
  85.                 goalfound = 1
  86.                 #for row in self.visited:
  87.                 #    print(*row)
  88.                 #for row in self.parent:
  89.                 #    print(*row)
  90.  
  91.                 while [startx, starty] != [x, y]:
  92.                     [x, y] = self.parent[x][y]
  93.                     solution.append([x, y])
  94.                 self.clearpath()
  95.                 return solution
  96.  
  97.  
  98.  
  99.  
  100. map = field("map.txt")
  101.  
  102. #print(map.start)
  103. #print(map.tomato)
  104. #print(map.goal)
  105. #print(map.map)
  106. solution = map.search(map.start[0], map.start[1], map.tomato[0], map.tomato[1]) + map.search(map.tomato[0], map.tomato[1], map.goal[0], map.goal[1])
  107.  
  108. print(map.search(map.tomato[0], map.tomato[1], map.start[0], map.start[1]))
  109. print(map.search(map.goal[0], map.goal[1],map.tomato[0], map.tomato[1]))
  110. print(solution)
  111. laststep = map.start
  112. lsolution = ""
  113. while solution:
  114.     step = solution.pop(0)
  115.     if (step[0]+1) == laststep[0]:
  116.         lsolution = lsolution + "U"
  117.     elif (step[0]-1) == laststep[0]:
  118.         lsolution = lsolution + "D"
  119.     elif (step[1]+1) == laststep[1]:
  120.         lsolution = lsolution + "L"
  121.     elif (step[1]-1) == laststep[1]:
  122.         lsolution = lsolution + "R"
  123.     laststep = step
  124. print(lsolution)
  125. #while not map.goalfound:
  126.    
  127.  
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement