#P15156. [SWERC 2024] Recovering the Tablet
[SWERC 2024] Recovering the Tablet
题目描述
A long time ago, before our modern civilization arose and before any of you were born, was the year -1966 (3961 Before SWERC). During this dark period, there were no streaming services nor any programming contests. Thus, to entertain themselves, humans had rudimentary games that they played on clay tablets.
This year, a mysterious game was created: Kakurus. We know close to nothing about Kakurus (the Internet Archive had not yet been created), except for a few rules described on an artifact you have discovered:
- The game is played on a grid;
- Each cell is either black or white;
- White cells are originally empty, but you will have to put an integer from to (inclusive) inside each white cell;
- Horizontal constraint: A black cell can contain an integer corresponding to the sum of the consecutive white cells to its right (until the first black cell or the limit of the grid);
- Vertical constraint: A black cell can contain an integer corresponding to the sum of the consecutive white cells below it (until the first black cell or the limit of the grid).
Note that the last two rules are independent from each other: a black cell can have , or integers inside it. Note also that no constraint is placed on repetitions of numbers. Finally, since we want this problem to be interesting, every white cell is covered by exactly one vertical constraint and by exactly one horizontal constraint.
At the bottom of your artifact lies a grid of Kakurus. It is already filled, but not necessarily with a correct solution – probably a deterioration due to the age of this antique artifact. Can you find a valid solution that is as close as possible to this proposed solution?
If the number you write on a white cell is and the value of the proposed solution for this cell is , then the closeness score is . The final closeness score of the grid is the sum of all closeness scores of the cells. Your objective is to find the minimum closeness score that can be achieved.
输入格式
The first line of the input consists of three integers, , and , respectively the number of lines and columns of this grid, and the number of sum constraints.
Then, lines follow. The th of these lines only contains digits from to . The th character equals if and only if the cell at line and column (, ) is black, and otherwise is equal to the value of this cell (which is thus white) in the proposed solution.
Then lines follow. Each line is of the form where is equal to either or , , and is an integer between and , inclusive. The sum of the consecutive white cells at the right of (when ) or below (when ) the cell at line and column must be equal to in your solution.
It is guaranteed that every white cell is covered by exactly one vertical and one horizontal constraint.
输出格式
If the grid has no solution, you must output IMPOSSIBLE. Otherwise, your output should consist of the minimum closeness score that can be achieved.
4 4 7
0000
0032
0233
0204
H 3 1 9
V 1 3 6
H 2 2 5
V 2 2 5
V 1 4 9
H 4 1 2
H 4 3 4
1
3 4 5
0000
0012
0345
H 2 2 3
H 3 1 12
V 2 2 3
V 1 3 5
V 1 4 6
IMPOSSIBLE