runnig

BotClean partially observable

Feb 7th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. X, Y = 0, 1
  2. W, H = 5, 5
  3. X_MOVE = {+1: 'RIGHT', -1: 'LEFT'}
  4. Y_MOVE = {-1: 'UP', +1: 'DOWN'}
  5. def sign(x):
  6.   if x > 0: return +1
  7.   if x < 0: return -1
  8.   return 0
  9.  
  10. def next_move(posr, posc, board):
  11.   path = [
  12.     (1,1), (2,1), (3,1), (3,2), (3,3), (2,3), (1,3), (1,2)
  13.   ]
  14.   bot_pos = (posc, posr)
  15.   if board[posr][posc] == 'd':
  16.     print "CLEAN"
  17.     return
  18.  
  19.   max_dist = W * W + H * H
  20.   min_target_dist = max_dist
  21.   target_pos = None  
  22.   follow_path = False
  23.   path_index = 0
  24.   for y in range(H):
  25.     for x in range(W):
  26.       v = board[y][x]
  27.       if v != 'd':
  28.         continue
  29.       pos = (x,y)
  30.       x_delta = pos[X] - bot_pos[X]
  31.       y_delta = pos[Y] - bot_pos[Y]
  32.       dist = (x_delta ** 2) + (y_delta ** 2)
  33.       if dist < min_target_dist:
  34.         min_target_dist = dist
  35.         target_pos = pos
  36.   if not target_pos:
  37.     follow_path = True
  38.     for i,pos in enumerate(path):
  39.       x_delta = pos[X] - bot_pos[X]
  40.       y_delta = pos[Y] - bot_pos[Y]
  41.       dist = (x_delta ** 2) + (y_delta ** 2)
  42.       if dist < min_target_dist:
  43.         min_target_dist = dist
  44.         target_pos = pos
  45.         path_index = i
  46.        
  47.   x_delta = target_pos[X] - bot_pos[X]
  48.   y_delta = target_pos[Y] - bot_pos[Y]
  49.   if x_delta == 0 and y_delta == 0:
  50.     target_pos = path[(path_index+1) % len(path)]
  51.     x_delta = target_pos[X] - bot_pos[X]
  52.     y_delta = target_pos[Y] - bot_pos[Y]
  53.    
  54.   x_dir = sign(x_delta)
  55.   y_dir = sign(y_delta)
  56.   if abs(x_delta) > abs(y_delta):
  57.     action = X_MOVE[x_dir]
  58.   else:
  59.     action = Y_MOVE[y_dir]
  60.   print action
  61.  
  62.  
  63. if __name__ == "__main__":
  64.     pos = [int(i) for i in raw_input().strip().split()]
  65.     board = [[j for j in raw_input().strip()] for i in range(5)]
  66.     next_move(pos[0], pos[1], board)
Advertisement
Add Comment
Please, Sign In to add comment