Escape the Maze with a Drill
InterviewTime limit1sMemory limit128 MB
Decide whether a robot can reach the target on a grid by drilling through at most k walls.
- Level
Medium4 of 10
- Topics
- Shortest path, BFS, Graph, Matrix
- Solved
- No attempts yet
Problem
A robot is trapped in a maze shaped as an grid. Each cell is either empty or a wall. The robot cannot step onto a wall directly, and it can never leave the maze because the outer boundary is solid. The robot does carry a drill, however: spending unit of energy turns one wall cell into an empty cell, after which the robot may enter it.
The robot starts at a given cell and wants to reach a destination cell. In one step it moves to a cell that is horizontally or vertically adjacent. Entering an empty cell is free, but entering a wall cell requires first drilling that wall, which costs unit of energy. The robot has a limited amount of stored energy.
Given the map of the maze and the amount of stored energy, decide whether the robot can reach the destination.
Input
The first line contains an integer (), the number of test cases.
Each test case is given as follows. The first line contains an integer (), the amount of energy stored in the robot. The next line contains two integers and (, ), the number of rows and columns of the maze. Each of the following lines contains exactly characters describing one row of the maze. The character * is a wall and . is an empty cell. S marks the robot start and T marks the destination. Every symbol on a maze row is separated from the next by at least one whitespace character.
Output
For each test case, print a single line containing y if the robot can reach the destination while spending at most units of energy, or n otherwise.