Madoka and Childish Pranks (Codeforces 1647C)
Tags: Greedy, constructive algorithms

Madoka as a child was an extremely capricious girl, and one of her favorite pranks was drawing on her wall. According to Madoka's memories, the wall was a table of $$$n$$$ rows and $$$m$$$ columns, consisting only of zeroes and ones. The coordinate of the cell in the $$$i$$$-th row and the $$$j$$$-th column ($$$1 \le i \le n$$$, $$$1 \le j \le m$$$) is $$$(i, j)$$$.One day she saw a picture "Mahou Shoujo Madoka Magica" and decided to draw it on her wall. Initially, the Madoka's table is a table of size $$$n \times m$$$ filled with zeroes. Then she applies the following operation any number of times:Madoka selects any rectangular subtable of the table and paints it in a chess coloring (the upper left corner of the subtable always has the color $$$0$$$). Note that some cells may be colored several times. In this case, the final color of the cell is equal to the color obtained during the last repainting.  White color means $$$0$$$, black means $$$1$$$. So, for example, the table in the first picture is painted in a chess coloring, and the others are not. For better understanding of the statement, we recommend you to read the explanation of the first test.Help Madoka and find some sequence of no more than $$$n \cdot m$$$ operations that allows you to obtain the picture she wants, or determine that this is impossible.

Input: Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 100$$$) — the size of the table. Each of the following $$$n$$$ lines contains a string of length $$$m$$$ consisting only of $$$1$$$ and $$$0$$$ — description of the picture that Madoka wants to obtain.

Output: If it is impossible to obtain the given picture, print $$$-1$$$.Otherwise, print in the first line a single integer $$$q$$$ ($$$0 \leq q \leq n \cdot m$$$) — the number of operations you need to obtain the picture. Note that you do not need to minimize the number of operations.Then for each operation (in the order of execution) print a single line containing four numbers — the coordinates of the upper-left corner and the lower-right corner of the rectangle.

Note: The description of the first test case is below.  In the third test case, it is impossible to paint the desired picture.In the fourth test case, the initial table is already the desired picture.

Sample Inputs:
4
4 5
01000
10100
01010
00110
2 3
001
010
3 3
110
101
000
1 1
0

Sample Outputs:
4
1 1 3 3
3 3 4 4
4 3 4 4
4 2 4 3
1
1 2 2 3
-1
0

Solution (Python):
from collections import defaultdict
from copy import deepcopy
import copy

def main():
    l = []
    (n, m) = map(int, input().split())
    l2 = ['0' for i in range(n)]
    for i in range(n):
        l.append(input())
    a = False
    for j in range(m):
        if l[0][j] == '1':
            a = True
            break
    if l[0][0] != '0' and a == True:
        print(-1)
        return
    a = None
    s = ''
    for i in range(n):
        for j in range(m):
            if l[i][j] == '0':
                a = True
                ind1 = i
                ind2 = j
                break
        if a == True:
            break
    if a == None:
        print(-1)
        return
    c = 0
    for i in range(n - 1, 0, -1):
        for j in range(m):
            if l[i][j] == '1':
                s += f'{i} {j + 1} {i + 1} {j + 1}\n'
                c += 1
    for i in range(m - 1, -1, -1):
        if l[0][i] == '1':
            s += f'{1} {i} {1} {i + 1}\n'
            c += 1
    print(c)
    if c != 0:
        print(s.strip())
for i in range(int(input())):
    main()

Solution (Python):
def rotate(s):
    return [[s[j][i] for j in range(len(s))] for i in range(len(s[0]) - 1, -1, -1)]

def cnt(s):
    cnt = 0
    for i in s:
        for j in i:
            if j == '1':
                cnt += 1
    print(f'{cnt}\n')
import sys
input = sys.stdin.readline
print = sys.stdout.write

def main():
    for _ in range(int(input())):
        f = True
        (x, y) = map(int, input().split())
        s = [list(str(input())[:-1]) for _ in range(x)]
        if s[0][0] == '1':
            f = False
        if f:
            cnt(s)
            for i in range(-1, -x - 1, -1):
                for j in range(-1, -y, -1):
                    if s[i][j] == '1':
                        print(f'{x + i + 1} {y + j} {x + i + 1} {y + j + 1}\n')
            for i in range(-1, -x, -1):
                if s[i][0] == '1':
                    print(f'{x + i} {1} {x + i + 1} {1}\n')
        else:
            print(f'-1\n')
main()

Solution (Python):
def solution(box):
    if box[0][0] == 1:
        print(-1)
        return
    res = []
    (m, n) = (len(box), len(box[0]))
    for i in range(m - 1, 0, -1):
        for j in range(n - 1, 0, -1):
            if box[i][j] == 1:
                res.append(f'{i + 1} {j} {i + 1} {j + 1}')
    for i in range(m - 1, 0, -1):
        if box[i][0] == 1:
            res.append(f'{i} {1} {i + 1} {1}')
    for j in range(n - 1, 0, -1):
        if box[0][j] == 1:
            res.append(f'{1} {j} {1} {j + 1}')
    print(len(res))
    for line in res:
        print(line)
    return
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    box = []
    for k in range(n):
        box.append([int(i) for i in input()])
    solution(box)