Does the problem name look familiar? This problem will very much haunt me for life, as one made a submission on it 9 seconds before the contest ended, leaving an unremovable stain in my mind.
__jk__ and onbert are playing a game on a rectangular grid of $n$ rows and $m$ columns, where at least one of $n$ and $m$ is even. For positive integers $1 \le r \le n$ and $1 \le c \le m$, we denote cell $(r,c)$ as the $c$-th cell in the $r$-th row. You can assume that all cells have lengths and widths $1$.
Each cell of the grid is coloured either black or white. Moreover, the grid has exactly $\frac{nm}{2}$ cells coloured black, and the remaining exactly $\frac{nm}{2}$ cells coloured white.
__jk__ has full control of all the black cells. For every (unordered) pair of black cells, he draws a directed arrow between the centre of the two cells. Note that __jk__ can choose which direction he wants each arrow to be drawn.
On the other hand, onbert has full control of the all the white cells. Similarly, for every (unordered) pair of white cells, he draws a directed arrow between the centre of the two cells. Note that onbert can choose which direction he wants each arrow to be drawn as well.
onbert once invited __jk__ to play Overcooked (a cooperative game) but __jk__ blundered an average of $998244853$ times per game. Now, __jk__ wants to convince onbert that they can be a good pair (of cooperative game players). Is it possible to direct for both of them to direct their arrows in such a way that the vector sum of the $\frac{nm}{2}(\frac{nm}{2}-1)$ edges sum to $\vec{0}$? Help __jk__ by giving a valid way of directed arrows for the two players, or report that no such valid ways exist.
Informally, suppose there are variables $x$ and $y$, both initially set to be $0$. For each arrow drawn by either __jk__ or onbert, suppose the arrow is drawn from cell $(a,b)$ to cell $(c,d)$, then add $c-a$ to $x$ and add $d-b$ to $y$. You have to direct the arrows in such a way that the final values of $x$ and $y$ are both $0$, or report that it is impossible to do so.
Input
The first line of the input contains two positive integers $n$ and $m$, denoting the number of rows and columns of the grid.
Next $n$ lines of the input contains $m$ characters each, denoting the colour of each cell of the grid. The $j$-th character in the $i$-th row is 0
if cell $(i,j)$ is coloured black, and 1
if cell $(i,j)$ is coloured white.
All test cases are generated such that exactly $\frac{nm}{2}$ cells are 0
and exactly $\frac{nm}{2}$ cells are 1
.
Output
On the first line, output YES
if it is possible to direct the edges, or NO
if it is impossible to direct the edges such that they sum to $\vec{0}$.
If you have outputted YES
on the first line, output $\frac{nm}{2}(\frac{nm}{2}-1)$ more lines, each containing $4$ positive integers, representing the values of $r_1$, $c_1$, $r_2$, and $c_2$ respectively. This means that a direct edge is drawn from the centre of the cell $(r_1,c_1)$ to the centre of the cell $(r_2,c_2)$. You may output the edges in any arbitrary order. If there are multiple valid constructions, output any one of them.
Note that your output must satisfy the following requirements, otherwise it will be treated as incorrect:
- $1 \le r_1,r_2 \le n$.
- $1 \le c_1,c_2 \le m$.
- $(r_1,c_1) \neq (r_2,c_2)$.
- The colours of cell $(r_1,c_1)$ and cell $(r_2,c_2)$ are the same.
- For each unordered pair of cells $\{(i,j),(k,l)\}$ where cells $(i,j) \neq (k,l)$ have the same colour, there is exactly one line such that $(r_1,c_1,r_2,c_2) = (i,j,k,l)$ or $(k,l,i,j)$.
If you have outputted NO
on the first line, do not output any additional lines.
Constraints
For all test cases, $2 \le nm \le 1000$. $nm$ is even. There are exactly $\frac{nm}{2}$ cells coloured black and exactly $\frac{nm}{2}$ cells coloured white.
Subtask 1 (4 points): $n=1$. The leftmost $\frac{m}{2}$ cells are coloured black, and the rightmost $\frac{m}{2}$ cells are coloured white.
Subtask 2 (13 points): $n=1$.
Subtask 3 (22 points): $n=2$.
Subtask 4 (11 points): $nm \le 12$.
Subtask 5 (19 points): $nm$ is NOT a multiple of $4$.
Subtask 6 (12 points): $nm$ is a multiple of $8$.
Subtask 7 (11 points): $nm \ge 250$. The colours of the cells are uniformly randomly generated among all configurations of $n \times m$ grids with exactly $\frac{nm}{2}$ cells black and exactly $\frac{nm}{2}$ white.
Subtask 8 (8 points): No additional constraints.
Sample Test Cases
Input | Output | |
---|---|---|
1 6 010101 |
YES 1 1 1 3 1 3 1 5 1 5 1 1 1 2 1 4 1 4 1 6 1 6 1 2 |
|
2 2 01 01 |
YES 2 2 1 2 1 1 2 1 |
|
2 6 101010 010101 |
NO |
Scoring: Per Subtask
Authored by jkjkjk
Appeared in Joint-School Pre-HKOI Mini Competition 1