Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- X, Y = 0, 1
- W, H = 5, 5
- X_MOVE = {+1: 'RIGHT', -1: 'LEFT'}
- Y_MOVE = {-1: 'UP', +1: 'DOWN'}
- def sign(x):
- if x > 0: return +1
- if x < 0: return -1
- return 0
- def next_move(posr, posc, board):
- path = [
- (1,1), (2,1), (3,1), (3,2), (3,3), (2,3), (1,3), (1,2)
- ]
- bot_pos = (posc, posr)
- if board[posr][posc] == 'd':
- print "CLEAN"
- return
- max_dist = W * W + H * H
- min_target_dist = max_dist
- target_pos = None
- follow_path = False
- path_index = 0
- for y in range(H):
- for x in range(W):
- v = board[y][x]
- if v != 'd':
- continue
- pos = (x,y)
- x_delta = pos[X] - bot_pos[X]
- y_delta = pos[Y] - bot_pos[Y]
- dist = (x_delta ** 2) + (y_delta ** 2)
- if dist < min_target_dist:
- min_target_dist = dist
- target_pos = pos
- if not target_pos:
- follow_path = True
- for i,pos in enumerate(path):
- x_delta = pos[X] - bot_pos[X]
- y_delta = pos[Y] - bot_pos[Y]
- dist = (x_delta ** 2) + (y_delta ** 2)
- if dist < min_target_dist:
- min_target_dist = dist
- target_pos = pos
- path_index = i
- x_delta = target_pos[X] - bot_pos[X]
- y_delta = target_pos[Y] - bot_pos[Y]
- if x_delta == 0 and y_delta == 0:
- target_pos = path[(path_index+1) % len(path)]
- x_delta = target_pos[X] - bot_pos[X]
- y_delta = target_pos[Y] - bot_pos[Y]
- x_dir = sign(x_delta)
- y_dir = sign(y_delta)
- if abs(x_delta) > abs(y_delta):
- action = X_MOVE[x_dir]
- else:
- action = Y_MOVE[y_dir]
- print action
- if __name__ == "__main__":
- pos = [int(i) for i in raw_input().strip().split()]
- board = [[j for j in raw_input().strip()] for i in range(5)]
- next_move(pos[0], pos[1], board)
Advertisement
Add Comment
Please, Sign In to add comment