Advertisement
GeorgiLukanov87

06. Range Day - Multidimensional Lists - Exercise 2

Sep 3rd, 2022
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. # 06. Range Day
  2.  
  3. # Multidimensional Lists - Exercise 2
  4. # https://judge.softuni.org/Contests/Practice/Index/3194#5
  5.  
  6.  
  7. def is_inside_func(row, col, matrix):
  8.     return 0 <= row < len(matrix) and 0 <= col < len(matrix)
  9.  
  10.  
  11. def shoot_func(row, col, matrix, direction):
  12.     if direction == 'down':
  13.         while is_inside_func(row + 1, col, matrix):
  14.             if matrix[row + 1][col] == 'x':
  15.                 targets_down.append([row + 1, col])
  16.                 matrix[row + 1][col] = '.'
  17.                 break
  18.             else:
  19.                 row += 1
  20.  
  21.     elif direction == 'up':
  22.         while is_inside_func(row - 1, col, matrix):
  23.             if matrix[row - 1][col] == 'x':
  24.                 targets_down.append([row - 1, col])
  25.                 matrix[row - 1][col] = '.'
  26.                 break
  27.             else:
  28.                 row -= 1
  29.  
  30.     elif direction == 'left':
  31.         while is_inside_func(row, col - 1, matrix):
  32.             if matrix[row][col - 1] == 'x':
  33.                 targets_down.append([row, col - 1])
  34.                 matrix[row][col - 1] = '.'
  35.                 break
  36.             else:
  37.                 col -= 1
  38.  
  39.     elif direction == 'right':
  40.         while is_inside_func(row, col + 1, matrix):
  41.             if matrix[row][col + 1] == 'x':
  42.                 targets_down.append([row, col + 1])
  43.                 matrix[row][col + 1] = '.'
  44.                 break
  45.             else:
  46.                 col += 1
  47.  
  48.  
  49. def move_func(row, col, matrix, direction, steps):
  50.     last_position = (row, col)
  51.  
  52.     if direction == 'right':
  53.         if is_inside_func(row, col + steps, matrix) and matrix[row][col + steps] == '.':
  54.             matrix[row][col] = '.'
  55.             matrix[row][col + steps] = 'A'
  56.             last_position = (row, col + steps)
  57.         else:
  58.             return last_position
  59.  
  60.     elif direction == 'left':
  61.         if is_inside_func(row, col - steps, matrix) and matrix[row][col - steps] == '.':
  62.             matrix[row][col] = '.'
  63.             matrix[row][col - steps] = 'A'
  64.             last_position = (row, col - steps)
  65.         else:
  66.             return last_position
  67.  
  68.     elif direction == 'up':
  69.         if is_inside_func(row - steps, col, matrix) and matrix[row - steps][col] == '.':
  70.             matrix[row - steps][col] = '.'
  71.             matrix[row - steps][col] = 'A'
  72.             last_position = (row - steps, col)
  73.         else:
  74.             return last_position
  75.  
  76.     elif direction == 'down':
  77.         if is_inside_func(row + steps, col, matrix) and matrix[row + steps][col] == '.':
  78.             matrix[row + steps][col] = '.'
  79.             matrix[row + steps][col] = 'A'
  80.             last_position = (row + steps, col)
  81.         else:
  82.             return last_position
  83.  
  84.     return last_position
  85.  
  86.  
  87. start_row = 0
  88. start_col = 0
  89. matrix = []
  90. targets = []
  91.  
  92. targets_down = []
  93. for row in range(5):
  94.     matrix.append(input().split())
  95.  
  96.     for col in range(5):
  97.         if matrix[row][col] == 'A':  # Finding start position
  98.             start_row = row
  99.             start_col = col
  100.  
  101.         elif matrix[row][col] == 'x':  # Finding shoot targets
  102.             targets.append([row, col])
  103.  
  104. n_shoots = int(input())
  105.  
  106. for _ in range(n_shoots):
  107.     command = input().split()
  108.     direction = command[1]
  109.  
  110.     if command[0] == 'shoot':
  111.         shoot_func(start_row, start_col, matrix, direction)
  112.  
  113.     elif command[0] == 'move':
  114.         steps = int(command[2])
  115.         last_position = move_func(start_row, start_col, matrix, direction, steps)
  116.         start_row = last_position[0]
  117.         start_col = last_position[1]
  118.  
  119.     if len(targets_down) == len(targets):
  120.         break
  121.  
  122. if len(targets_down) == len(targets):
  123.     print(f'Training completed! All {len(targets)} targets hit.')
  124. else:
  125.     print(f'Training not completed! {len(targets) - len(targets_down)} targets left.')
  126.  
  127. if targets_down:
  128.     for target in targets_down:
  129.         print(target)
  130.  
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement