Advertisement
Kamend1

Again 04_Easter_Bunny

Jan 22nd, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. def find_bunny_location(matrix, size):
  2.     start_row = 0
  3.     start_col = 0
  4.     for row_idx in range(size):
  5.         for col_idx in range(size):
  6.             if matrix[row_idx][col_idx] == 'B':
  7.                 start_row = row_idx
  8.                 start_col = col_idx
  9.     return start_row, start_col
  10.  
  11.  
  12. field_size = int(input())
  13. field = [input().split() for _ in range(field_size)]
  14.  
  15. start_row_idx, start_col_idx = find_bunny_location(field, field_size)
  16. best_route = []
  17. most_eggs = -float('inf')
  18. best_direction = ''
  19.  
  20. directions = {
  21.     'left': [0, -1],
  22.     'right': [0, +1],
  23.     'up': [-1, 0],
  24.     'down': [+1, 0],
  25. }
  26.  
  27. for direction in directions.keys():
  28.     bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
  29.     field[bunny_row_idx][bunny_col_idx] = 0
  30.     current_eggs = 0
  31.     visited = []
  32.  
  33.     while True:
  34.         bunny_row_idx += directions[direction][0]
  35.         bunny_col_idx += directions[direction][1]
  36.         if 0 <= bunny_row_idx < field_size and 0 <= bunny_col_idx < field_size \
  37.                 and field[bunny_row_idx][bunny_col_idx] != 'X':
  38.             visited.append([bunny_row_idx, bunny_col_idx])
  39.             current_eggs += int(field[bunny_row_idx][bunny_col_idx])
  40.         else:
  41.             break
  42.  
  43.     if current_eggs > most_eggs:
  44.         most_eggs = current_eggs
  45.         best_direction = direction
  46.         best_route = visited
  47.  
  48. print(best_direction)
  49. for element in best_route:
  50.     print(element)
  51. print(most_eggs)
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement