Advertisement
GeorgiLukanov87

04. Easter Bunny - Multidimensional Lists - Exercise 2

Sep 2nd, 2022 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. # 04. Easter Bunny
  2.  
  3. # Multidimensional Lists - Exercise 2
  4. # https://judge.softuni.org/Contests/Practice/Index/3194#3
  5.  
  6.  
  7.  
  8. def go_left_func(row, col, matrix):  # LEFT
  9.     position = 1
  10.     total_eggs = 0
  11.     step_indexes = []
  12.     while is_inside_func(row, col - position, matrix):
  13.         if matrix[row][col - position] != "X":
  14.             total_eggs += int(matrix[row][col - position])
  15.             step_indexes.append([row, col - position])
  16.         else:
  17.             # found X trap
  18.             break
  19.         position += 1
  20.     return total_eggs, step_indexes
  21.  
  22.  
  23. def go_right_func(row, col, matrix):  # RIGHT
  24.     position = 1
  25.     total_eggs = 0
  26.     step_indexes = []
  27.     while is_inside_func(row, col + position, matrix):
  28.         if matrix[row][col + position] != "X":
  29.             total_eggs += int(matrix[row][col + position])
  30.             step_indexes.append([row, col + position])
  31.         else:
  32.             # found X trap
  33.             break
  34.         position += 1
  35.     return total_eggs, step_indexes
  36.  
  37.  
  38. def go_up_func(row, col, matrix):  # UP
  39.     position = 1
  40.     total_eggs = 0
  41.     step_indexes = []
  42.     while is_inside_func(row - position, col, matrix):
  43.         if matrix[row - position][col] != "X":
  44.             total_eggs += int(matrix[row - position][col])
  45.             step_indexes.append([row - position, col])
  46.         else:
  47.             # found X trap
  48.             break
  49.         position += 1
  50.     return total_eggs, step_indexes
  51.  
  52.  
  53. def go_down_func(row, col, matrix):  # DOWN
  54.     position = 1
  55.     total_eggs = 0
  56.     step_indexes = []
  57.     while is_inside_func(row + position, col, matrix):
  58.         if matrix[row + position][col] != "X":
  59.             total_eggs += int(matrix[row + position][col])
  60.             step_indexes.append([row + position, col])
  61.         else:
  62.             # found X trap
  63.             break
  64.         position += 1
  65.     return total_eggs, step_indexes
  66.  
  67.  
  68. def find_bunny_start_position_func(matrix):
  69.     for row_index in range(len(matrix)):
  70.         for col_index in range(len(matrix)):
  71.             if matrix[row_index][col_index] == 'B':
  72.                 return row_index, col_index
  73.  
  74.  
  75. def is_inside_func(current_row, current_col, default_M_size):
  76.     return 0 <= current_row < len(default_M_size) and 0 <= current_col < len(default_M_size)
  77.  
  78.  
  79. size = int(input())
  80. matrix = []
  81. for _ in range(size):
  82.     current_line = input().split()
  83.     matrix.append(current_line)
  84.  
  85. row, col = find_bunny_start_position_func(matrix)  # START POSITION (3 , 0)
  86.  
  87. direction = ""
  88. all_steps = []
  89. max_eggs_sum = -9999999999
  90.  
  91. #################################
  92. #                               #
  93. #     1   3   7    9   11       #
  94. #     X   5   4    X   63       #
  95. #     7   3   21   95  1        #
  96. #     B   1   73   4   9        #
  97. #     9   2   33   2   0        #
  98. #                               #
  99. #################################
  100.  
  101. left_result, steps = go_left_func(row, col, matrix)
  102. if left_result > max_eggs_sum and steps:
  103.     max_eggs_sum = left_result
  104.     direction = 'left'
  105.     all_steps = steps
  106.  
  107. right_result, steps = go_right_func(row, col, matrix)
  108. if right_result > max_eggs_sum and steps:
  109.     max_eggs_sum = right_result
  110.     direction = 'right'
  111.     all_steps = steps
  112.  
  113. up_result, steps = go_up_func(row, col, matrix)
  114. if up_result > max_eggs_sum and steps:
  115.     max_eggs_sum = up_result
  116.     direction = 'up'
  117.     all_steps = steps
  118.  
  119. down_result, steps = go_down_func(row, col, matrix)
  120. if down_result > max_eggs_sum and steps:
  121.     max_eggs_sum = down_result
  122.     direction = 'down'
  123.     all_steps = steps
  124.  
  125. print(direction)
  126. for step in all_steps:
  127.     print(step)
  128. print(max_eggs_sum)
  129.  
  130.  
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement