Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import sys
- import math
- import time
- from copy import deepcopy
- from typing import Tuple, List, Optional
- def idebug(*args):
- # return
- print(*args, file=sys.stderr, flush=True)
- def debug(*args):
- # return
- print(*args, file=sys.stderr, flush=True)
- def dist(x, y, X, Y):
- return abs(X - x) + abs(Y - y)
- def get_directions(grid, x, y):
- directions = []
- for _dir, (dx, dy) in DIRECTIONS.items():
- new_x, new_y = x + dx, y + dy
- if not (0 <= new_x < width and 0 <= new_y < width) or grid[new_y][new_x] != '.' or (new_x, new_y) in visited:
- continue
- directions.append(_dir)
- return directions
- def bfs(grid, submarine, phantom):
- x, y = submarine
- X, Y = phantom
- # Auto-generated code below aims at helping you parse
- # the standard input according to the problem statement.
- DIRECTIONS = {'E': (1, 0), 'S': (0, 1), 'W': (-1, 0), 'N': (0, -1)}
- width, height, my_id = [int(i) for i in input().split()]
- idebug(width, height, my_id)
- grid = []
- for i in range(height):
- line = input()
- idebug(line)
- grid.append(list(line))
- # Write an action using print
- # To debug: print("Debug messages...", file=sys.stderr, flush=True)
- start_positions = [(x, y) for x in range(height) for y in range(width) if grid[y][x] == '.']
- start_x, start_y = random.choice(start_positions)
- print(start_x, start_y)
- phantoms = {i + width * j: [(i, j)] for j in range(15) for i in range(15) if grid[i][j] == '.'}
- debug(f'{len(phantoms)} phantoms: {phantoms}')
- visited = []
- # game loop
- while True:
- line = input()
- idebug(line)
- x, y, my_life, opp_life, torpedo_cooldown, sonar_cooldown, silence_cooldown, mine_cooldown = [int(i) for i in line.split()]
- sonar_result = input()
- idebug(sonar_result)
- opponent_orders = input()
- idebug(opponent_orders)
- visited.append((x, y))
- """Orders parsing"""
- op_torpedo = None
- if opponent_orders != 'NA':
- orders = opponent_orders.split('|')
- debug(orders)
- for order in orders:
- order = order.split()
- if order[0] == 'TORPEDO':
- op_torpedo = list(map(int, order[1:3]))
- debug(op_torpedo)
- else:
- phantom_dir = order[1]
- debug(f'{len(phantoms)} phantoms: {phantoms}')
- """Updates phantom possible positions"""
- for i in range(225):
- if i in phantoms:
- x, y = phantoms[i][-1]
- dx, dy = DIRECTIONS[phantom_dir]
- new_x, new_y = x + dx, y + dy
- if not (0 <= new_x < width and 0 <= new_y < width) or grid[new_y][new_x] != '.' or (op_torpedo and dist(x, y, *op_torpedo) > 4):
- del phantoms[i]
- continue
- phantoms[i].append((new_x, new_y))
- debug(orders)
- debug(f'{len(phantoms)} phantoms: {phantoms}')
- moves = get_directions(grid, x, y)
- if moves:
- move = random.choice(moves)
- print(f'MOVE {move} TORPEDO')
- else:
- print('SURFACE')
- visited.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement