Under construction

Still being built. Things may change

Zerg Rush!!!

Time limit1sMemory limit128 MB

Summary
Simulate a grid battle of two Zergling armies over t turns following detailed attack, death, movement, and regeneration rules.
Level

Medium7 of 10

Topics
Simulation, Implementation, Matrix, Brute force
Solved
No attempts yet

Problem

A classic Starcraft tactic is to mass a huge swarm of cheap units such as Zerglings and throw them all at the opponent at once. When both players do this simultaneously, the clash is quick, brutal, and messy. In this problem you simulate such a battle, which may involve far more Zerglings than a real game would ever allow.

The battlefield is an N×NN \times N grid. Every Zergling occupies exactly one square, and no two Zerglings ever share a square. Each Zergling starts with 3535 hit points. Its attack value is 55 plus the attack upgrade of the player who controls it. When one Zergling attacks another, the damage dealt equals the attacker's attack value minus the armour upgrade of the player who owns the defender, and the defender loses that many hit points. (Attack values are 55 to 88 and armour upgrades are 00 to 33, so every hit deals at least 22 damage.)

Every turn, each Zergling independently decides what to do, using this (not very bright) algorithm:

  • If at least one enemy Zergling stands in one of the 88 squares horizontally, vertically, or diagonally adjacent to it, the Zergling attacks one of them. A Zergling attacks at most one enemy per turn.
  • Otherwise, if the opposing player still has at least one Zergling anywhere on the grid, the Zergling moves. It first picks the enemy Zergling with the smallest Manhattan distance to it, then steps to whichever of its in-grid adjacent squares minimizes the Manhattan distance to that chosen enemy. The Manhattan distance between two squares is the sum of the absolute differences of their row indices and of their column indices.

Whenever a rule allows more than one direction, directions are preferred in clockwise order starting from north: north, northeast, east, southeast, south, southwest, west, northwest. North points toward the first row and west toward the first column. Concretely, a Zergling attacks the enemy found in the first such direction, and it moves in the first such direction that reaches the minimizing square. If several enemies are tied for closest when choosing a move target, the northernmost of them is chosen, and among those the westernmost.

Once every Zergling has decided, the turn resolves in exactly this order:

  1. All attacks are applied simultaneously; each defender loses the total damage dealt to it this turn.
  2. Every Zergling now at 00 or fewer hit points dies and is removed from the grid.
  3. Every Zergling that chose to move now moves, simultaneously, subject to two rules. First, if the target square is occupied by another Zergling that is not itself vacating that square this turn, the mover stays where it is (so a Zergling may move into a square that is being vacated, and two or more Zerglings may swap or rotate). Second, if two or more Zerglings try to move into the same square, the one in the northernmost row has priority and the others stay; among those tied in the northernmost row, the westernmost one moves and the rest stay.
  4. Finally, every surviving Zergling with fewer than 3535 hit points regenerates 11 hit point.

Input

The input contains several test cases and ends with a case in which N=0N = 0; that terminating case is not processed.

Each test case is given as follows:

  • A line with the integer NN (2≤N≤1502 \le N \le 150), the side length of the grid.
  • A line with two integers between 00 and 33: the attack upgrade and the armour upgrade of player 1.
  • A line with two integers between 00 and 33: the attack upgrade and the armour upgrade of player 2.
  • NN lines describing the initial grid. A . is an empty square, a 1 is a Zergling owned by player 1, and a 2 is a Zergling owned by player 2. The first row is the north edge and the first column is the west edge.
  • A line with the integer tt (0≤t≤4000 \le t \le 400), the number of turns to simulate.

Output

For each test case, output the grid after tt turns in the same format as the input grid: NN lines of NN characters each. Print one empty line between the outputs of consecutive test cases.

Examples7

  1. Example 1

    Input
    2
    0 0
    0 0
    1.
    ..
    0
    2
    0 0
    0 0
    1.
    .2
    100
    0
    
    Expected output
    1.
    ..
    
    ..
    ..
    
  2. Example 2

    Input
    2
    3 3
    0 0
    1.
    .2
    4
    2
    3 3
    0 0
    1.
    .2
    5
    0
    
    Expected output
    1.
    .2
    
    1.
    ..
    
  3. Example 3

    Input
    3
    0 0
    0 0
    1.1
    ...
    ..1
    50
    0
    
    Expected output
    1.1
    ...
    ..1
    
  4. Example 4

    Input
    2
    0 0
    0 0
    12
    ..
    1
    2
    0 0
    0 0
    12
    ..
    9
    0
    
    Expected output
    12
    ..
    
    ..
    ..
    
  5. Example 5

    Input
    4
    0 0
    0 0
    1..2
    .12.
    .21.
    2..1
    0
    0
    
    Expected output
    1..2
    .12.
    .21.
    2..1
    
  6. Example 6

    Input
    2
    0 0
    0 0
    1.
    .2
    8
    2
    0 0
    0 0
    1.
    .2
    9
    0
    
    Expected output
    1.
    .2
    
    ..
    ..
    
  7. Example 7

    Input
    4
    0 0
    0 0
    1..2
    ....
    ....
    ....
    1
    4
    0 0
    0 0
    1..2
    ....
    ....
    ....
    10
    0
    
    Expected output
    .12.
    ....
    ....
    ....
    
    ....
    ....
    ....
    ....