#K93947. Restore the Garden

    ID: 38532 Type: Default 1000ms 256MiB

Restore the Garden

Restore the Garden

You are given an n x n garden grid where each cell represents a plant’s color. Some cells are pre-filled with integers from 1 to n, and empty cells are denoted by 0. Your task is to restore the garden by filling in the empty cells so that each row and each column becomes a permutation of the numbers 1 through n (i.e. each color appears exactly once in each row and column). This is essentially a partial Latin square completion problem.

If a valid restoration is possible, output YES followed by the restored garden grid. Otherwise, output NO.

Note: The input guarantees that the grid size is small (typically n ≤ 10) so that a backtracking approach is feasible.

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 10), the size of the garden.

The next n lines each contain n integers separated by spaces. The integer 0 represents an empty cell and integers 1 to n represent pre-assigned colors that cannot be changed.

outputFormat

If a complete and valid garden configuration is found, output YES on the first line, followed by n lines each containing n space-separated integers representing the restored grid. If no valid restoration exists, output NO alone.

## sample
4
1 0 3 4
0 3 4 1
3 4 0 2
4 1 2 0
YES

1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3

</p>