# Problem Description
This is a matrix traversal problem requiring implementation of a specific snake-like pattern in a square matrix (N x N). The traversal follows a vertical zigzag pattern, starting from the bottom-right corner and moving in alternating upward and downward directions while progressing from right to left. The function should return the elements in the order they are visited during this traversal.

# Visual Facts
1. The image shows a 3x3 matrix represented as nodes numbered from 1 to 9
2. The nodes are connected by purple arrows showing the traversal direction
3. The matrix elements are arranged in a grid pattern (3 rows × 3 columns)
4. The arrows show three distinct vertical paths:
   - Third column: 9 → 6 → 3 (upward movement: from last row to first row)
   - Second column: 2 → 5 → 8 (downward movement: from first row to last row)
   - First column: 7 → 4 → 1 (upward movement: from last row to first row)
5. The traversal order for this 3x3 matrix is: [9, 6, 3, 2, 5, 8, 7, 4, 1]

# Visual Patterns
1. Column-wise Movement Pattern:
   - Starting from rightmost column
   - Right column (3rd): Movement is upward
   - Middle column (2nd): Movement is downward
   - Left column (1st): Movement is upward

2. Traversal Direction Pattern:
   - The overall progression is from right to left
   - When reaching the top or bottom of a column, move to the next column on the left

3. Sequential Rules:
   - Start from the bottom-right element (9)
   - Each element is visited exactly once
   - Direction alternates with each column


# Solution
def solution(matrix: list[list[int]]) -> list[int]:
    """
    Traverses a given matrix according to the snake pattern shown in the figure.
    
    Parameters:
        matrix (list[list[int]]): A 2D list (N x N matrix) containing integers that need to be traversed.
    
    Returns:
        list[int]: A list of integers representing the traversed elements.
    """
    result = []
    N = len(matrix)
    i = 0
    for col in range(N-1, -1, -1):
        if i % 2 == 0:
            for row in range(N-1, -1, -1):
                result.append(matrix[row][col])
        
        else:
            for row in range(N):
                result.append(matrix[row][col])
        i += 1
    return result


# Test Script
def test():
    test_cases = [
        ([[1, 2], [3, 4]], [4, 2, 1, 3]),
        ([[9, 9, 9], [9, 9, 9], [9, 9, 9]], [9, 9, 9, 9, 9, 9, 9, 9, 9]),
        ([[99, 102], [672, 5]], [5, 102, 99, 672]),
        ([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [9, 6, 3, 2, 5, 8, 7, 4, 1]),
        ([[63, 22, 17], [98, 21, 3], [76, 33, 9]], [9, 3, 17, 22, 21, 33, 76, 98, 63]),
        ([[53, 54, 55], [56, 57, 58], [59, 60, 61]], [61, 58, 55, 54, 57, 60, 59, 56, 53]),
        ([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], [16, 12, 8, 4, 3, 7, 11, 15, 14, 10, 6, 2, 1, 5, 9, 13]),
        ([[54, 39, 74, 11], [8, 43, 13, 27], [73, 18, 45, 9], [63, 67, 30, 40]], [40, 9, 27, 11, 74, 13, 45, 30, 67, 18, 43, 39, 54, 8, 73, 63]),
        ([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]],
         [25, 20, 15, 10, 5, 4, 9, 14, 19, 24, 23, 18, 13, 8, 3, 2, 7, 12, 17, 22, 21, 16, 11, 6, 1]),
        ([[21, 23, 98, 76, 33], [63, 67, 30, 40, 5], [73, 18, 45, 9, 2], [8, 43, 13, 27, 1], [98, 77, 23, 11, 9]],
         [9, 1, 2, 5, 33, 76, 40, 9, 27, 11, 23, 13, 45, 30, 98, 23, 67, 18, 43, 77, 98, 8, 73, 63, 21]),
        
    ]

    for i, (matrix, expected_output) in enumerate(test_cases):
        try:
            output = solution(matrix)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`matrix`: {matrix}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`matrix`: {matrix}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()