Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_bunny_location(matrix, size):
- start_row = 0
- start_col = 0
- for row_idx in range(size):
- for col_idx in range(size):
- if matrix[row_idx][col_idx] == 'B':
- start_row = row_idx
- start_col = col_idx
- return start_row, start_col
- field_size = int(input())
- field = [[int(el) if el.isdigit() else str(el) for el in input().split()] for _ in range(field_size)]
- start_row_idx, start_col_idx = find_bunny_location(field, field_size)
- best_route = []
- most_eggs = -float('inf')
- best_direction = ''
- for direction in ['left', 'right', 'up', 'down']:
- if direction == 'left':
- bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
- current_eggs = 0
- visited = []
- bunny_col_idx -= 1
- while bunny_col_idx >= 0:
- if field[bunny_row_idx][bunny_col_idx] != 'X':
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- break
- bunny_col_idx -= 1
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'left'
- best_route = visited
- elif direction == 'right':
- bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
- current_eggs = 0
- visited = []
- bunny_col_idx += 1
- while bunny_col_idx < field_size:
- if field[bunny_row_idx][bunny_col_idx] != 'X':
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- break
- bunny_col_idx += 1
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'right'
- best_route = visited
- elif direction == 'up':
- bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
- current_eggs = 0
- visited = []
- bunny_row_idx -= 1
- while bunny_row_idx >= 0:
- if field[bunny_row_idx][bunny_col_idx] != 'X':
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- break
- bunny_row_idx -= 1
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'up'
- best_route = visited
- elif direction == 'down':
- bunny_row_idx, bunny_col_idx = start_row_idx, start_col_idx
- current_eggs = 0
- visited = []
- bunny_row_idx += 1
- while bunny_row_idx < field_size:
- if field[bunny_row_idx][bunny_col_idx] != 'X':
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- break
- bunny_row_idx += 1
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'down'
- best_route = visited
- print(best_direction)
- for element in best_route:
- print(element)
- print(most_eggs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement