Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 04. Easter Bunny
- # Multidimensional Lists - Exercise 2
- # https://judge.softuni.org/Contests/Practice/Index/3194#3
- def go_left_func(row, col, matrix): # LEFT
- position = 1
- total_eggs = 0
- step_indexes = []
- while is_inside_func(row, col - position, matrix):
- if matrix[row][col - position] != "X":
- total_eggs += int(matrix[row][col - position])
- step_indexes.append([row, col - position])
- else:
- # found X trap
- break
- position += 1
- return total_eggs, step_indexes
- def go_right_func(row, col, matrix): # RIGHT
- position = 1
- total_eggs = 0
- step_indexes = []
- while is_inside_func(row, col + position, matrix):
- if matrix[row][col + position] != "X":
- total_eggs += int(matrix[row][col + position])
- step_indexes.append([row, col + position])
- else:
- # found X trap
- break
- position += 1
- return total_eggs, step_indexes
- def go_up_func(row, col, matrix): # UP
- position = 1
- total_eggs = 0
- step_indexes = []
- while is_inside_func(row - position, col, matrix):
- if matrix[row - position][col] != "X":
- total_eggs += int(matrix[row - position][col])
- step_indexes.append([row - position, col])
- else:
- # found X trap
- break
- position += 1
- return total_eggs, step_indexes
- def go_down_func(row, col, matrix): # DOWN
- position = 1
- total_eggs = 0
- step_indexes = []
- while is_inside_func(row + position, col, matrix):
- if matrix[row + position][col] != "X":
- total_eggs += int(matrix[row + position][col])
- step_indexes.append([row + position, col])
- else:
- # found X trap
- break
- position += 1
- return total_eggs, step_indexes
- def find_bunny_start_position_func(matrix):
- for row_index in range(len(matrix)):
- for col_index in range(len(matrix)):
- if matrix[row_index][col_index] == 'B':
- return row_index, col_index
- def is_inside_func(current_row, current_col, default_M_size):
- return 0 <= current_row < len(default_M_size) and 0 <= current_col < len(default_M_size)
- size = int(input())
- matrix = []
- for _ in range(size):
- current_line = input().split()
- matrix.append(current_line)
- row, col = find_bunny_start_position_func(matrix) # START POSITION (3 , 0)
- direction = ""
- all_steps = []
- max_eggs_sum = -9999999999
- #################################
- # #
- # 1 3 7 9 11 #
- # X 5 4 X 63 #
- # 7 3 21 95 1 #
- # B 1 73 4 9 #
- # 9 2 33 2 0 #
- # #
- #################################
- left_result, steps = go_left_func(row, col, matrix)
- if left_result > max_eggs_sum and steps:
- max_eggs_sum = left_result
- direction = 'left'
- all_steps = steps
- right_result, steps = go_right_func(row, col, matrix)
- if right_result > max_eggs_sum and steps:
- max_eggs_sum = right_result
- direction = 'right'
- all_steps = steps
- up_result, steps = go_up_func(row, col, matrix)
- if up_result > max_eggs_sum and steps:
- max_eggs_sum = up_result
- direction = 'up'
- all_steps = steps
- down_result, steps = go_down_func(row, col, matrix)
- if down_result > max_eggs_sum and steps:
- max_eggs_sum = down_result
- direction = 'down'
- all_steps = steps
- print(direction)
- for step in all_steps:
- print(step)
- print(max_eggs_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement