Monster Path (Large)

Walk exactly S steps on a grid, catching each cell's monster with probability P or Q on first visit, to maximize the expected number caught.

Hard8Dynamic programmingBit manipulationGraphMathNo attempts yetTime limit5sMemory limit512 MB

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 runs out quickly, so you have to choose your path carefully to catch as many monsters as possible.

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

Whenever you step into a cell in which you have not yet caught the monster, you try to catch it. You succeed with probability PP if the cell has a monster attractor, and with probability QQ otherwise. A monster that you catch goes away, so you cannot catch another monster in that cell, even on later visits. If you fail, you can try again the next time you step into that cell. The starting cell is special: you have no chance of catching its monster before your first step.

You plan your path optimally before you make any move. Find the maximum possible expected number of monsters that you catch.

If R=1R = 1 and C=1C = 1, there is no cell to step into. In that case the expected number is 00, even when SS is 11 or more.

Input

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

Each test case starts with a line of five space-separated integers RR, CC, RsR_s, CsC_s, and SS. RR and CC are the numbers of rows and columns in the grid, RsR_s and CsC_s are 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 the monster in a cell with a monster attractor, and QQ is the probability of catching the monster in a cell without one. Each value is given to exactly four decimal places.

Each of the next RR lines contains CC space-separated characters. The jj-th character of the ii-th line describes the cell at row ii and column jj. Each character is either ., meaning the cell has no attractor, or A, meaning the cell has an attractor.

Limits

  • 1T1001 \le T \le 100
  • 1R201 \le R \le 20
  • 1C201 \le C \le 20
  • 0Rs<R0 \le R_s < R
  • 0Cs<C0 \le C_s < C
  • 0Q<P10 \le Q < P \le 1
  • 0S90 \le S \le 9

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 11 and yy is the maximum possible expected number of monsters that you catch.

Print yy rounded half up to exactly seven digits after the decimal point. Print all seven digits even when the value is an integer, as in 2.0000000. No input has an answer that lies exactly on a rounding boundary.

Explanation

In the first test case of the first example, one optimal path is (0,0)(0,1)(0,2)(1,2)(2,2)(2,3)(0,0) \to (0,1) \to (0,2) \to (1,2) \to (2,2) \to (2,3). On this path, the expected number of monsters that you catch is 0.2+0.2+0.2+0.8+0.2=1.60.2 + 0.2 + 0.2 + 0.8 + 0.2 = 1.6. You have no chance of catching a monster before your first step, which is why the sum has five probabilities, not six.

In the second test case of the same example, one optimal path is (9,1)(9,2)(8,2)(8,3)(8,2)(9,1) \to (9,2) \to (8,2) \to (8,3) \to (8,2). The expected number is 0.1+0.6121+0.1+0.23743359=1.049533590.1 + 0.6121 + 0.1 + 0.23743359 = 1.04953359, which rounds to 1.04953361.0495336 in the required format. The last term, 0.237433590.23743359, is the probability 10.61211 - 0.6121 that the monster in (8,2)(8,2) is still there on your second visit, multiplied by the catch probability 0.61210.6121.