#P9082. [PA 2018] Gra
[PA 2018] Gra
Problem Description
This problem is translated from PA 2018 Round 5 Gra.
It is the weekend now. You can finally relax and play your favorite strategy game. The goal of the game is to collect all the coins on the map and bring them back to the base. The rules are as follows:
- The map is an grid. Rows are numbered from to from top to bottom, and columns are numbered from to from left to right. We use to denote the cell in row and column . Two cells are adjacent if and only if they share a side.
- Your base is located at cell in the top-left corner. You can recruit new characters there, and you must bring collected coins back there. In the remaining cells, each cell initially contains either some number of coins or some number of stones.
- There are two types of characters that can move on the map: a farmer can collect coins but cannot enter a cell containing stones, while a tank can clear stones and can enter any type of cell.
- The game proceeds in rounds. In each round, each character may move at most once, to a cell adjacent to its current cell. Two characters cannot be in the same cell at the same time. All moves are instantaneous (movement takes zero time).
- If at the end of a round a farmer is on a cell with coins, it takes coins and puts them into its backpack. If the cell has fewer than coins, it takes all of them. The farmer’s backpack has unlimited capacity. However, a farmer cannot enter a cell where the number of stones is not . If at the end of a round the farmer returns to the base, it unloads all coins in its backpack into the base.
- If at the end of a round a tank is on a cell with stones, it clears stones from that cell (or all of them if there are fewer than ).
- Initially, the base contains coins. Buying any character (farmer or tank) costs coins (before buying, the base must have at least that many coins). After buying, the character appears immediately, so a newly bought character can also move in the same round.
Your task is: for each input map (that is, for each test case), find a sequence of commands such that after playing according to this sequence, all coins on the map have been brought back to the base (and may have been partially or completely spent). In other words, in the final state, no cell contains any coins, and no farmer’s backpack contains any coins. The commands are shown in the table below:
| Command | Effect |
|---|---|
| Buy a farmer character and spawn it at the base. | |
| Buy a tank character and spawn it at the base. | |
| Move a character from to the adjacent cell . | |
| End this round. | |
| End the game (i.e., the current test case). |
Your program will be tested on testdata prepared by the organizers. Each testdata set consists of a number of maps (test cases). Each testdata set has a limit (see the “Constraints and Limits” section). This is a limit on the average number of rounds per testdata set. In other words, if there are maps in the testdata set, your program must finish all games within at most rounds in total. We define the number of rounds for one test case as the number of times the command is used plus .
Invalid commands, exceeding the round limit, or failing to achieve the goal will all be judged as Wrong Answer.
Input Format
The first line contains two integers , denoting the number of test cases and the average round limit. In all testdata except the sample, .
For each testdata set, the first line contains a positive integer . In all testdata except the sample, .
Then follow lines, each with integers. denotes the base (which is always in the top-left corner). A positive number means this cell contains coins, and a negative number means this cell contains stones.
The map is generated as follows: for each testdata set, the organizers choose a constant , which is the probability that a cell contains stones. For each cell except the base, an integer is chosen uniformly at random from to , and then is assigned to this cell. After that, with probability , this value is multiplied by .
Output Format
For each test case, output a sequence of commands, one command per line. You may output at most commands.
2 12
3
0 -8 -512
-16 -1 -128
8 -2 -512
3
0 64 -1
64 -1 -1
1 -1 -1
R TANK
M 0 0 1 0
=
=
M 1 0 1 1
R FARMER
M 0 0 1 0
=
M 1 0 2 0
=
M 2 0 1 0
=
M 1 0 0 0
=
===
R FARMER
M 0 0 0 1
R FARMER
M 0 0 1 0
=
=
=
=
M 0 1 0 0
=
M 0 0 0 1
=
=
M 1 0 2 0
=
M 2 0 1 0
=
M 1 0 0 0
=
M 0 0 1 0
=
R FARMER
M 1 0 2 0
M 0 0 1 0
M 0 1 0 0
=
===
Hint
Sample 1 Explanation
For the first test case, we first buy a tank and immediately move it from to , so that all stones are cleared in two rounds. Then we move the tank to , buy a farmer at the base, and send it to to collect coins. After all coins are collected, we let the farmer return the coins and empty its backpack. We could buy a farmer in the first round, but it would have to wait until the tank clears the stones before it can move.
The second test case shows a solution that is not optimal but is correct. Note that a farmer can leave a cell without collecting all the coins on it. Recruiting the first two characters will spend all initial coins (), so we can only buy the third character after the farmer brings coins back to the base.
The first test case uses rounds, and the second test case uses rounds. The average is rounds, which does not exceed the given limit .
Constraints
This problem uses bundled tests.
There are ten subtasks, and each subtask contains to testdata sets. The exact values of and are given in the table below:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|---|
Please note that the sample and the testdata differ slightly in map size and in the number of test cases.
Translated by ChatGPT 5