Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 06. Range Day
- # Multidimensional Lists - Exercise 2
- # https://judge.softuni.org/Contests/Practice/Index/3194#5
- def is_inside_func(row, col, matrix):
- return 0 <= row < len(matrix) and 0 <= col < len(matrix)
- def shoot_func(row, col, matrix, direction):
- if direction == 'down':
- while is_inside_func(row + 1, col, matrix):
- if matrix[row + 1][col] == 'x':
- targets_down.append([row + 1, col])
- matrix[row + 1][col] = '.'
- break
- else:
- row += 1
- elif direction == 'up':
- while is_inside_func(row - 1, col, matrix):
- if matrix[row - 1][col] == 'x':
- targets_down.append([row - 1, col])
- matrix[row - 1][col] = '.'
- break
- else:
- row -= 1
- elif direction == 'left':
- while is_inside_func(row, col - 1, matrix):
- if matrix[row][col - 1] == 'x':
- targets_down.append([row, col - 1])
- matrix[row][col - 1] = '.'
- break
- else:
- col -= 1
- elif direction == 'right':
- while is_inside_func(row, col + 1, matrix):
- if matrix[row][col + 1] == 'x':
- targets_down.append([row, col + 1])
- matrix[row][col + 1] = '.'
- break
- else:
- col += 1
- def move_func(row, col, matrix, direction, steps):
- last_position = (row, col)
- if direction == 'right':
- if is_inside_func(row, col + steps, matrix) and matrix[row][col + steps] == '.':
- matrix[row][col] = '.'
- matrix[row][col + steps] = 'A'
- last_position = (row, col + steps)
- else:
- return last_position
- elif direction == 'left':
- if is_inside_func(row, col - steps, matrix) and matrix[row][col - steps] == '.':
- matrix[row][col] = '.'
- matrix[row][col - steps] = 'A'
- last_position = (row, col - steps)
- else:
- return last_position
- elif direction == 'up':
- if is_inside_func(row - steps, col, matrix) and matrix[row - steps][col] == '.':
- matrix[row - steps][col] = '.'
- matrix[row - steps][col] = 'A'
- last_position = (row - steps, col)
- else:
- return last_position
- elif direction == 'down':
- if is_inside_func(row + steps, col, matrix) and matrix[row + steps][col] == '.':
- matrix[row + steps][col] = '.'
- matrix[row + steps][col] = 'A'
- last_position = (row + steps, col)
- else:
- return last_position
- return last_position
- start_row = 0
- start_col = 0
- matrix = []
- targets = []
- targets_down = []
- for row in range(5):
- matrix.append(input().split())
- for col in range(5):
- if matrix[row][col] == 'A': # Finding start position
- start_row = row
- start_col = col
- elif matrix[row][col] == 'x': # Finding shoot targets
- targets.append([row, col])
- n_shoots = int(input())
- for _ in range(n_shoots):
- command = input().split()
- direction = command[1]
- if command[0] == 'shoot':
- shoot_func(start_row, start_col, matrix, direction)
- elif command[0] == 'move':
- steps = int(command[2])
- last_position = move_func(start_row, start_col, matrix, direction, steps)
- start_row = last_position[0]
- start_col = last_position[1]
- if len(targets_down) == len(targets):
- break
- if len(targets_down) == len(targets):
- print(f'Training completed! All {len(targets)} targets hit.')
- else:
- print(f'Training not completed! {len(targets) - len(targets_down)} targets left.')
- if targets_down:
- for target in targets_down:
- print(target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement