# Problem Description
This is a mxn matrix traversal problem where we need to calculate the final position after k steps following a specific movement pattern in an m×n matrix. Starting from position (1,1), we need to follow a predefined path that involves moving right initially, then following a zigzag pattern up and down while moving left, until reaching the endpoint at (2,1)(multiple rows) or (1,n)(only a sigle row).

# Visual Facts
1. The image shows 4 different matrix examples of varying sizes:
   - Example 1: 1×6 matrix
   - Example 2: 2×4 matrix
   - Example 3: 3×4 matrix
   - Example 4: 6×6 matrix

2. Each example has:
   - A blue circle marking the start point (always at top-left)
   - A purple circle marking the end point
   - Orange arrows showing the movement path
   - Clear grid lines defining the matrix structure

3. All examples start at position (1,1) (top-left corner)

4. The path is marked with continuous orange arrows showing direction

# Visual Patterns
1. Movement Pattern Sequence:
   a. Initial Phase:
      - Always starts with rightward movement along the first row
      - Moves right until reaching the last column
   
   b. Second Phase:
      - Makes one step down after reaching the right edge

   b. Zigzag Pattern:
      - Moves down to bottom row
      - Takes one step left
      - Moves up to the **second** row
      - Takes one step left
      - Moves down to bottom row
      - Repeats this zigzag pattern

2. Pattern Rules:
   - The path never returns to the first row after the initial rightward movement
   - Movement alternates between going down to the bottom row and up to the **second** row
   - After each vertical movement (up or down), there's always one step left
   - The pattern continues until reaching position (2,1)(if multiple rows) or (1,n)(if a single row)

3. Boundary Behaviors:
   - Never crosses the matrix boundaries
   - The pattern continues until reaching position (2,1)


# Solution
def solution(m: int, n: int, k: int) -> list[int, int]:        
    """
    Given an m×n matrix, determine the coordinates after k steps, starting from position (1,1) (top-left corner).
    Each step can only move to an adjacent cell following a specific movement pattern.

    Parameters:
        m (int): Number of rows in the matrix.
        n (int): Number of columns in the matrix.
        k (int): Number of steps to move.

    Returns:
        list[int, int]: Final coordinates [row, column] after k steps.
    """
    row, col = (1, 1)
    if k < n - 1:
        return [1, k + 1]
    k -= n - 1
    row, col = (1, n)
    if k == 0:
        return [row, col]
    k -= 1
    row += 1
    while k > 0:
        if row == 2:
            steps_down = min(k, m - 2)
            row += steps_down
            k -= steps_down
        elif row == m:
            steps_up = min(k, m - 2)
            row -= steps_up
            k -= steps_up
        if k > 0:
            k -= 1
            col -= 1
        if col == 1:
            row = 2
            break
    return [row, col]


# Test Script
def test():
    test_cases = [
        (4, 4, 15, [2, 1]),
        (4, 4, 6, [4, 4]),
        (4, 4, 11, [3, 2]),
        (4, 4, 1, [1, 2]),
        (6, 6, 35, [2, 1]),
        (6, 6, 21, [6, 3]),
        (6, 6, 4, [1, 5]),
        (6, 6, 13, [4, 5]),
        (6, 4, 2, [1, 3]),
        (6, 4, 7, [5, 4]),
        (6, 4, 23, [2, 1]),
        (4, 6, 23, [2, 1]),
        (4, 6, 8, [4, 6]),
        (4, 6, 20, [4, 2]),
    ]

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

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

test()