Submission details
Task:Ruudukko
Sender:Legu
Submission time:2025-09-27 13:36:21 +0300
Language:Python3 (PyPy3)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.12 sdetails

Code

grid = []
n = None
m = None

def can_put(x, row, col):
    if row > 0 and abs(grid[row-1][col] - x) == 1:
        return False
    if row < n - 1 and abs(grid[row+1][col] - x) == 1:
        return False
    if col > 0 and abs(grid[row][col-1] - x) == 1:
        return False
    if col < m - 1 and abs(grid[row][col+1] - x) == 1:
        return False
    return True

def solve(step):
    if step == n * m:
        return True
    for row in range(n):
        for col in range(m):
            if grid[row][col] != -1:
                continue
            if can_put(step + 1, row, col):
                grid[row][col] = step + 1
                if solve(step + 1):
                    return True
                grid[row][col] = -1
    return False

def main():
    global grid, n, m
    t = int(input())
    for _ in range(t):
        n, m = (int(x) for x in input().split())
        grid = []
        for _ in range(n):
            grid.append([-1] * m)
        if solve(0):
            print("YES")
            for i in range(n):
                print(*grid[i])
        else:
            print("NO")


if __name__ == "__main__":
    main()

Test details

Test 1

Verdict: ACCEPTED

input
100
1 1
1 2
2 1
1 3
...

correct output
YES
1
NO
NO
NO
...

user output
YES
1
NO
NO
NO
...