#P2566. [SCOI2009] 围豆豆
[SCOI2009] 围豆豆
Description
Are you tired of playing Pac-Man on your phone? Recently, the MOKIA phone launched a new game called “Enclosing Beans.” Give it a try.
The rules are simple. In an grid there are beans, and each bean has a value . The player may choose any cell as the starting cell. Each move, the player may go to one of the four orthogonally adjacent cells, and eventually must return to the starting cell. The final score is the total value of all beans enclosed by the path minus the number of steps taken. Some cells contain obstacles; at no time may the player enter a cell that contains an obstacle or a bean. The player’s minimum possible score is , i.e., they may choose to do nothing.
Note the notion of being enclosed: a bean is considered enclosed if it lies inside the polygon formed by the path (which may be a complex polygon with self-intersections). See the two examples below:

In the first example, the bean lies inside the rectangle formed by the path, so it is enclosed. In the second example, even though the path visits the neighboring cells around the bean, the interior of the polygon formed by the path does not contain the bean, so it is not enclosed.
Bubu has become obsessed with this game but cannot get a high score. You decide to write a program to help him pass the game.
Input Format
The first line contains two integers and , the dimensions of the grid.
The second line contains an integer , the total number of beans.
The third line contains integers to , the values of the beans.
Then follow lines, each with characters, forming an character matrix describing the game grid. Character 0 denotes an empty cell, # denotes an obstacle, and digits 1 to 9 denote beans with the corresponding indices.
Output Format
Output a single integer, the maximum possible score.
3 8
3
30 -100 30
00000000
010203#0
00000000
38
Hint
of the testdata satisfies .
of the testdata satisfies , , .
Translated by ChatGPT 5
京公网安备 11011102002149号