Snake Matrix Path
Medium
matrix
traversal
zigzag
pattern
Problem Description
Given an m×n matrix, print its elements in a zigzag "snake" pattern. Start from top-left, go right in first row, then left in second row, then right in third row, and so on.
Input Format
First line contains m and n. Next m lines contain n space-separated integers each.
Output Format
Elements in snake pattern, space-separated on a single line.
Constraints
• 1 ≤ m, n ≤ 100
• -1000 ≤ matrix elements ≤ 1000
• Alternate row direction: right, left, right, left...
• Start from top-left corner
• Output all elements in traversal order
Sample Input/Output
Input:
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Output:
1 2 3 4 8 7 6 5 9 10 11 12
Explanation
Row 0: 1→2→3→4 (left to right). Row 1: 8→7→6→5 (right to left). Row 2: 9→10→11→12 (left to right).
Solution Hints
80
Points
Points
2000ms
Time Limit
Time Limit
128MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now