# 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 down initially, then following a zigzag pattern right and left while moving up, until reaching the endpoint at (1,2)(stop when moving left) or (1,n)(stop when moving right).

# Visual Facts
1. The image shows 4 different matrix examples of varying sizes:
   - Example 1: 2×4 matrix
   - Example 2: 6×3 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 downward movement along the first column
      - Moves down until reaching the bottom row
   
   b. Zigzag Pattern:
      - Moves right to rightmost column
      - Takes one step up
      - Moves left to the **second** column
      - Takes one step up
      - Moves right to rightmost column
      - Repeats this zigzag pattern

2. Pattern Rules:
   - The path never returns to the first column after the initial downward movement
   - Movement alternates between going right to the rightmost column and left to the **second** column
   - After each horizontal movement (riight or left), there's always one step up
   - The pattern continues until reaching position (1,2) or (1,n)

3. Boundary Behaviors:
   - Never crosses the matrix boundaries
   - The end point is always at the first row (1,2) or (1,n)

4. Example:
   Suppose the shape of matrix is (3, 4). 
   a. Initial Phase:
   Moves down until reaching the bottom row: (1,1) -> (2,1) -> (3,1)
   
   b. Second Phase:
   Moves one step rightward to the second row: (3,1) -> (3,2)

   c. Zigzag Pattern:
   Moves right to rightmost column: (3,2) -> (3,3) -> (3,4)
   Takes one step up: (3,4) -> (2,4)
   Moves left to second column: (2,4) -> (2,3) -> (2,2) 
   Takes one step up:  (2,2) -> (1,2)
   Moves right to rightmost column: (1,2) -> (1,3) -> (1,4)


# 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.
    """
    if k < m:
        return [k + 1, 1]
    k -= (m - 1)
    i = m - 1   
    j = 0
    direct_status = "right"
    while k > 0:
        if direct_status == "right":
            j += 1
            if  j == n - 1 and i != 0 :
                direct_status = "up"
            
        elif direct_status == "up":
            i -= 1
            if j == 1:
                direct_status = "right"
            elif j == n - 1:
                direct_status = "left"
            else:
                assert False
        elif direct_status == "left":
            j -= 1
            if j == 1 and i != 0:
                direct_status = "up"
        k -= 1
    return [i + 1, j + 1]     
            


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

    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()