Maze Stretching
Time limit1sMemory limit128 MB
Given a maze and target shortest-path length L, find the vertical stretch factor X so that the minimum-cost path (with horizontal steps costing 1 and vertical steps costing X) equals L exactly, using binary search over X combined with shortest-path computation.
- Level
Medium7 of 10
- Topics
- Binary search, Shortest path, Graph
- Solved
- No attempts yet
Problem
Normally a path through a maze is measured as the number of steps taken from the start to the end, where each step covers a distance of exactly 1. Suppose instead that we may stretch the maze along its vertical dimension (north-south), either shortening or extending it. Stretching only changes the distance covered by a step between two vertically adjacent cells: such a step now covers a distance of instead of 1. Horizontal steps (east-west) always cover a distance of 1.
The maze is a two-dimensional grid where # marks a wall, S marks the starting cell, and E marks the ending cell; every other cell is open. You may move between orthogonally adjacent open cells.
Because of outside conditions, the shortest path from S to E must be made to have length exactly . You may not change the layout of the maze, and you may not change anything in the horizontal dimension. You may only stretch the vertical dimension, by any percentage.
The stretch percentage is the length of a single vertical step expressed as a percentage: a vertical step covers a distance of (so ). Find the percentage for which the shortest path of the maze becomes exactly .
Input
The first line contains the number of test cases. Each test case begins with a line containing two numbers and , where is the required length of the shortest path and is the number of lines that describe the maze. The following lines describe the maze, one row per line. The length of each row is the horizontal dimension of the maze.
Output
For each test case, output the stretch percentage in the following format:
Case #K: P%
where K is the test case number, starting from 1. must have a leading zero when its value is between 0 and 1. is rounded to 3 decimal places and always printed with exactly 3 decimal places (adding trailing zeros when needed).
Constraints
The height and width of the maze are each at most 100 cells.
All lines describing a single maze have the same length.
There is always a solution.
The result is between 0.000 and 1000.000, inclusive.
There is never a path connecting S and E that moves only horizontally, so the result is always unique.
Hint
Consider a maze whose shortest path takes two horizontal steps and one vertical step, giving a natural length of 3. To make the shortest path have length 2.5, the single vertical step must cover a distance of 0.5, which corresponds to stretching the vertical dimension to 50%.