Advertisement
Kamend1

05. Alice in Wonderland

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