Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum jumps to reach home in Python
Suppose there is an array called forbidden, where forbidden[i] indicates that the bug cannot jump to the position forbidden[i], and we also have three values a, b, and x. A bug's home is at position x on the number line. It is at position 0 initially. It can jump by following rules ?
Bug can jump exactly a positions to the right.
Bug can jump exactly b positions to the left.
Bug cannot jump backward twice in a row.
Bug cannot jump to any forbidden positions given in the array.
Bug can jump forward beyond its home, but it cannot jump to positions numbered with negative values.
We have to find the minimum number of jumps required for the bug to reach the destination. If there is no such possible sequence, then return -1.
So, if the input is like forbidden = [2,3,7,9, 12], a = 4, b = 2, x = 16, then the output will be 7 because, starting from 0, jump a = 4 forward twice to reach 4 and 8, but cannot jump to 12 as this is forbidden, then step back b = 2 to 6, then jump to 10, 14, 18 and then two back to reach 16, so there are 7 steps.
Algorithm
To solve this, we will follow these steps ?
Initialize queue with tuple (0,0,True) representing (position, jumps, can_go_backward)
Convert forbidden list to set for O(1) lookup
Set limit as a + b + maximum of x and maximum forbidden position
-
Use BFS to find minimum jumps:
For each position, try jumping forward by 'a' steps
If previous jump wasn't backward, try jumping backward by 'b' steps
Skip positions that are forbidden or out of bounds
Example
Let us see the following implementation to get better understanding ?
from collections import deque
def solve(forbidden, a, b, x):
queue = deque([(0, 0, True)]) # (position, jumps, can_go_backward)
forbidden_set = set(forbidden)
visited = set()
# Set upper limit to avoid infinite search
limit = max(max(forbidden_set), x) + a + b
while queue:
curr, jumps, can_go_backward = queue.popleft()
# Skip if out of bounds or forbidden
if curr < 0 or curr > limit or curr in forbidden_set:
continue
# Skip if already visited with same backward capability
state = (curr, can_go_backward)
if state in visited:
continue
visited.add(state)
# Check if reached destination
if curr == x:
return jumps
# Jump forward (always allowed)
queue.append((curr + a, jumps + 1, True))
# Jump backward (only if can_go_backward is True)
if can_go_backward:
queue.append((curr - b, jumps + 1, False))
return -1
# Test the function
forbidden = [2, 3, 7, 9, 12]
a = 4
b = 2
x = 16
print(solve(forbidden, a, b, x))
7
How It Works
The algorithm uses Breadth-First Search (BFS) to find the minimum number of jumps. Starting from position 0, it explores all possible positions by jumping forward (+a) or backward (-b) while respecting the constraints:
Forward jumps: Always allowed, sets can_go_backward to True
Backward jumps: Only allowed if the previous jump wasn't backward
State tracking: Uses (position, can_go_backward) to avoid revisiting same states
Boundary check: Limits search space to prevent infinite loops
Conclusion
This problem efficiently finds minimum jumps using BFS with state tracking. The key insight is preventing consecutive backward jumps while exploring all valid positions within reasonable bounds.
