Minigolf
InterviewTime limit4sMemory limit1024 MB
On a grid with walls, a ball can be putted 1 to K cells in a straight line; find the fewest putts to reach the hole.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Array, Implementation
- Solved
- No attempts yet
Problem
You are playing minigolf on an grid. In one stroke you can putt the ball any number of steps up to straight in one of the four directions: up, down, right, left. You cannot putt the ball through a wall or off the course, of course.
Your task is to compute the minimum number of putts needed to get the ball into the hole.
Input
The first line contains three integers , and ( and ): the number of rows, the number of columns, and how far you can putt.
Then follow lines, each with characters, describing the minigolf course:
- "
." means the cell is empty. - "
#" means the cell contains a wall. - "
S" means this cell is the one you start from. There is exactly one "S" in the input. - "
G" means this cell contains the hole. There is exactly one "G" in the input.
It is guaranteed that the hole can be reached from the starting cell.
Output
Print one integer: the minimum number of putts you need to shoot the ball into the hole.