Walking to a friend in the plaza
Write code · Medium · §9.3 Nearest first
The map of a plaza in a metaverse social network is given as the grid plaza. '.' is a cell an avatar can walk on, '#' is a wall, 'A' is the cell where my avatar stands, and 'B' is the cell where my friend's avatar stands. In one step, an avatar moves to an adjacent cell above, below, to the left, or to the right. It cannot move into a wall or off the map.
Complete the function solution, which returns the smallest number of steps my avatar needs to reach my friend's cell. If my avatar cannot reach my friend, return -1.
Function
solution(plaza)
Constraints
- The length of
plazais from 1 to 20. - The strings in
plazaall have the same length, which is from 1 to 20. - The strings in
plazaconsist only of'.','#','A', and'B', and'A'and'B'each appear exactly once in the whole map.
Examples
| plaza | result |
|---|---|
['#####', '#A..#', '#.#.#', '#..B#', '#####'] | 4 |
['#####', '#A#B#', '#.#.#', '#...#', '#####'] | 6 |
Starter code
def solution(plaza):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.