Siru's Department Store Tour
Time limit2sMemory limit1024 MB
Find the shortest path on a grid from Siru's start to any chair, avoiding pillars and cells within Manhattan distance K of mannequins.
- Level
Medium5 of 10
- Topics
- BFS, Matrix, Shortest path
- Solved
- No attempts yet
Problem
Siru went to a department store with her parents. Her parents have a lot of shopping to do and need to visit many places. Walking around with them is too tiring for Siru, so she wants to sit down on a chair and rest.
The department store is a grid with rows and columns. Each move to an adjacent cell up, down, left, or right costs 1 stamina. Siru starts at her current position and wants to walk to one of the chairs scattered around the store and sit down. Siru cannot leave the department store, because her parents would scold her.
The store has pillars that hold up the building and mannequins that display clothes. Siru cannot move into a cell with a pillar. She is afraid of mannequins, so she also avoids every cell whose distance to a mannequin is or less. The distance between cells and is . However, Siru sets out with her parents, so she can leave even if her starting cell is within distance of a mannequin.
Her legs hurt, so she wants to reach a chair while spending as little stamina as possible. Find the minimum stamina Siru spends.
Input
The first line contains the number of rows , the number of columns , and the distance that must be kept from mannequins, separated by spaces. (, )
The next lines each contain numbers, describing the store from top to bottom. An empty cell is 0, a pillar is 1, a chair is 2, a mannequin is 3, and Siru's starting position is 4. The starting position appears exactly once, and it holds no pillar, chair, or mannequin.
Output
If Siru can reach a chair, print the minimum stamina she spends. If she cannot reach any chair, print -1.
Hint
Python users are recommended to submit with PyPy.