This page is still under construction.

Parts of this page are still being built. What you see may change.

Robots

Time limit1sMemory limit128 MB

Summary
Simulate a 31x31 robot game where robots chase you, applying a tie-broken greedy movement and teleport strategy to report whether you win or lose.
Level

Hard8 of 10

Topics
Simulation, Implementation, Greedy, BFS
Solved
No attempts yet

Problem

Robots is a single-player game played on a 31×3131 \times 31 board. The board is divided into 1×11 \times 1 cells arranged in 3131 rows and 3131 columns. Each cell is identified by (r,c)(r, c), where rr is the row and cc is the column, both numbered from 11. A cell may be empty, occupied by you, occupied by a robot, or occupied by debris. Your goal is to move so that every robot is destroyed before the robots destroy you.

Initially you occupy cell (15,15)(15, 15), and there are RR robots (1≤R≤50)(1 \le R \le 50) placed in RR distinct cells other than (15,15)(15, 15). Every other cell is empty. You are also given a list of TT (0≤T≤20)(0 \le T \le 20) cells that are possible teleport destinations. You move first, and afterwards you and the robots move alternately.

On your turn you may do exactly one of the following.

  • Walk to an adjacent cell in one of the eight compass directions, if that cell is empty.
  • Walk to an adjacent cell that contains debris by pushing the debris one more cell along the same direction, provided the cell the debris is pushed into does not already contain debris. If that cell contains a robot, the robot is destroyed.
  • Teleport to one of the listed destinations, which must be an empty cell.
  • Remain stationary.

You may never make a move that would push you or any debris off the board.

When the robots move, each robot steps to the adjacent cell (in one of the eight compass directions, even if it is not empty) that is closest to your current cell, i.e. your cell after your latest move. The distance between (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) is ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|. All robots step at the same time. If two or more robots step onto the same cell, or a robot steps onto a cell that already contains debris, all of those robots are destroyed. A destroyed robot becomes debris.

You lose if any robot steps onto your current cell, even if several robots do so and destroy one another. You win if every robot has been destroyed and none stepped onto your current cell.

To survive as long as possible you consider only moves that do not lead to an immediate loss (a loss before your next turn). The strategy is: walk to a cell (or stay put) so that the number of robots remaining after your move and the robots' following move is as small as possible. Break ties by choosing the move that maximizes the minimum distance from your destination to the remaining robots just before your next turn. Break further ties by the smallest destination row, and finally by the smallest destination column.

If no walking or staying move avoids an immediate loss, you teleport to the first still-unused legal destination in the list that does not lead to an immediate loss, always scanning the list from the beginning. If no such destination exists, you remain stationary and lose.

Implement this strategy and report how the game turns out.

Input

The input contains several test cases. Each test case begins with a line containing two integers RR and TT separated by a space. The next RR lines each contain two integers, the row and column of a robot's starting cell; the robots start on distinct cells, none of which is (15,15)(15, 15). The following TT lines each contain two integers, the row and column of a teleport destination, in the order they should be tried. The input ends with a test case in which R=T=0R = T = 0; this case must not be processed.

Output

For each test case print the case number on its own line in the format shown, starting from 11: Case k:.

Every time you teleport, print a line of the form

Move m: teleport to (r,c)

where mm is the number of moves you have made so far, including this one, and (r,c)(r,c) is the teleport destination.

Then print the outcome. If you win, print

Won game after making m moves.
Final position: (r,c)
Number of cells with debris: d

where mm is the number of moves made when you won, (r,c)(r,c) is your final cell, and dd is the number of cells containing debris. Always use the word "moves", even when m=1m = 1.

If you lose, print

Lost game after making m moves.
Final position: (r,c)
Number of cells with debris: d
Number of robots remaining: n

where mm is the number of moves made when you lost, (r,c)(r,c) is the cell where you were destroyed, dd is the number of cells containing debris, and nn is the number of robots still remaining. Always use the word "moves", even when m=1m = 1.

Separate the output for consecutive test cases with a blank line.

Examples3

  1. Example 1

    Input
    4 0
    17 18
    13 18
    8 12
    10 12
    4 0
    17 17
    13 17
    13 13
    17 13
    3 3
    17 18
    13 18
    5 31
    15 16
    16 15
    3 7
    0 0
    
    Expected output
    Case 1:
    Won game after making 5 moves.
    Final position: (14,16)
    Number of cells with debris: 1
    
    Case 2:
    Lost game after making 2 moves.
    Final position: (15,15)
    Number of cells with debris: 1
    Number of robots remaining: 0
    
    Case 3:
    Move 30: teleport to (16,15)
    Move 58: teleport to (15,16)
    Move 86: teleport to (3,7)
    Lost game after making 114 moves.
    Final position: (1,29)
    Number of cells with debris: 1
    Number of robots remaining: 1
    
  2. Example 2

    Input
    2 0
    14 15
    16 15
    0 0
    
    Expected output
    Case 1:
    Lost game after making 1 moves.
    Final position: (15,15)
    Number of cells with debris: 1
    Number of robots remaining: 0
    
  3. Example 3

    Input
    50 0
    1 1
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 10
    1 11
    1 12
    1 13
    1 14
    1 15
    1 16
    1 17
    1 18
    1 19
    1 20
    1 21
    1 22
    1 23
    1 24
    1 25
    31 1
    31 2
    31 3
    31 4
    31 5
    31 6
    31 7
    31 8
    31 9
    31 10
    31 11
    31 12
    31 13
    31 14
    31 15
    31 16
    31 17
    31 18
    31 19
    31 20
    31 21
    31 22
    31 23
    31 24
    31 25
    0 0
    
    Expected output
    Case 1:
    Won game after making 12 moves.
    Final position: (15,13)
    Number of cells with debris: 24