#P5570. [NOI2012] 三重镇
[NOI2012] 三重镇
Background
Student Xiao Xi recently became interested in the iOS game Triple Town. After playing, Xiao Xi also started thinking about how to get a higher score in this game.
Problem Description
As shown in the figure below, the game is played on an grid map. The game provides a building sequence. The player chooses an empty cell each time and places the corresponding building unit in order. Buildings have nine different levels. From low to high they are Grass, Bush, Tree, Hut, etc. (for convenience, we call them ).

After the player builds a unit on an empty cell, it may trigger a reaction. A reaction happens if: starting from this cell, the size of the connected component formed by cells with the same building level as this unit is at least . Then this connected component will be merged into one building of the next level. The new building is placed at the position of the last built unit, and all other cells in the component become empty. Here, a connected component means a set of positions that are directly or indirectly adjacent.
Also note that is the highest level, so connected components of multiple will not merge. For example, in the figure below, after building the middle , the connected to it are merged into one .

Note that during merging, chain reactions may occur, as shown below.

The game score depends on the units the player builds and the units generated by reactions. Whenever a building unit is placed or generated by a reaction, the corresponding score is added. The score table for different building levels is as follows:
| Building | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Score |
Using the two game processes above as examples: in Figure 2, first an is built, gaining points. Then the reacts and generates an , gaining another points. The total is . In Figure 3, this move scores points.
To reduce the difficulty, the game also provides two items: “star” and “bomb”. At the start of the game, the player is given stars and bombs, and can use them at any time. Their functions are:
- “Star” item: It can be placed on an empty cell. When a star is placed, it automatically becomes the highest-level building that can trigger a reaction. If no reaction can be triggered at that position, the star becomes . For example, placing a star at the very center in Figure 3, the star automatically becomes . The score of the star is calculated according to the building level it becomes.
- “Bomb” item: A bomb can be placed on a cell that already has a building. It destroys the building and turns that cell back into empty. When using a bomb, half of the destroyed building’s score is deducted (i.e. the score added is negative).
During the game, the player must place buildings in the given order, but may interleave the use of the two items at any time. The goal is to achieve the highest possible score through proper operations.
Input Format
This is an output-only problem.
For each test, the first line of the input file contains two integers , , indicating that the map has rows and columns.
The next line contains two integers , , indicating the number of star items and bomb items.
The next lines contain the initial map. The character . means an empty cell, and digits represent buildings of the corresponding levels.
The next line contains a number , indicating the length of the building sequence.
The last line contains space-separated integers between , indicating the content of the building sequence.
Output Format
For the given input files tritown1.in ~ tritown10.in, you need to submit your output files tritown1.out ~ tritown10.out respectively.
Each output file contains the player’s game commands. There are types of commands:
| Command | Meaning |
|---|---|
PUT x y |
Place the next unit in the building sequence into the empty cell at row , column . |
STAR x y |
Place a star item into the empty cell at row , column . |
BOMBER x y |
Place a bomb at row , column . This cell must be non-empty. |
END |
End the game and settle the current score. |
The output must end with the END command. The player may end the game at any time.
2 3
1 1
..1
221
2
1 3
PUT 1 2
PUT 1 1
STAR 2 1
END
Hint
Sample Explanation
This sample corresponds to the figure below:

The first move scores .
The second move scores .
The third move scores .
The total score is .
Scoring
For each test, we provide scoring parameters , one per line in this order, in the additional files tritown1.ans ~ tritown10.ans. If your output is invalid, you get points. Otherwise, let your game score be . Your score is given by the table below:
| Score | Condition | Score | Condition |
|---|---|---|---|
| 10 | 5 | ||
| 9 | 4 | ||
| 8 | 3 | ||
| 7 | 2 | ||
| 6 | 1 |
If multiple conditions are satisfied, take the highest score among them.
Additional Files
The checker needs to be compiled before use.
Please go to Github to download the latest source code of testlib.h.
Translated by ChatGPT 5