Under construction

Still being built. Things may change

Monster Path (Small)

Time limit5sMemory limit512 MB

Summary
Given a small grid, a start cell, and a fixed step count, choose a walk maximizing the expected number of distinct monsters caught.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation, Brute force
Solved
No attempts yet

Problem

Codejamon is a mobile game in which monster trainers walk around the real world to catch monsters. Your smartphone is old and its battery does not last long, so you have to choose your path carefully to catch as many monsters as possible.

The Codejamon world is a rectangular grid with RR rows and CC columns. Rows are numbered from top to bottom starting at 0, and columns are numbered from left to right starting at 0. You start in the cell in row RsR_s and column CsC_s, and you take exactly SS unit steps. One step moves you to a cell that shares an edge with your current cell. A cell that only shares a corner is not reachable in one step.

Whenever you step into a cell where you have not caught a monster yet, you catch the monster in that cell with probability PP if the cell has a monster attractor, and with probability QQ if it does not. Once you catch the monster in a cell, it goes away, and later visits to that cell give you nothing. If you fail, you try again on every later visit to that cell. The starting cell is an exception. You have no chance of catching a monster there before your first step.

You plan the whole path before you move. Find the largest possible expected number of monsters that you catch.

Input

The first line contains the number of test cases TT. TT test cases follow.

Each test case starts with a line of five integers RR, CC, RsR_s, CsC_s and SS. RR and CC are the numbers of rows and columns of the grid, (RsR_s, CsC_s) is the row and column of your starting cell, and SS is the number of steps you take.

The next line contains two decimals PP and QQ. PP is the probability of catching a monster in a cell with an attractor, and QQ is the probability in a cell without one. Both are given with exactly four digits after the decimal point.

Each of the next RR lines contains CC characters separated by spaces. The jj-th character of the ii-th line describes the cell in row ii and column jj. It is . if the cell has no attractor, and A if it has one.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤R≤101 \le R \le 10
  • 1≤C≤101 \le C \le 10
  • 0≤Rs<R0 \le R_s < R
  • 0≤Cs<C0 \le C_s < C
  • 0≤Q<P≤10 \le Q < P \le 1
  • 0≤S≤50 \le S \le 5
  • If R=1R = 1 and C=1C = 1, then S=0S = 0.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting at 1 and yy is the largest expected number of monsters you can catch within the given number of steps.

Print yy with exactly seven digits after the decimal point, rounded at the eighth decimal digit. A digit 5 in the eighth decimal place rounds the seventh place up. Print a zero answer as 0.0000000.

Notes

In the first test case of the example, one optimal path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3). The expected number of monsters caught on it is 0.2 + 0.2 + 0.2 + 0.8 + 0.2 = 1.6. There is no chance to catch a monster before the first step, so five probabilities are added, not six.

In the second test case, one optimal path is (9,1) -> (9,2) -> (8,2) -> (8,3) -> (8,2). The expected value is 0.1 + 0.6121 + 0.1 + 0.23743359 = 1.04953359, which prints as 1.0495336. The term 0.23743359 for the second entry into (8,2) is 0.3879, the probability that the first attempt failed, times 0.6121.

Examples1

  1. Example 1

    Input
    2
    4 4 0 0 5
    0.8000 0.2000
    . . . .
    . . . .
    . . A .
    . A . A
    10 10 9 1 4
    0.6121 0.1000
    . . A A . . . . . .
    A . . . . . . . . .
    . . A . . . . A . .
    . . . A A . . . . .
    . A A A . . . . . A
    A . A A . . . . A .
    . A . . . . . A . .
    . . . . A A . . . .
    . . A . . . A . . A
    . . . . A . . A . .
    
    Expected output
    Case #1: 1.6000000
    Case #2: 1.0495336