# Problem Description
This is a 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,n), we need to follow a predefined path that involves moving left initially, then following a zigzag pattern down and up while moving right, until reaching the endpoint at (2,n).

# 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-right)
   - 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,n) (top-right corner)

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

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

   c. Zigzag Pattern:
      - Moves downward to bottom row
      - Takes one step right
      - Moves upward until reach the second row
      - Takes one step right
      - Moves downward 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 right
   - The pattern continues until reaching position (2,n)

3. Boundary Behaviors:
   - Never crosses the matrix boundaries
   - Uses the full height of the matrix in vertical movements
   - The end point is always at the second row, last column (2,n)


# 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 (n,1) (top-right 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 < n:
        return [1, n - k]
    else:
        remain_step = k - (n)
        i = 1
        j = 0
        move_downward = True
        pre_direction = not move_downward
        while remain_step>0:
            if m == 2:
                j+=1
            elif (i < m - 1 and i > 1) or (pre_direction != move_downward) :
                i += 1 if move_downward else -1
                pre_direction = move_downward

            else:
                # move rightward
                j += 1
                move_downward = not move_downward

            remain_step -= 1
    
    return [i+1, j+1]

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

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