#C5750. Zigzag Grid Pattern

    ID: 49434 Type: Default 1000ms 256MiB

Zigzag Grid Pattern

Zigzag Grid Pattern

In this problem, you are given a positive integer ( n ) and you need to generate an ( n \times n ) grid filled with consecutive integers in a zigzag (or snake) pattern. Specifically, the grid is filled row by row such that:

  • For even-indexed rows (0, 2, 4, ... using 0-indexing), numbers are filled from left to right.
  • For odd-indexed rows (1, 3, 5, ...), numbers are filled from right to left.

After filling the grid, you must format the output so that each number is right-justified within a field width of 3. The program will continuously read integers from standard input and for each integer ( n ) (until a 0 is encountered), output the formatted grid followed by an empty line.

For example, when ( n = 3 ), the grid is constructed as follows:

Row 0 (even): 1, 2, 3 Row 1 (odd): 6, 5, 4 Row 2 (even): 7, 8, 9

The formatted output should appear as:

1 2 3 6 5 4 7 8 9

inputFormat

The input consists of multiple integers separated by whitespace. Each integer represents the size ( n ) of the grid to generate. The input terminates when a 0 is encountered. Do note that the 0 is not processed.

outputFormat

For each grid, output the ( n \times n ) grid with each number right-justified in a 3-digit format. Each row of the grid should be printed on a new line, and after printing each grid, output an extra empty line.## sample

2
0
  1   2

4 3

</p>