#P10085. Alice's Binary Grid Pattern
Alice's Binary Grid Pattern
Alice's Binary Grid Pattern
Alice is fascinated by binary. She believes that only things that have something to do with binary are beautiful. One day, she imagined a pattern and decided to draw it on a grid whose width and height are both 2^n. The grid cells are either black or white, and initially all cells are white.
Alice defines a painting operation as follows: choose a cell; then, that cell and its four neighbors (up, down, left, right) have their colors inverted (black becomes white and white becomes black). Note that the grid is on a torus – the first row and the last row are adjacent, and similarly the first column and the last column are adjacent.
Your task is to either output a sequence of operations to achieve her desired pattern or indicate that it is unsolvable. If there are multiple valid solutions, output any one of them.
Note: The operations you output must transform an all-white grid into the given target pattern. The toggle effect of an operation on a cell is done modulo 2 (i.e. applying the same operation twice cancels out).
inputFormat
The input begins with an integer n
on the first line. The grid has size 2^n x 2^n
. Following this are 2^n
lines, each containing a string of exactly 2^n
characters. Each character is either '0' (representing a white cell) or '1' (representing a black cell), which describes the desired target pattern.
outputFormat
If a solution exists, output the number of operations m
on the first line, followed by m
lines each containing two integers. Each pair of integers (r, c) indicates that you should perform the painting operation on the cell in the r-th row and c-th column (1-indexed). If no solution exists, simply output -1
(without quotes).
sample
1
10
00
1
1 1
</p>