Neighbor Grid (Codeforces 1375B)
Tags: Greedy, constructive algorithms

You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k > 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.

Input: The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively.The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$).It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.

Output: If it is impossible to obtain a good grid, print a single line containing "NO".Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero).If there are multiple possible answers, you may print any of them.

Note: In the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.

Sample Inputs:
5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0

Sample Outputs:
YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0

Solution (Python):
t = int(input())
for _ in range(t):
    (n, m) = list(map(int, input().split()))
    ar = []
    ans = 'yes'
    for i in range(n):
        line = list(map(int, input().split()))
        if i == 0 or i == n - 1:
            if line[0] > 2 or line[-1] > 2:
                ans = 'no'
            if max(line) > 3:
                ans = 'no'
            ar.append(' '.join(list('2' + '3' * (m - 2) + '2')))
        else:
            if line[0] > 3 or line[-1] > 3:
                ans = 'no'
            if max(line) > 4:
                ans = 'no'
            ar.append(' '.join(list('3' + '4' * (m - 2) + '3')))
    if ans == 'yes':
        print('YES')
        print('\n'.join(ar))
    else:
        print('NO')

Solution (Python):
list_ans = []

def panduan(list1, i):
    if i == 0 or i == n - 1:
        if list1[0] > 2 or list1[-1] > 2:
            return 1
        for x in list1[1:-1]:
            if x > 3:
                return 1
    else:
        if list1[0] > 3 or list1[-1] > 3:
            return 1
        for x in list1[1:-1]:
            if x > 4:
                return 1
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    listx = []
    for i in range(n):
        listx.append(list(map(int, input().split())))
    for i in range(n):
        list1 = listx[i][:]
        if panduan(list1, i) == 1:
            list_ans.append('NO')
            break
    else:
        list_ans.append('YES')
        list_ans.append('2 ' + '3 ' * (m - 2) + '2')
        for i in range(n - 2):
            list_ans.append('3 ' + '4 ' * (m - 2) + '3')
        list_ans.append('2 ' + '3 ' * (m - 2) + '2')
for item in list_ans:
    print(item)

Solution (Python):
import sys
from math import factorial
input = sys.stdin.readline
mod = int(1e+16 + 1)
infi = float('inf')
alpha = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def comding():
    (n, m) = tup()
    a = [ils() for i in range(n)]
    check = True
    for i in range(n):
        for j in range(m):
            c = 4
            if i == 0 or i == n - 1:
                c -= 1
            if j == 0 or j == m - 1:
                c -= 1
            if a[i][j] > c:
                check = False
                break
            else:
                a[i][j] = c
    if check:
        print('YES')
        for i in a:
            print(*i)
    else:
        print('NO')
    return

def si():
    return input()

def ii():
    return int(input())

def tup():
    return map(int, input().split())

def ls():
    return [x for x in input().split()]

def sls():
    return [map(str, input().split())]

def ils():
    return [int(x) for x in input().split()]

def ncr(n, r):
    return factorial(n) // (factorial(r) * factorial(n - r))

def sils():
    return sorted([int(x) for x in input().split()])

def ceil(x, y):
    if x % y == 0:
        return x // y
    else:
        return x // y + 1

def gcd(x, y):
    while y:
        (x, y) = (y, x % y)
    return x

def isPrime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i = i + 6
    return True
for __ in range(ii()):
    comding()