5478.Loops

Time Limit: 1s Memory Limit: 256MB

Consider four integers $A$, $B$, $C$, and $D$, such that $A < B < C < D$. Let's put them in the corners of a square in some order and draw a loop $A - B - C - D - A$. Depending on the arrangement of the integers, we can get different loop shapes, but some arrangements produce the same shape:

6iubeoDD2gj45TLW6xjEBzD0MEqQz391PQ3jfeTE.png
There are three possible loop shapes we can get:

4Q6CTtKFlTmpJx7oRb46zO0y2FWcLpDRecvGwG6c.png
Now consider an $n\times m$ matrix filled with distinct integers from $1$ to $nm$, inclusive. Each $2\times 2$ square in this matrix can be seen as a square with integers in its corners. Let's build a loop for each of these squares like we did before:

Nm1V0LitROKNzp4baFaqMNEgkxF3LMMYzFs99JX2.png
Your task is to perform the inverse operation. You are given the shape types for all $(n-1)(m-1)$ loops, and you need to build an $n\times m$ matrix filled with distinct integers from $1$ to $nm$, inclusive, that produces these shapes.

Input Format(From the terminal/stdin)

The first line contains two integers $n$ and $m$ ($2\le n, m\le 500$).

Each of the next $n-1$ lines contains a string of $m-1$ characters without spaces. Each character is a digit from $1$ to $3$, denoting the type of the shape of the corresponding loop.

Output Format(To the terminal/stdout)

Print an $n\times m$ matrix filled with distinct integers from $1$ to $nm$, inclusive, that produces the shapes of the loops in the input.

It can be shown that such a matrix always exists. If there are multiple answers, print any of them.

Sample Input

Copy
3 4
113
231
 · \n
   \n
   \n

Sample Output special judge

Copy
9 11 7 12
4 6 1 8
2 10 5 3
 ·  · ·  \n
 · · · \n
 ·  · · \n
Source: NWRRC 2023

Submit

请先 登录

© 2025 FAQs