NBLINDESC - Not So Blind Escape


Illidan Stormage, the blind Warcraft hero is thrown in a maze for punishment. Being a blind hero, he cannot see anything but he can sense which direction is North, East, South and West.

Assume that the maze is laid out on a N x M grid, and each grid location is either blocked or free. Illidan starts from a free location at time 0, and his goal is to escape from the maze. He has four possible moves - move to North, East, South or West. Making a move to an adjacent cell takes exactly 1 unit of time. If Illidan attempts to move out of the grid or attempts to move into an obstacle, he will stay in his current location but it will still cost him 1 unit of time. There is exactly one exit in the maze, located in one of the free cells. Illidan escapes immediately whenever he reaches the exit.

Keep in mind that Illidan is blind, so he may get confused while making a move. To avoid all this chaos, he decided to follow a rather simple strategy. Before he starts his journey to escape the maze, he selects four real numbers: PN, PE, PS and PW denoting the probability to move North, East, South and West respectively. No matter where he is, he will always attempt to move North with probability PN, East with probability PE, South with probability PS, and West with probability PW. At any moment, Illidan must attempt to make a move in one of these four directions. In other words, this means that PN + PE + PS + PW = 1 must be equal to 1.

Illidan does not want to be stuck in the maze for too long. He has information about the maze so he knows his starting position, the location of the exit cell and all the free cells and obstacles initially. Before he attempts to escape, he would like to set the values PN, PE, PS and PW such that the expected time for him to escape the maze is minimized.

Given the description of the maze, Illidan’s starting position and the location of the exit cell, calculate the minimum expected time for Illidan to escape. You can safely assume that Ilidan’s starting cell and the exit cell will always be connected, that is, it will be possible to reach the exit cell via some sequence of moves.

Input

The first line of the input contains an integer T, denoting the number of test cases. Then the description of T test cases follow. The first line of every test case will start with a blank line. The second line contains two space separated integers, N and M denoting the number of rows and the number of columns of the grid. Each of the next N lines will contain exactly M characters on each line describing the grid. The grid will only consist of the characters . @ # and *.

. indicates a free cell.

@ indicates Illidan’s starting position which is also a free cell and will occur exactly once.

# denotes an obstacle.

* denotes the location of the exit cell, is of course a free cell and will occur exactly once.

Constraints

  • 1 ≤ T ≤ 32
  • 1 ≤ N, M ≤ 8
  • Output

    For each test case, print the expected time for Illidan to escape the maze if he chooses the probabilities optimally in a single line. Error less than 10-6 will be ignored.

    Sample Input

    2
    
    1 2
    @*
    
    3 3
    @..
    #.#
    ..*
    

    Sample Output

    1.0000000000000
    16.360252929211
    

    Credits

    Problem name and story inspired by LightOJ 1426 - Blind Escape

    Or if you prefer a blinder (easier) version SPOJ BLINDESC - Blind Escape II


    hide comments
    [Rampage] Blue.Mary: 2019-11-18 04:29:44

    Is the error bound actually absolute or relative?

    -> Good question! In the onsite contest, it was either absolute or relative. Since the answer can't be less than 1, it was relative. Here, I used the generic judge which will ignore FP rounding up to 10^-6. I'm not exactly sure if that uses absolute or relative error, but looks like it only checks for absolute error.

    Btw, do you think it'd be better to relax the precision error more? I see that you got quite a few wrong answers before finally getting accepted.

    -> I've done several tests after I get AC. The largest answer I can get is about 3e3. If you use judge "Ignore FP Rounding up to 1e-6", the actually relative error bound is about 3e-10. This is somewhat tight when using double. (When I solve problems in other OJs, usually they use relative error bound 1e-6 or 1e-9.)

    Last edit: 2019-11-19 02:10:35

    Added by:sgtlaugh
    Date:2019-11-16
    Time limit:16s
    Source limit:50000B
    Memory limit:1536MB
    Cluster: Cube (Intel G860)
    Languages:All
    Resource:Own Problem, Used in 2019-2020 ACM-ICPC Asia Bangkok Regional Contest