Under construction

Still being built. Things may change

Maze Stretching

Time limit1sMemory limit128 MB

Summary
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 XX 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 LL. 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 PP is the length of a single vertical step expressed as a percentage: a vertical step covers a distance of P/100P / 100 (so X=P/100X = P / 100). Find the percentage PP for which the shortest path of the maze becomes exactly LL.

Input

The first line contains the number of test cases. Each test case begins with a line containing two numbers LL and NN, where LL is the required length of the shortest path and NN is the number of lines that describe the maze. The following NN 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. PP must have a leading zero when its value is between 0 and 1. PP 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%.

Examples3

  1. Example 1

    Input
    2
    2.5 4
    #####
    #S  #
    #  E#
    #####
    21 13
    ############
    #S##     #E#
    # ##  #  # #
    #   # #  # #
    ### # #  # #
    #   # #  # #
    #  ## #  # #
    ##  # #  # #
    ### # #  # #
    ##  # #  # #
    #  ## #    #
    #     #    #
    ############
    
    Expected output
    Case #1: 50.000%
    Case #2: 21.053%
    
  2. Example 2

    Input
    1
    5 6
    #######
    #  #E##
    ##    #
    #   # #
    # S   #
    #######
    
    Expected output
    Case #1: 100.000%
    
  3. Example 3

    Input
    1
    9.28 8
    #########
    # ###   #
    ##E##   #
    ##  S  ##
    #    #  #
    #  #  # #
    ####    #
    #########
    
    Expected output
    Case #1: 728.000%