Connected Component on a Chessboard (Codeforces 1196E)
Tags: Implement, constructive algorithms

You are given two integers $$$b$$$ and $$$w$$$. You have a chessboard of size $$$10^9 \times 10^9$$$ with the top left cell at $$$(1; 1)$$$, the cell $$$(1; 1)$$$ is painted white.Your task is to find a connected component on this chessboard that contains exactly $$$b$$$ black cells and exactly $$$w$$$ white cells. Two cells are called connected if they share a side (i.e. for the cell $$$(x, y)$$$ there are at most four connected cells: $$$(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)$$$). A set of cells is called a connected component if for every pair of cells $$$C_1$$$ and $$$C_2$$$ from this set, there exists a sequence of cells $$$c_1$$$, $$$c_2$$$, ..., $$$c_k$$$ such that $$$c_1 = C_1$$$, $$$c_k = C_2$$$, all $$$c_i$$$ from $$$1$$$ to $$$k$$$ are belong to this set of cells and for every $$$i \in [1, k - 1]$$$, cells $$$c_i$$$ and $$$c_{i + 1}$$$ are connected.Obviously, it can be impossible to find such component. In this case print "NO". Otherwise, print "YES" and any suitable connected component.You have to answer $$$q$$$ independent queries.

Input: The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries. Then $$$q$$$ queries follow.The only line of the query contains two integers $$$b$$$ and $$$w$$$ ($$$1 \le b, w \le 10^5$$$) — the number of black cells required and the number of white cells required.It is guaranteed that the sum of numbers of cells does not exceed $$$2 \cdot 10^5$$$ ($$$\sum w + \sum b \le 2 \cdot 10^5$$$).

Output: For each query, print the answer to it.If it is impossible to find the required component, print "NO" on the first line.Otherwise, print "YES" on the first line. In the next $$$b + w$$$ lines print coordinates of cells of your component in any order. There should be exactly $$$b$$$ black cells and $$$w$$$ white cells in your answer. The printed component should be connected.If there are several answers, you can print any. All coordinates in the answer should be in the range $$$[1; 10^9]$$$.


Sample Inputs:
3
1 1
1 4
2 5

Sample Outputs:
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5

Solution (Python):
import sys
input = sys.stdin.readline

def output(a, b):
    sys.stdout.write(str(a) + ' ' + str(b) + '\n')
q = int(input())
for testcases in range(q):
    (b, w) = map(int, input().split())
    if w > 4 + (b - 1) * 3 or b > 4 + (w - 1) * 3:
        print('NO')
        continue
    print('YES')
    if b <= w:
        for i in range(b * 2):
            output(4, 3 + i)
        w -= b
        if w:
            output(4, 2)
            w -= 1
        count = 0
        while w:
            if count % 2 == 0:
                output(3, 3 + count // 2 * 2)
            else:
                output(5, 3 + count // 2 * 2)
            count += 1
            w -= 1
    else:
        for i in range(w * 2):
            output(5, 3 + i)
        b -= w
        if b:
            output(5, 2)
            b -= 1
        count = 0
        while b:
            if count % 2 == 0:
                output(4, 3 + count // 2 * 2)
            else:
                output(6, 3 + count // 2 * 2)
            count += 1
            b -= 1

Solution (Python):
for i in range(int(input())):
    (a, b) = map(int, input().split())
    dx = 0
    if a > b:
        (a, b) = (b, a)
        dx = 1
    if b > 3 * a + 1:
        print('NO')
        continue
    print('YES')
    s = a + a + 1
    for j in range(1, s):
        print(f'{j} {3 + dx}')
    tmp_b = a
    count = 0
    while tmp_b < b and count < a:
        tmp_b += 1
        count += 1
        print(f'{count + count} {2 + dx}')
    count = 0
    while tmp_b < b and count < a:
        tmp_b += 1
        count += 1
        print(f'{count + count} {4 + dx}')
    if tmp_b < b:
        print(f'{s} {3 + dx}')

Solution (Python):
import sys
T = int(input())
for _ in range(T):
    (b, w) = map(int, input().split())
    (big, small) = (max(b, w), min(b, w))
    x0 = 2
    y0 = 2 if w < b else 3
    s_list = []
    b_list = []
    for i in range(small):
        s_list.append((x0 + i * 2, y0))
    for i in range(small - 1):
        b_list.append((x0 + i * 2 + 1, y0))
    b_list += [(x0 - 1, y0), (s_list[-1][0] + 1, y0)]
    for i in range(small):
        b_list += [(x0 + i * 2, y0 - 1), (x0 + i * 2, y0 + 1)]
    if len(b_list) < big:
        print('NO')
    else:
        print('YES')
        for s in s_list:
            print(*s)
        for i in range(big):
            print(b_list[i][0], b_list[i][1])