# Problem Description
This is a matrix traversal problem requiring implementation of a specific horizontal "snake-like" pattern traversal of a square matrix (N x N). Starting from the bottom-right corner, the traversal follows a horizontal zigzag pattern, alternating between leftward and rightward movements while progressing upward row by row. The function needs to 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 horizontal paths:
   - Bottom row: 9 → 8 → 7 (leftward movement)
   - Middle row: 4 → 5 → 6 (rightward movement)
   - Top row: 3 → 2 → 1 (leftward movement)
5. The traversal sequence for this 3x3 matrix is: [9, 8, 7, 4, 5, 6, 3, 2, 1]

# Visual Patterns
1. Row-wise Movement Pattern:
   - Bottom-to-top progression overall
   - Bottom row: Movement is leftward
   - Middle row: Movement is rightward
   - Top row: Movement is leftward

2. Sequential Rules:
   - Start from the bottom-right element (9)
   - Each element is visited exactly once
   - Movement continues until reaching the top-left element (1)

3. Boundary Pattern:
   - When reaching the leftmost element in a leftward movement, move up
   - When reaching the rightmost element in a rightward movement, move up
   - The pattern terminates after processing all elements

# 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 row in range(N-1, -1, -1):
        if i % 2 == 0:
            for col in range(N-1, -1, -1):
                result.append(matrix[row][col])
        
        else:
            for col in range(N):
                result.append(matrix[row][col])
        i += 1
    return result


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