#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 n×mn \times m 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 L1,L2,L3,,L9L_1, L_2, L_3, \ldots , L_9).

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 33. 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 L9L_9 is the highest level, so connected components of multiple L9L_9 will not merge. For example, in the figure below, after building the middle L1L_1, the L1L_1 connected to it are merged into one L2L_2.

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 L1L_1 L2L_2 L3L_3 L4L_4 L5L_5 L6L_6 L7L_7 L8L_8 L9L_9
Score 44 2020 100100 500500 15001500 50005000 2000020000 100000100000 500000500000

Using the two game processes above as examples: in Figure 2, first an L1L_1 is built, gaining 44 points. Then the L1L_1 reacts and generates an L2L_2, gaining another 2020 points. The total is 2424. In Figure 3, this move scores 4+20+100+500=6244+20+100+500=624 points.

To reduce the difficulty, the game also provides two items: “star” and “bomb”. At the start of the game, the player is given pp stars and qq 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 L1L_1. For example, placing a star at the very center in Figure 3, the star automatically becomes L3L_3. 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 nn, mm, indicating that the map has nn rows and mm columns.

The next line contains two integers pp, qq, indicating the number of star items and bomb items.

The next nn lines contain the initial n×mn \times m map. The character . means an empty cell, and digits 191\sim 9 represent buildings of the corresponding levels.

The next line contains a number kk, indicating the length of the building sequence.

The last line contains kk space-separated integers between 191\sim 9, indicating the content of the building sequence.

Output Format

For the given 1010 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 44 types of commands:

Command Meaning
PUT x y Place the next unit in the building sequence into the empty cell at row xx, column yy.
STAR x y Place a star item into the empty cell at row xx, column yy.
BOMBER x y Place a bomb at row xx, column yy. 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 4+20+100=1244+20+100=124.

The second move scores 100100.

The third move scores 100+500=600100+500=600.

The total score is 124+100+600=824124+100+600=824.

Scoring

For each test, we provide 99 scoring parameters a10,a9,,a2a_{10}, a_9, \ldots, a_2, one per line in this order, in the additional files tritown1.ans ~ tritown10.ans. If your output is invalid, you get 00 points. Otherwise, let your game score be wuserw_{user}. Your score is given by the table below:

Score Condition Score Condition
10 wusera10w_{user}\geq a_{10} 5 wusera5w_{user}\geq a_5
9 wusera9w_{user}\geq a_9 4 wusera4w_{user}\geq a_4
8 wusera8w_{user}\geq a_8 3 wusera3w_{user}\geq a_3
7 wusera7w_{user}\geq a_7 2 wusera2w_{user}\geq a_2
6 wusera6w_{user}\geq a_6 1 wuser>0w_{user}>0

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