#P3568. [POI 2014] WAZ-Snake

[POI 2014] WAZ-Snake

Description

A snake fills a 3×n3 \times n board completely. The snake is divided into segments numbered from 11 to 3n3n. Consecutive segments (i.e., kk and k+1k+1) must occupy squares that share an edge. Some segment numbers on the board are erased. Can you reconstruct the snake?

Constraints: 1n10001 \le n \le 1000.

Description

Input Format

The first line contains an integer nn (1n10001 \le n \le 1000), the length of the board.

The next three lines describe the board. The ii-th of these lines contains nn integers aija_{i j} (0aij3n0 \le a_{i j} \le 3n) for 1jn1 \le j \le n.

If aij>0a_{i j} > 0, then aija_{i j} is the number of the snake’s segment occupying the jj-th square of the ii-th row. If aij=0a_{i j} = 0, then the number on this square is unknown.

It is guaranteed that there is at least one valid reconstruction.

Output Format

Print three lines. The ii-th line should contain nn positive integers bijb_{i j} (1jn1 \le j \le n).

All numbers bijb_{i j} together must form a permutation of 1,2,,3n1, 2, \dots, 3n. For every (i,j)(i, j) with aij>0a_{i j} > 0, it must hold that bij=aijb_{i j} = a_{i j}. Moreover, for every kk from 11 to 3n13n-1, the squares containing kk and k+1k+1 must share an edge.

If multiple solutions exist, output any of them.

9
0 0 5 0 17 0 0 0 21
8 0 0 3 16 0 0 25 0
0 0 0 0 0 0 0 0 23

7 6 5 4 17 18 19 20 21
8 1 2 3 16 15 26 25 22
9 10 11 12 13 14 27 24 23

Hint

Translated by ChatGPT 5