#P4739. Torus Drone Simulation

    ID: 17983 Type: Default 1000ms 256MiB

Torus Drone Simulation

Torus Drone Simulation

You are given a toroidal grid with \(r\) rows and \(c\) columns. The rows are numbered from \(1\) to \(r\) (top to bottom) and the columns from \(1\) to \(c\) (left to right). Each cell \((a,b)\) has a positive integer elevation \(h_{a,b}\). The grid is toroidal, meaning that both rows and columns wrap around circularly.

A drone is initially located at cell \((1,1)\). You will be given \(q\) commands. There are two types of commands:

  • move k — the drone makes \(k\) steps.
  • change a b e — the elevation at cell \((a,b)\) changes to \(e\).

In each step, suppose the drone is currently at cell \((i, j)\). It will consider the three cells in column \(j+1\) (with wrapping so that the column after \(c\) is \(1\)): the cell directly to the right, the cell diagonally right-down, and the cell diagonally right-up. Formally, if we let \(r_i = i-1\) (using 0-indexing), then the three candidates (using 0-indexing) are:

[ \begin{aligned} & (i, ((j) % c) + 1),\[6pt] & (((i % r) + 1), ((j) % c) + 1),\[6pt] & (((i-2 + r) % r) + 1, ((j) % c) + 1). \end{aligned} ]

The drone will move to the candidate cell with the highest elevation. It is guaranteed that in any column, no three circularly consecutive cells (in the cyclic order of rows) have the same elevation. Thus, each move is well defined.

After each move command, output the drone’s current position (row and column).

inputFormat

The first line contains three integers \(r\), \(c\), and \(q\) — the number of rows, columns, and commands.

The next \(r\) lines each contain \(c\) positive integers, representing the initial elevations of the grid cells.

The following \(q\) lines describe commands. Each command is either in the format move k or change a b e, where \(k\), \(a\), \(b\), and \(e\) are integers.

outputFormat

For each move command, output a line with two integers separated by a space: the row and column of the drone’s position after performing that move command.

sample

3 4 5
1 2 3 4
2 3 4 1
3 4 1 2
move 2
change 2 3 10
move 3
change 3 4 8
move 1
2 3

3 2 2 3

</p>