Pinely Round 4 (Div. 1 + Div. 2)
G
Grid Reset
Codeforces
2700
原题
← FH →
翻译中…

You are given a grid consisting of $n$ rows and $m$ columns, where each cell is initially white. Additionally, you are given an integer $k$, where $1 \le k \le \min(n, m)$.

翻译中…

You will process $q$ operations of two types:

翻译中…
  • $\mathtt{H}$ (horizontal operation) — You choose a $1 \times k$ rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black.
  • $\mathtt{V}$ (vertical operation) — You choose a $k \times 1$ rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black.
翻译中…

After each operation, if any rows or columns become completely black, all cells in these rows and columns are simultaneously reset to white. Specifically, if all cells in the row and column a cell is contained in become black, all cells in both the row and column will be reset to white.

翻译中…

Choose the rectangles in a way that you can perform all given operations, or determine that it is impossible.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains four integers $n$, $m$, $k$, and $q$ ($1 \le n, m \le 100$, $1 \le k \le \min(n, m)$, $1 \le q \le 1000$) — the number of rows and columns in the grid, the size of the operation rectangle, and the number of operations, respectively.

翻译中…

The second line of each test case contains a string $s$ of length $q$, consisting only of characters $\mathtt{H}$ and $\mathtt{V}$ — the sequence of operation types.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output a single integer $-1$ if it is impossible to perform all the operations.

翻译中…

Otherwise, output $q$ lines. Each line contains two integers $i$, $j$ ($1 \le i \le n$, $1 \le j \le m$) — the coordinates of the top-left cell of the operation rectangle.

翻译中…

If there are multiple solutions, output any of them.

翻译中…

Note

翻译中…

image Illustration of example.

翻译中…

The first operation is horizontal. The operation rectangle starts at $(1,1)$ and is a $1 \times 3$ rectangle. After the operation, cells $(1,1)$, $(1,2)$, and $(1,3)$ become black.

翻译中…

The second operation is vertical. The operation rectangle starts at $(2,1)$ and is a $3 \times 1$ rectangle. After the operation, cells $(2,1)$, $(3,1)$, and $(4,1)$ become black. At this point, the first column becomes completely black, so all cells in the first column are reset to white.

翻译中…

The third operation is vertical. The operation rectangle starts at $(1,1)$ and is a $3 \times 1$ rectangle. After the operation, cells $(1,1)$, $(2,1)$, and $(3,1)$ become black.

翻译中…

The fourth operation is horizontal. The operation rectangle starts at $(2,3)$ and is a $1 \times 3$ rectangle. After the operation, cells $(2,3)$, $(2,4)$, and $(2,5)$ become black.

翻译中…

The fifth operation is horizontal. The operation rectangle starts at $(3,3)$ and is a $1 \times 3$ rectangle. After the operation, cells $(3,3)$, $(3,4)$, and $(3,5)$ become black.

翻译中…

The sixth operation is vertical. The operation rectangle starts at $(2,2)$ and is a $3 \times 1$ rectangle. After the operation, cells $(2,2)$, $(3,2)$, and $(4,2)$ become black. At this point, two rows and one column become completely black, so all cells in these rows and the column are reset to white.

翻译中…

样例

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

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