【State Compression DP】 SCOI2009 Weidoudou

Subject

Luogu link There are \ (D \) beans
in a \ (N × M \) matrix grid , and each bean has a different score \ (V_i \) . The player can choose any square as the starting grid, and can move to the four adjacent grids at random every time he moves, and finally return to the starting grid. The final score of the player is the sum of the scores of all Doudou surrounded by the path minus the number of steps the player moves. There are obstacles in some grids in the matrix, and players cannot enter the grids containing obstacles or beans at any time. The lowest possible score for a player is 0, which means nothing is done. (The example can be viewed by opening the link, which is more important)

Input format

The two integers \ (N \) and \ (M \) in the first row are the side lengths of the matrix.
An integer \ (D \) in the second line is the total number of beans.
The third line contains \ (D \) integers \ (V_1 \) to \ (V_D \) , which are the scores of each bean.
Then there is a \ (N × M \) character matrix on line N to describe the state of the game matrix, 0 means space and # means obstacle. The numbers 1 to 9 respectively indicate the corresponding number of beans.

Output format

Contains only an integer, the highest possible score.

Sample input

3 8
3
30 -100 30
00000000
010203#0
00000000

Sample output

38

data range

50% of the data satisfies \ (1≤D≤3 \) .
100% of the data satisfies \ (1≤D≤9 \) , \ (1≤N \) , \ (M≤10 \) , \ (-10000≤V_i≤10000 \) .

Ideas

basic method

Learn now Sell the method to
judge whether a point is inside a polygon: draw a ray from the target point and see the number of intersections of this ray with all sides of the polygon. If there are an odd number of intersections, it means inside, if there are even number of intersections, it means outside.

Correct answer

First of all, I see that \ (D \) is very small, in the range of 10, so it must be used to press DP.
Direct violence enumeration starting point. The state should not only record the current position, but also the parity of each Doudian right ray.
The parity of each bean's right ray is a string of \ (D \) bits 01, which can be regarded as a binary number of \ (D \) bits for convenient recording (bitset is available, of course my Konjac is useless orz).
Then \ (f [i] [j] [k] \) represents the coordinate \ ((i, j) \) , and the parity of the t-th Doudian right ray is \ (k >> t \ & 1 \) .
Then you can write a DP + SPFA to solve the problem and enumerate the starting point and status.
Remember to preprocess the coordinates.

Guess you like

Origin www.cnblogs.com/Midoria7/p/12702928.html