Program to check whether one point can be converted to another or not in Python

Suppose we have a starting point (sx, sy) and a target point (tx, ty). We need to check whether a sequence of moves exists to transform the start point to the end point. Each move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y).

This problem can be solved using a recursive approach by working backwards from the target point to check if we can reach the starting point.

Algorithm Steps

To solve this problem, we follow these steps ?

  • If sx > tx or sy > ty, return False (impossible to reach target)

  • If sx equals tx, check if (ty-sy) is divisible by sx

  • If sy equals ty, check if (tx-sx) is divisible by sy

  • Otherwise, recursively check both possible previous states: (sx, sy, tx-ty, ty) and (sx, sy, tx, ty-tx)

Example

Let's trace through the example (1,1) ? (4,5) ?

def solve(sx, sy, tx, ty):
    if sx > tx or sy > ty:
        return False
    if sx == tx:
        return (ty - sy) % sx == 0
    if sy == ty:
        return (tx - sx) % sy == 0
    return solve(sx, sy, tx - ty, ty) or solve(sx, sy, tx, ty - tx)

# Test the example
sx, sy = 1, 1
tx, ty = 4, 5
result = solve(sx, sy, tx, ty)
print(f"Can transform ({sx},{sy}) to ({tx},{ty}): {result}")
Can transform (1,1) to (4,5): True

Step-by-Step Transformation

The actual sequence of moves from (1,1) to (4,5) is ?

# Show the transformation steps
moves = [(1, 1)]
x, y = 1, 1

# Move sequence: (1,1) ? (2,1) ? (3,1) ? (4,1) ? (4,5)
x, y = x + y, y  # (1,1) ? (2,1)
moves.append((x, y))

x, y = x + y, y  # (2,1) ? (3,1) 
moves.append((x, y))

x, y = x + y, y  # (3,1) ? (4,1)
moves.append((x, y))

x, y = x, x + y  # (4,1) ? (4,5)
moves.append((x, y))

print("Transformation sequence:")
for i, move in enumerate(moves):
    print(f"Step {i}: {move}")
Transformation sequence:
Step 0: (1, 1)
Step 1: (2, 1)
Step 2: (3, 1)
Step 3: (4, 1)
Step 4: (4, 5)

Testing Multiple Cases

Let's test the function with different input cases ?

def solve(sx, sy, tx, ty):
    if sx > tx or sy > ty:
        return False
    if sx == tx:
        return (ty - sy) % sx == 0
    if sy == ty:
        return (tx - sx) % sy == 0
    return solve(sx, sy, tx - ty, ty) or solve(sx, sy, tx, ty - tx)

# Test multiple cases
test_cases = [
    ((1, 1), (4, 5)),   # True - possible
    ((1, 1), (3, 3)),   # True - possible  
    ((2, 3), (1, 4)),   # False - impossible
    ((1, 2), (5, 7))    # True - possible
]

for (sx, sy), (tx, ty) in test_cases:
    result = solve(sx, sy, tx, ty)
    print(f"({sx},{sy}) ? ({tx},{ty}): {result}")
(1,1) ? (4,5): True
(1,1) ? (3,3): True
(2,3) ? (1,4): False
(1,2) ? (5,7): True

How It Works

The algorithm works backwards from the target point. At each step, we ask: "What could the previous point have been?" Since we can only reach (tx, ty) from either (tx-ty, ty) or (tx, ty-tx), we recursively check if either of these previous states can be reached from the starting point.

Conclusion

This recursive solution efficiently determines if one point can be transformed to another using the given move rules. The key insight is working backwards from the target point and checking all possible previous states until we reach the base cases.

Updated on: 2026-03-26T14:45:23+05:30

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements