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)]
- best_route = []
- most_eggs = 0
- best_direction = ''
- valid_direction = True
- for direction in ['left', 'right', 'up', 'down']:
- if direction == 'left':
- bunny_row_idx, bunny_col_idx = find_bunny_location(field, field_size)
- valid_direction = True
- current_eggs = 0
- visited = []
- while valid_direction:
- bunny_col_idx -= 1
- if bunny_col_idx >= 0:
- if field[bunny_row_idx][bunny_col_idx] == 'X':
- valid_direction = False
- else:
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- valid_direction = False
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'left'
- best_route = visited
- else:
- visited = []
- elif direction == 'right':
- bunny_row_idx, bunny_col_idx = find_bunny_location(field, field_size)
- valid_direction = True
- current_eggs = 0
- visited = []
- while valid_direction:
- bunny_col_idx += 1
- if bunny_col_idx < field_size:
- if field[bunny_row_idx][bunny_col_idx] == 'X':
- valid_direction = False
- else:
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- valid_direction = False
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'right'
- best_route = visited
- else:
- visited = []
- elif direction == 'up':
- bunny_row_idx, bunny_col_idx = find_bunny_location(field, field_size)
- valid_direction = True
- current_eggs = 0
- visited = []
- while valid_direction:
- bunny_row_idx -= 1
- if bunny_row_idx >= 0:
- if field[bunny_row_idx][bunny_col_idx] == 'X':
- valid_direction = False
- else:
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- valid_direction = False
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'up'
- best_route = visited
- else:
- visited = []
- elif direction == 'down':
- bunny_row_idx, bunny_col_idx = find_bunny_location(field, field_size)
- valid_direction = True
- current_eggs = 0
- visited = []
- while valid_direction:
- bunny_row_idx += 1
- if bunny_row_idx < field_size:
- if field[bunny_row_idx][bunny_col_idx] == 'X':
- valid_direction = False
- else:
- visited.append([bunny_row_idx, bunny_col_idx])
- current_eggs += field[bunny_row_idx][bunny_col_idx]
- else:
- valid_direction = False
- if current_eggs > most_eggs:
- most_eggs = current_eggs
- best_direction = 'down'
- best_route = visited
- else:
- visited = []
- print(best_direction)
- for element in best_route:
- print(element)
- print(most_eggs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement