B. Pebble Automaton
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A Pebble Automaton is a grid of cells. Each cell can contain some number of stones. In every iteration, each cell can move some number of stones up, right, down and left. Each cell only knows two things, based on which it will make its decision:

  • Its position in the grid
  • The number of stones in the cell

It knows nothing about other cells, previous or current iteration. It just moves the stones based on how many stones it has.

Your task is to define these functions for every cell, such that in the end you calculate something!

For each task, the grid is initially empty. Then, $$$A$$$ stones are placed in the top left corner and $$$B$$$ stones are placed in the top right corner. After that, the iterations start. Each iteration, every cell moves its stones simultaneously. If during some iteration no stones were moved, the Pebble Automaton stops and the final grid state is considered the output.

You need to solve $$$5$$$ separate tasks:

  1. ($$$10$$$ points) $$$A+B$$$ stones in the bottom right corner
  2. ($$$20$$$ points) $$$|A-B|$$$ stones in the bottom right corner
  3. ($$$15$$$ points) $$$\min(A, B)$$$ stones in the bottom right corner
  4. ($$$15$$$ points) $$$\max(A, B)$$$ stones in the bottom right corner
  5. ($$$40$$$ points) $$$1$$$ stone in the bottom left corner if $$$A \gt B$$$ and $$$1$$$ stone in the bottom right corner if $$$B \gt A$$$. If $$$A = B$$$, then both corners must contain $$$0$$$ stones.

For each task, remaining cells in the grid can have any number of stones. You will get $$$50\%$$$ of points for a task if you only solve it for odd grid size $$$N$$$.

Constraints:

  • $$$1 \leq A, B \leq 100$$$,
  • Total number of iterations before stopping is at most $$$10^3$$$
  • Cells can never move stones outside of the grid
Input

First line contains two numbers – $$$S (1 \leq S \leq 5)$$$ and $$$N (5 \leq N \leq 10)$$$. $$$S$$$ is the number of the task, $$$N$$$ is the size of the grid. The grid is always square.

Interaction

The grader will ask you some number of queries. Each query is $$$3$$$ numbers – $$$x$$$, $$$y$$$ ($$$1 \leq x, y \leq N$$$) and $$$count$$$ ($$$0 \leq count \leq 200$$$). Each query asks about the cell in row $$$x$$$ and column $$$y$$$ with $$$count$$$ stones.

The output for each query is $$$4$$$ numbers separated by spaces – $$$u$$$, $$$r$$$, $$$d$$$ and $$$l$$$. This is the number of stones moved up, right, down and left respectively. The remaining stones will stay in this cell. Each query must be answered on a separate line with flushed output.

To read queries, use while(cin >> x >> y >> count) in C++ or for line in sys.stdin in Python.

Note

The queries will be called in any order so you don't have any information about the other cells or the previous iteration.