Bob's Table

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 0.6s
Memory limit: 256M

Problem types

Bob has a table with N rows and M columns, and he wants to fill each cell with an integer from 1 to K, following a few special rules.

Each cell must contain exactly one integer from 1 to K, and the table must satisfy all of the following constraints:

  • Every number from 1 to K must appear at least once in the table.

  • Each cell must have exactly two adjacent cells (up, down, left, or right) that contain the same number.

  • All cells with the same number must form a connected region — that is, if two cells contain the same number, there must be a path between them using only cells with that number, moving only up, down, left, or right.

Given N, M, and K, can you help Bob fill the table while satisfying these constraints? If it's not possible, just output NO.

Input Specification

The first line contains an integer T (1 \le T \le 10^5), the number of test cases.

Each of the following T lines contains three integers N, M, K, (1 \le N, M  \le 2 \times 10^5, 1 \le K \le N \times M), — the number of rows, columns, and number range. The sum of N \times M for all test cases is not more than 2 \times 10^5.

Output Specification

For each test case, if it is impossible to create a valid garden, print NO. Otherwise, print YES and then a valid solution to fill up the table. Each integer is from 1 to K. If there are multiple ways, you may print any valid solution.

Note that the checker for this problem is custom and, hence, the whitespace is strict, so do not output excess whitespace.

Constraints

Subtask Points Additional constraints
1 14 N, M \le 4
2 16 N \le 4.
3 13 N \le 6.
4 22 N=M.
5 12 K is randomly generated from 1 to N \times M.
6 23 No additional constraints

Sample Input

5
2 2 2
2 2 1
4 4 4
4 4 2
4 6 3

Sample Output

NO
YES
1 1
1 1
YES
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
YES
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
YES
1 1 1 1 1 1
1 2 2 3 3 1
1 2 2 3 3 1
1 1 1 1 1 1

Comments

There are no comments at the moment.