Up and to the Right
Fill an n by n grid with the numbers 1 to n * n, one number per cell, by the following rule. Put 1 in the cell at row row and column col (both counted from 0, with row 0 at the top). After placing a number, look at the cell one row up and one column to the right of it, wrapping around the edges: going up from row 0 lands in row n - 1, and going right from column n - 1 lands in column 0. If that cell is still empty, the next number goes there. If it is already filled, the next number goes in the cell directly below the one just filled instead, again wrapping from row n - 1 to row 0 (that cell is always empty). Return the filled grid as a list of rows.
Example 1
1 starts in the middle of the top row. Up and right from it wraps to the bottom row, so 2 goes in the bottom-right corner, and 3 wraps to the left edge. The cell up and right of 3 already holds 1, so 4 goes directly below 3. Started this way, the rule builds the classic magic square whose rows, columns and diagonals all add up to 15.
Example 2
1 is in the bottom-left cell and 2 goes up and right of it. Up and right of 2 wraps back to the cell of 1, so 3 goes directly below 2, in the bottom row, and 4 wraps around to the top-left cell.
Constraints
1 <= n <= 90 <= row < nand0 <= col < n
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Wireworld StepEasy
- Crush and FallMedium
- Four in a RowMedium
- Life-Like GenerationsMedium
- Minutes to BurnMedium
- Paths Around RocksMedium
- Ramp DropMedium
- Settle the SandpileMedium