Brothers

Interview

Time limit1sMemory limit128 MB

Summary
Given a grid of counties owned by heirs in a cycle, apply K simultaneous rounds where a cell switches to the previous heir if an orthogonal neighbor already has that heir, then print the grid.
Level

Medium4 of 10

Topics
Simulation, Implementation, Matrix, Array
Solved
No attempts yet

Problem

A great King ruled a kingdom shaped like a rectangle. Before he died, he divided the territory into a grid of small rectangular counties and distributed them among his sons.

The King did not know that his sons had a peculiar rivalry: heir 00 hated heir 11, heir 11 hated heir 22, and so on; the last heir, N−1N-1, hated heir 00. Each heir hated exactly one other heir and no one else, so in general heir ii hated heir (i+1) mod N(i+1) \bmod N.

When the King died, war broke out. Two counties are adjacent if they share a horizontal or vertical border. During an attack, a county XX conquers an adjacent county YY whenever the owner of XX hates the owner of YY, and the conquered county then belongs to the attacker. All attacks happen simultaneously, and one round of simultaneous attacks is called a battle.

Because the only heir who hates the owner vv of a county is heir (v−1) mod N(v-1) \bmod N, every county that is attacked is conquered by that same heir. Equivalently: in each battle, a county currently owned by vv becomes owned by (v−1) mod N(v-1) \bmod N if at least one of its orthogonal (up, down, left, right) neighbors is owned by (v−1) mod N(v-1) \bmod N; otherwise it keeps owner vv.

Given the number of heirs, the initial land distribution, and the number of battles, determine the land distribution after all battles have taken place. For example, with three heirs (N=3N = 3) a single battle transforms the map according to the rule above.

Input

The input contains several test cases. The first line of a test case contains four integers NN, RR, CC and KK separated by single spaces: NN is the number of heirs (2≤N≤1002 \le N \le 100), RR and CC are the dimensions of the kingdom (2≤R,C≤1002 \le R, C \le 100), and KK is the number of battles (1≤K≤1001 \le K \le 100). Heirs are numbered from 00 (the first heir) to N−1N-1 (the last heir).

Each of the next RR lines contains CC integers Hr,cH_{r,c} separated by single spaces: Hr,cH_{r,c} is the initial owner of the county in row rr and column cc (0≤Hr,c≤N−10 \le H_{r,c} \le N-1).

The last test case is followed by a line containing four zeros separated by single spaces.

Output

For each test case, print RR lines with CC integers each, separated by single spaces, in the same format as the input, representing the land distribution after all KK battles.

Examples1

  1. Example 1

    Input
    3 4 4 3
    0 1 2 0
    1 0 2 0
    0 1 2 0
    0 1 2 2
    4 2 3 4
    1 0 3
    2 1 2
    8 4 2 1
    0 7
    1 6
    2 5
    3 4
    0 0 0 0
    
    Expected output
    2 2 2 0
    2 1 0 1
    2 2 2 0
    0 2 0 0
    1 0 3
    2 1 2
    7 6
    0 5
    1 4
    2 3