Advertisement
mbstanchev

range_day

Jan 30th, 2023
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. n = 5
  2. matrix = []
  3. all_targets = 0
  4. hit_targets = 0
  5. hit_pos = []
  6. shooter_pos = ()
  7. directions = {
  8.     "up": (-1, 0),
  9.     "down": (1, 0),
  10.     "left": (0, -1),
  11.     "right": (0, 1),
  12. }
  13. for r in range(n):
  14.     matrix.append(input().split())
  15.     for c in range(n):
  16.         if matrix[r][c] == 'A':
  17.             shooter_pos = (r, c)
  18.         if matrix[r][c] == 'x':
  19.             all_targets += 1
  20.  
  21. number_of_commands = int(input())
  22. for command in range(number_of_commands):
  23.     action = input().split()
  24.     if action[0] == 'move':
  25.         direction = action[1]
  26.         steps = int(action[2])
  27.         next_pos_row = (shooter_pos[0] + (directions[direction][0] * steps))
  28.         next_pos_col = (shooter_pos[1] + (directions[direction][1] * steps))
  29.         if not (0 <= next_pos_row < len(matrix) and 0 <= next_pos_col < len(matrix)):
  30.             continue
  31.         if matrix[next_pos_row][next_pos_col] == 'x':
  32.             continue
  33.         shooter_pos = (next_pos_row, next_pos_col)
  34.     elif action[0] == 'shoot':
  35.         shoot_direction = action[1]
  36.         shooting_row = shooter_pos[0] + directions[shoot_direction][0]
  37.         shooting_col = shooter_pos[1] + directions[shoot_direction][1]
  38.         while 0 <= shooting_row < len(matrix) and 0 <= shooting_col < len(matrix):
  39.  
  40.             if not matrix[shooting_row][shooting_col] == 'x':
  41.                 shooting_row += directions[shoot_direction][0]
  42.                 shooting_col += directions[shoot_direction][1]
  43.                 continue
  44.             hit_targets += 1
  45.             hit_pos.append([shooting_row, shooting_col])
  46.             matrix[shooting_row][shooting_col] = '.'
  47.  
  48.             break
  49. if all_targets != hit_targets:
  50.     print(f"Training not completed! {all_targets - hit_targets} targets left.")
  51.     print(*hit_pos, sep='\n')
  52. else:
  53.     print(f'Training completed! All {hit_targets} targets hit.')
  54.     print(*hit_pos, sep='\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement