Codeforces Round 1030 (Div. 2)
E
Grid Coloring
Codeforces
2400
原题
← D2F →
翻译中…

There is a $n\times m$ grid with each cell initially white. You have to color all the cells one-by-one. After you color a cell, all the colored cells furthest from it receive a penalty. Find a coloring order, where no cell has more than $3$ penalties.

翻译中…

Note that $n$ and $m$ are both odd.

翻译中…

The distance metric used is the chessboard distance while we decide ties between cells with Manhattan distance. Formally, a cell $(x_2, y_2)$ is further away than $(x_3, y_3)$ from a cell $(x_1, y_1)$ if one of the following holds:

翻译中…
  • $\max\big(\lvert x_1 - x_2 \rvert, \lvert y_1 - y_2 \rvert\big) \gt \max\big(\lvert x_1 - x_3 \rvert, \lvert y_1 - y_3 \rvert\big)$
  • $\max\big(\lvert x_1 - x_2 \rvert, \lvert y_1 - y_2 \rvert\big)=\max\big(\lvert x_1 - x_3 \rvert, \lvert y_1 - y_3 \rvert\big)$ and $\lvert x_1 - x_2 \rvert + \lvert y_1 - y_2 \rvert \gt \lvert x_1 - x_3 \rvert + \lvert y_1 - y_3 \rvert$
翻译中…

It can be proven that at least one solution always exists.

翻译中…

image Example showing penalty changes after coloring the center of a $5 \times 5$ grid. The numbers indicate the penalty of the cells.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains two odd integers $n$ and $m$ ($1 \le n, m \le 4999$) — the number of rows and columns.

翻译中…

It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $5000$.

翻译中…

Output

翻译中…

For each test case, output $n \cdot m$ lines where the $i$-th line should contain the coordinates of the $i$-th cell in your coloring order. If there are multiple solutions, print any of them.

翻译中…

The empty lines in the example output are just for increased readability. You're not required to print them.

翻译中…

Note

翻译中…

In the first test case, the grid can be colored as follows:

翻译中…

image The numbers indicate the penalty of the cells.

翻译中…

样例

Input #1
3
3 3
1 1
1 5
Output #1
2 1
2 3
2 2
1 1
3 2
3 3
3 1
1 3
1 2

1 1

1 2
1 4
1 5
1 1
1 3
InputCopy OutputCopy
题目信息
题号
2118E
时间限制
2 s
内存限制
256 MB
标签
构造
计算几何
贪心
数学

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