How snake traversal works
Mow a lawn. You push the mower to the far end, turn around, and come back along the next strip. Nobody walks back to the start of the lawn to begin every strip from the same side. Snake traversal is that idea on a grid: row 0 goes left to right, row 1 goes right to left, row 2 goes left to right again.
Compared with row-major order the set of cells is the same and the number of steps is the same. What changes is the path: consecutive cells are always neighbours, so there is never a long jump from the end of one row to the start of the next.
In the visualizer the direction shows as a variable, and the first cell of each new row is marked as a turn, the moment the path swings around.
The loop shape
The outer loop is unchanged: i from 0 to rows − 1. The inner loop depends on whether the row is even or odd. Even rows (i % 2 == 0) run j upwards; odd rows run it downwards from cols − 1.
If you dislike two inner loops, count steps instead of columns: let n run from 0 to cols − 1 and set j = n on even rows, j = cols − 1 − n on odd rows. One loop, one formula, same path.
A worked example: a 3 × 4 grid
Number the cells 1 to 12 in reading order. Row 0 reads forwards, row 1 backwards, row 2 forwards:
Look at the joins: 4 is next to 8, and 5 is next to 9. Every pair of consecutive visits touches. There are only two turns on three rows, and the Visited counter still ends on 12.
Where you'll meet it
- Printers, plotters and 3D printers. The head sweeps back and forth; returning to the left edge every line would double the travel.
- Robot vacuums and lawn mowers. Boustrophedon coverage is the standard way to cover a rectangle.
- Interview questions. “Print the matrix in snake pattern” and “zigzag order” both mean this.
- Grid puzzles and games. Snakes and Ladders numbers its board exactly this way.
Mistakes beginners make
- Reversing the values instead of the loop. You do not need to reverse a row; you need to read it from the other end.
- Starting the odd rows at
cols. The last valid column iscols − 1. - Checking the wrong index. The parity test is on the row
i, not onj.
Rows and columns are not the only lines through a grid. Next: Diagonal traversal →
Snake traversal questions
What is snake traversal of a matrix?
A traversal that walks the first row left to right, the second row right to left, the third left to right, and so on. The path snakes down the grid without ever jumping back to the start of a row. It is also called zigzag or boustrophedon order.
How do I reverse the loop on odd rows?
Check i % 2. On even rows run j from 0 up to cols − 1; on odd rows run j from cols − 1 down to 0. A neat alternative is a single loop over n from 0 to cols − 1 with j = i % 2 == 0 ? n : cols − 1 − n.
Where is snake order used?
Anywhere a moving head should not waste a trip back to the start: dot-matrix and inkjet printers, plotters, CNC machines and 3D printer infill all sweep back and forth. Ancient Greek inscriptions were sometimes written this way too, which is where the name boustrophedon, ox-turning, comes from.
Does snake traversal visit every cell?
Yes, exactly once, so it takes rows × columns steps like row-major order. The only thing that changes is the direction on odd rows, which is why the visualizer marks the first cell of each new row as a turn.