Under construction

Still being built. Things may change

Battle simulation

Time limit2sMemory limit1024 MB

Summary
Simulate dash commands on a grid where units stop when they meet an enemy and cannot exceed their movement power, then print final positions.
Level

Medium6 of 10

Topics
Shortest path, Simulation, Graph
Solved
No attempts yet

Problem

You are building a battle simulation game where two factions fight on a 2D grid map of size H×WH \times W. Each cell can be written as a coordinate pair (y,x)(y, x). Cells in the first row are (0,0),(0,1),⋯ ,(0,W−1)(0, 0), (0, 1), \cdots, (0, W-1) from left to right, and cells in the second row are (1,0),(1,1),⋯ ,(1,W−1)(1, 0), (1, 1), \cdots, (1, W-1). Every cell up to the last row is written in the same way. The cells directly above, below, left, and right of a cell are called adjacent to that cell.

Problem

The map is made of various terrains, and each terrain has a fixed roughness value. Several units are placed on the map without overlapping, and each unit belongs to one of the two factions. As long as it stays on the map, a unit can move up, down, left, or right to an adjacent cell. Each move costs stamina equal to the roughness of the cell entered. Some terrains are too rough to enter at all. Two units from different factions that are adjacent to each other are in combat.

Problem

All units ate a hearty combat ration before heading out, so their stamina is unlimited. However, each unit has a limit on the total stamina it can spend in a single dash, and this limit is called the unit's movement power. A dash is a tactical move in which a unit in combat rushes to a nearby target cell in one go, passing through one or more cells. A dash is possible only if no other unit is on the target cell. While dashing, a unit may pass through allied units. If it meets a unit of the opposing faction and becomes adjacent to it, combat starts and the unit must stop there. However, if the selected unit was already in combat, it can dash out of combat.

Problem

A bot picks random units and issues dash commands to test the game for bugs. The bot sometimes issues dash commands that cannot be carried out. A command is impossible if the target cell holds another unit, if the target cell is impassable terrain, or if no path to the target exists within the unit's movement power. If the game has no bugs, these commands must be ignored.

Problem

Now it is time to check for bugs. Given the bot's commands in time order, write a program that prints each unit's coordinates after all commands are processed in order.

Input

The first line has three space-separated integers NN, HH, and WW, the number of terrain types, the map height, and the map width. (1≤N≤91 \leq N \leq 9, 2≤H,W≤5002 \leq H,W \leq 500)

The next HH lines each contain WW integers giving the terrain number of each cell from left to right. Each number is between 11 and NN, inclusive.

The next line contains NN integers r1,r2,⋯ ,rNr_1, r_2, \cdots, r_N (−1≤ri≤4-1 \leq r_i \leq 4, ri≠0r_i \neq 0). If rir_i is −1-1, terrain ii is too rough to enter. Otherwise, rir_i is the roughness of terrain ii.

The next line contains the number of units MM. (1≤M≤H×W/41 \leq M \leq H \times W / 4)

The next MM lines describe units 1 through MM in order. Each line has four integers mm, tt, aa, bb: the movement power, the faction, and the yy and xx coordinates of the unit's cell. (1≤m≤201 \leq m \leq 20, 0≤t≤10 \leq t \leq 1, 0≤a<H0 \leq a < H, 0≤b<W0 \leq b < W)

Each cell holds at most one unit, and no unit is placed on a cell whose terrain cannot be entered.

The next line contains the number of dash commands KK. (1≤K≤10 0001 \leq K \leq 10\ 000)

The next KK lines each contain three integers uu, aa, bb, meaning that unit uu dashes to (a,b)(a, b). (1≤u≤M1 \leq u \leq M, 0≤a<H0 \leq a < H, 0≤b<W0 \leq b < W)

Output

Print MM lines. After all dash commands are processed, line ii contains the coordinates aia_i and bib_i of unit ii, separated by a space.

Hint

The first dash cannot be carried out. Ignoring combat, a unit could reach (2,3)(2, 3) and then (1,3)(1, 3) along (2,0)→(2,1)→(2,2)→(2,3)→(1,3)(2, 0) \to (2, 1) \to (2, 2) \to (2, 3) \to (1, 3). However, the enemy unit at (3,3)(3, 3) starts combat at (2,3)(2, 3), so (1,3)(1, 3) cannot be reached. No detour avoids combat either, so the command is ignored.

The second dash targets a cell with impassable terrain, so it is ignored.

The third dash is possible. The path (2,0)→(3,0)→(4,0)→(4,1)→(4,2)→(4,3)(2, 0) \to (3, 0) \to (4, 0) \to (4, 1) \to (4, 2) \to (4, 3) costs 77 stamina, which does not exceed the unit's movement power.

After all commands, unit 1 is at (4,3)(4, 3) because of the last command, and unit 2 stays at its initial position.

Examples1

  1. Example 1

    Input
    3 5 5
    1 1 3 3 2
    3 3 3 1 2
    1 1 1 2 1
    2 2 1 1 1
    1 1 1 1 3
    1 3 -1
    2
    7 0 2 0
    4 1 3 3
    3
    1 1 3
    2 4 4
    1 4 3
    
    Expected output
    4 3
    3 3