The TransCompilator

Solved LeetCode program · 54 of 1,000

#0054 · Spiral Matrix

Medium Array chunk-02.md

Original English source

The matrix is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The rows is 3. The columns is 3. The size is rows times columns. The visited is a list of size zeros. The spiral is an empty list of whole numbers. The row is 0. The column is 0. The down is 0. The right is 1.
For every step from 0 to one less than size.
Place item (row times columns plus column) of matrix into spiral. Set item (row times columns plus column) of visited to 1. The nextrow is row plus down. The nextcolumn is column plus right. The blocked is 0. The olddown is 0. If nextrow is less than 0 or nextrow is at least rows or nextcolumn is less than 0 or nextcolumn is at least columns, set blocked to 1. If blocked is 0 and item (nextrow times columns plus nextcolumn) of visited is 1, set blocked to 1. If blocked is 1, set olddown to down. If blocked is 1, set down to right. If blocked is 1, set right to 0 minus olddown. Increase row by down. Increase column by right.

For each value in spiral, show value.

Open in Playground Browse all 1,000 problems

Expected stdout

1
2
3
6
9
8
7
4
5