Advertisement
Kamend1

04. Easter Bunny Kamen Dimitrov

Jan 21st, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 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. field_size = int(input())
  12. field = [[int(el) if el.isdigit() else str(el) for el in input().split()] for _ in range(field_size)]
  13.  
  14. start_row_idx, start_col_idx = find_bunny_location(field, field_size)
  15. best_route = []
  16. most_eggs = -float('inf')
  17. best_direction = ''
  18.  
  19.  
  20. for direction in ['left', 'right', 'up', 'down']:
  21.     if direction == 'left':
  22.         bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
  23.         current_eggs = 0
  24.         visited = []
  25.         bunny_col_idx -= 1
  26.  
  27.         while bunny_col_idx >= 0:
  28.             if field[bunny_row_idx][bunny_col_idx] != 'X':
  29.                 visited.append([bunny_row_idx, bunny_col_idx])
  30.                 current_eggs += field[bunny_row_idx][bunny_col_idx]
  31.             else:
  32.                 break
  33.             bunny_col_idx -= 1
  34.  
  35.         if current_eggs > most_eggs:
  36.             most_eggs = current_eggs
  37.             best_direction = 'left'
  38.             best_route = visited
  39.  
  40.     elif direction == 'right':
  41.         bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
  42.         current_eggs = 0
  43.         visited = []
  44.         bunny_col_idx += 1
  45.  
  46.         while bunny_col_idx < field_size:
  47.             if field[bunny_row_idx][bunny_col_idx] != 'X':
  48.                 visited.append([bunny_row_idx, bunny_col_idx])
  49.                 current_eggs += field[bunny_row_idx][bunny_col_idx]
  50.             else:
  51.                 break
  52.             bunny_col_idx += 1
  53.  
  54.         if current_eggs > most_eggs:
  55.             most_eggs = current_eggs
  56.             best_direction = 'right'
  57.             best_route = visited
  58.  
  59.     elif direction == 'up':
  60.         bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
  61.         current_eggs = 0
  62.         visited = []
  63.         bunny_row_idx -= 1
  64.  
  65.         while bunny_row_idx >= 0:
  66.             if field[bunny_row_idx][bunny_col_idx] != 'X':
  67.                 visited.append([bunny_row_idx, bunny_col_idx])
  68.                 current_eggs += field[bunny_row_idx][bunny_col_idx]
  69.             else:
  70.                 break
  71.             bunny_row_idx -= 1
  72.  
  73.         if current_eggs > most_eggs:
  74.             most_eggs = current_eggs
  75.             best_direction = 'up'
  76.             best_route = visited
  77.  
  78.     elif direction == 'down':
  79.         bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
  80.         current_eggs = 0
  81.         visited = []
  82.         bunny_row_idx += 1
  83.  
  84.         while bunny_row_idx < field_size:
  85.             if field[bunny_row_idx][bunny_col_idx] != 'X':
  86.                 visited.append([bunny_row_idx, bunny_col_idx])
  87.                 current_eggs += field[bunny_row_idx][bunny_col_idx]
  88.             else:
  89.                 break
  90.             bunny_row_idx += 1
  91.  
  92.         if current_eggs > most_eggs:
  93.             most_eggs = current_eggs
  94.             best_direction = 'down'
  95.             best_route = visited
  96.  
  97. print(best_direction)
  98. for element in best_route:
  99.     print(element)
  100. print(most_eggs)
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement