Loops

시간 제한2초메모리 제한1024 MB

요약
n 곱하기 m 격자의 모든 2x2 정사각형에 대한 루프 모양이 주어질 때, 그 모양을 만드는 1부터 nm까지의 서로 다른 정수 행렬을 복원한다.
난이도

어려움10점 중 8점

유형
구현, 그리디, 완전 탐색
정답자
아직 제출이 없습니다

문제

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

There are three possible loop shapes we can get:

Now consider an n×mn\times m matrix filled with distinct integers from 11 to nmnm, inclusive. Each 2×22\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:

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

입력

The first line contains two integers nn and mm (2≤n,m≤5002\le n, m\le 500).

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

출력

Print an n×mn\times m matrix filled with distinct integers from 11 to nmnm, 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.

예제1

  1. 예제 1

    입력
    3 4
    113
    231
    
    예상 출력
    9 11 7 12
    4 6 1 8
    2 10 5 3