#P2489. [SDOI2011] 迷宫探险

    ID: 1503 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp搜索2011各省省选山东

[SDOI2011] 迷宫探险

Description

This is a single-player game.

At the start of the game, the player’s character spawns at some position in a maze. The player’s goal is to control the character to reach an exit of the maze (there may be multiple exits).

There are kk types of traps in the maze (denoted by A, B, C, …; the same letter represents the same type of trap). Each type of trap may be harmful or harmless, and at the beginning of the game the player does not know which traps are harmful and which are harmless.

Traps of the same type share the same state: all traps marked with the same letter are either all harmful or all harmless; it never happens that some are harmful and others are harmless. Every combination of trap states has a probability. Consider the following example:

When k=2k=2, there are two types of traps in the maze, denoted by A and B. There are 44 combinations of trap states:

  • A is harmless, B is harmless.
  • A is harmful, B is harmless.
  • A is harmless, B is harmful.
  • A is harmful, B is harmful.

The following table is a valid probability table:

A is harmless A is harmful
B is harmless 36%36\% 24%24\%
B is harmful 24%24\% 16%16\%

When k=3k=3, there are 88 different combinations of trap states. If we still insist on using a probability table, it would be three-dimensional (2×2×22 \times 2 \times 2, with each dimension corresponding to one trap type). When k3k \ge 3, this makes the problem hard to describe. Therefore, we use an array pp of size 2k2^{k} to describe the probability of each case. The index range of pp is 02k10 \sim 2^{k} - 1.

Array pp is constructed as follows:

For each possible combination of trap states, consider all kk types of traps. Let 11 indicate a harmful trap and 00 indicate a harmless trap. Treat A as the 00-th bit of a binary number (counting from the right), B as the 11-st bit, C as the 22-nd bit, and so on. With this, we obtain a kk-bit binary number. After converting it to decimal, the 2k2^{k} combinations of trap states correspond one-to-one to the integers 02k10 \sim 2^{k} - 1.

Let S=i=02k1piS = \displaystyle\sum_{i=0}^{2^k-1} p_i. Then the probability that trap-state combination ii occurs is piS\dfrac {p_{i}} {S}.

A valid array pp corresponding to the above table is 36,24,24,1636, 24, 24, 16.

Of course, the same probability table may correspond to multiple arrays pp (in fact, infinitely many arrays pp can match the table data). For example, the above table also corresponds to the array pp: 72,48,48,3272, 48, 48, 32.

The player’s character initially has HH hit points (HP). When the character steps on a trap, if the trap is harmful, 11 HP is lost; otherwise, if the trap is harmless, no HP is lost. In either case, the player immediately learns the information of that trap (harmful or harmless). Once HP is less than or equal to 00, the character dies immediately.

The maze can be regarded as an m×nm \times n grid map. Each cell may be:

  • .: flat ground, passable.
  • #: wall, impassable.
  • A, B, C, …: a trap.
  • $: the starting point; there is exactly one on the map.
  • @: an exit; there may be multiple, or none.

The character can move up, down, left, and right. Diagonal movement is not allowed, and the character cannot move outside the map.

Given the m×nm \times n map, kk, HH, and the probability array of size 2k2^{k}, your task is to compute the probability that, under an optimal strategy, the character reaches an exit alive.

Input Format

The first line contains 44 integers, denoting mm, nn, kk, and HH.

The next mm lines each contain nn characters describing the maze map.

The last line contains 2k2^{k} nonnegative integers describing the array pp, with indices starting from 00.

Output Format

Output a single number: the probability that, under an optimal strategy, the character reaches an exit alive. Round to 33 decimal places.

4 3 2 1
.$.
A#B
A#B
.@.
30 30 20 20
0.600
4 3 2 2
.$.
A#B
A#B
.@.
30 30 20 20
0.800
4 3 2 3
.$.
A#B
A#B
.@.
30 30 20 20
1.000
4 3 3 2
.$.
A#B
A#C
@@@
143 37 335 85 95 25 223 57
0.858

Hint

【Sample Explanation 1】

Move right, passing B. The probability that B is harmful is (20+20)(30+30+20+20)=0.4\frac {(20+20)}{(30+30+20+20)}=0.4. If B is harmful, the character dies and the game fails; otherwise, the player learns that B is harmless and continues through another B to reach the exit. The success probability is 0.60.6.

【Sample Explanation 2】

Move left, passing A. The probability that A is harmful is (30+30)(30+30+20+20)=0.5\frac {(30+30)} {(30+30+20+20)}=0.5. If A is harmful, 11 HP is lost, then switch to the right path to try B. To succeed, B must be harmless. Given that A is harmful, the conditional probability that B is harmless is 30(30+20)=0.6\frac {30}{(30+20)}=0.6. Hence, the probability of this branch is 0.5×0.6=0.30.5 \times 0.6 = 0.3. If A is harmless, the player can guide the character through two consecutive A to reach the exit, with probability 0.50.5. Therefore, the answer is 0.3+0.5=0.80.3 + 0.5 = 0.8.

【Sample Explanation 3】

The character has 33 HP but needs to pass through at most two traps, so taking either the left or the right path suffices to reach the exit.

Constraints

Test point ID mm nn kk HH
11 2929 2828 55 11
22 2828 2020 44
33 2525 3030 11
44 22
55 33
66 55 44 44
77 1212 1111 55
88 1919 1717 55 33
99 2323 2525 44
1010 3030 2929 55

For 100%100\% of the testdata, 1m301 \le m \le 30, 1n291 \le n \le 29, k5k \le 5, H5H \le 5, 0pi1050 \le p_i \le 10^5, and at least one pi>0p_i \gt 0.

Translated by ChatGPT 5