DeepInfra Round 1(Codeforces Round 1126,Div. 1 + Div. 2)
G
网格填充
Grid Filling
Codeforces
3100
原题
← FH1 →

网格填充AI (CI翻译)

You are given a grid of size $n \times m$. The rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. Two cells are adjacent if they share a side.

给定一个大小为 $n \times m$ 的网格。行从上到下编号为 $1$ 到 $n$,列从左到右编号为 $1$ 到 $m$。如果两个格子共用一条边,则称它们相邻。AI (CI翻译)

There are $h$ horizontal and $v$ vertical cuts in the grid. The $i$-th horizontal cut goes between rows $r_i$ and $r_i + 1$, and the $j$-th vertical cut goes between columns $c_j$ and $c_j + 1$; every cut crosses the whole grid. Together with the borders of the grid, the cuts split it into $(h + 1) \cdot (v + 1)$ rectangular pieces.

网格中有 $h$ 条横向切割线和 $v$ 条纵向切割线。第 $i$ 条横向切割线位于第 $r_i$ 行与第 $r_i + 1$ 行之间,第 $j$ 条纵向切割线位于第 $c_j$ 列与第 $c_j + 1$ 列之间;每条切割线都贯穿整个网格。这些切割线与网格边界一起,将网格分成 $(h + 1) \cdot (v + 1)$ 个矩形区域。AI (CI翻译)

Write a positive integer in every cell of the grid so that both of the following conditions hold:

请在网格的每个格子中填入一个正整数,使以下两个条件都成立:AI (CI翻译)

  • any two adjacent cells contain different integers, even if these cells belong to different pieces;
  • every piece consisting of $x$ cells contains each of the integers $1, 2, \ldots, x$ exactly once.
  • 任意两个相邻格子中的整数都不同,即使它们属于不同的区域;
  • 每个由 $x$ 个格子组成的区域中,整数 $1, 2, \ldots, x$ 都恰好出现一次。
AI (CI翻译)

Find any way to fill the grid satisfying these conditions, or determine that it is impossible.

请找出一种满足条件的填法,或者判断无解。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

每个输入包含多个测试用例。第一行包含测试用例数量 $t$ ($1 \le t \le 1000$)。接下来是各个测试用例的描述。AI (CI翻译)

The first line of each test case contains four integers $n$, $m$, $h$, and $v$ ($1 \le n, m \le 1000$, $0 \le h \lt n$, $0 \le v \lt m$) — the number of rows of the grid, the number of columns of the grid, the number of horizontal cuts, and the number of vertical cuts.

每个测试用例的第一行包含四个整数 $n$、$m$、$h$ 和 $v$ ($1 \le n, m \le 1000$, $0 \le h \lt n$, $0 \le v \lt m$)——分别表示网格的行数、列数、横向切割线数量和纵向切割线数量。AI (CI翻译)

The next line contains $h$ integers $r_1, r_2, \ldots, r_h$ ($1 \le r_1 \lt r_2 \lt \ldots \lt r_h \lt n$) — the positions of the horizontal cuts. If $h = 0$, this line is not present.

下一行包含 $h$ 个整数 $r_1, r_2, \ldots, r_h$ ($1 \le r_1 \lt r_2 \lt \ldots \lt r_h \lt n$)——表示横向切割线的位置。如果 $h = 0$,则不存在这一行。AI (CI翻译)

The next line contains $v$ integers $c_1, c_2, \ldots, c_v$ ($1 \le c_1 \lt c_2 \lt \ldots \lt c_v \lt m$) — the positions of the vertical cuts. If $v = 0$, this line is not present.

下一行包含 $v$ 个整数 $c_1, c_2, \ldots, c_v$ ($1 \le c_1 \lt c_2 \lt \ldots \lt c_v \lt m$)——表示纵向切割线的位置。如果 $v = 0$,则不存在这一行。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.

保证所有测试用例的 $n$ 之和不超过 $1000$。AI (CI翻译)

It is guaranteed that the sum of $m$ over all test cases does not exceed $1000$.

保证所有测试用例的 $m$ 之和不超过 $1000$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, if there is no valid way to fill the grid, output $-1$.

对于每个测试用例,如果不存在满足条件的填法,输出 $-1$。AI (CI翻译)

Otherwise, output $n$ lines. The $i$-th of these lines should contain $m$ integers — the numbers written in the cells of the $i$-th row of the grid, from left to right.

否则,输出 $n$ 行。第 $i$ 行应包含 $m$ 个整数,表示网格第 $i$ 行各个格子中填入的数,从左到右排列。AI (CI翻译)

If there are multiple valid ways to fill the grid, output any of them.

如果有多种满足条件的填法,输出任意一种即可。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first example, one possible way to fill the grid is shown below:

在第一个样例中,一种可能的填法如下图所示:AI (CI翻译)

image

imageAI (CI翻译)

In the third example, the dashed lines represent the cuts. One possible way to fill the grid is shown below:

在第三个样例中,虚线表示切割线。一种可能的填法如下图所示:AI (CI翻译)

image

imageAI (CI翻译)

In the fourth example, it can be shown that no valid filling exists.

在第四个样例中,可以证明不存在满足条件的填法。AI (CI翻译)

样例

Input #1
4
3 3 0 0
1 6 0 2
1 3
5 5 2 2
2 3
2 3
4 4 2 2
1 2
1 2
Output #1
1 2 3
4 5 6
7 8 9
1 2 1 2 1 3
1 2 1 2 1
3 4 2 3 4
1 2 1 2 1
3 4 2 3 4
1 2 1 2 1
-1
InputCopy OutputCopy
题目信息
题号
2271G
时间限制
2 s
内存限制
256 MB
标签
构造
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。