Running
InterviewTime limit1sMemory limit512 MB
On a grid with walls, each move slides 1 to K empty cells in one of four directions; find the minimum number of moves from start to goal.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Array, Shortest path
- Solved
- No attempts yet
Problem
Jinyoung wants to run in a gym of size to lose weight. The gym is divided into cells, and each cell is either empty or a wall. The cell in row , column is denoted .
Every second, Jinyoung chooses one direction among up, down, right, and left, and moves at least 1 and at most empty cells in that direction.
Given the start point and the end point , find the minimum time to move from the start point to the end point.
Input
The first line gives the size of the gym and , and the maximum number of cells that can be moved in 1 second.
From the second line, lines give the state of the gym. Each cell of the gym is either empty or a wall, where an empty cell is given as '.', and a wall is given as '#'.
The last line gives four integers , , , . The two cells are different cells and are always empty.
Output
Print the minimum time to move from to . If it is impossible to move, print -1.