#C1627. Minimum Teleports
Minimum Teleports
Minimum Teleports
You are given n people standing in a line and each person may or may not carry a teleportation token. A token allows a person to teleport forward up to d positions. The teleportation is only possible if both the current person and the target person possess a token (represented as '1' in the token string).
Your task is to determine the minimum number of teleports needed to reach from the first person (position 0) to the last person (position n-1). If it is impossible to reach the last person, print -1
.
Note: The first and the last persons must have a token (i.e. the first and last characters of the token string must be '1').
The problem can be modeled as a graph traversal problem where positions are nodes and a valid teleport from position i to j exists if:
[ \text{token}[i]=1 \quad \text{and} \quad \text{token}[j]=1 \quad \text{and} \quad j-i \leq d ]
You may use either Breadth-First Search (BFS) or Dynamic Programming (DP) to solve this problem efficiently.
inputFormat
The input consists of two lines:
- The first line contains two integers
n
andd
, wheren
is the number of people andd
is the maximum teleport distance. - The second line contains a binary string of length
n
where each character is either '0' or '1'. A character '1' indicates that the corresponding person has a teleportation token.
outputFormat
Output a single integer representing the minimum number of teleports required to reach the last person. If it is impossible, output -1
.
5 3
11001
2