#C11508. Prime Grid Snake Order
Prime Grid Snake Order
Prime Grid Snake Order
You are given an integer n
. Your task is to generate an n x n
grid filled with prime numbers. The grid should be filled in a snake order: for even-indexed rows (0-indexed) fill from left to right; for odd-indexed rows, fill from right to left. All primes in the grid must be unique, which naturally guarantees that no two adjacent cells (horizontally, vertically, or diagonally) share the same value.
Example 1:
Input: 1 Output: 2
Example 2:
Input: 2 Output: 2 3 7 5
Example 3:
Input: 3 Output: 2 3 5 13 11 7 17 19 23
Note: The grid is filled with the first n*n
prime numbers in the specified snake order.
inputFormat
The input consists of a single integer n
(1 ≤ n ≤ 50) taken from standard input.
outputFormat
Output the n x n
grid of prime numbers. Each of the n
lines should contain n
space-separated integers representing a row of the grid, following the snake order.
1
2
</p>