← Back

Problem 2.1 The Relaying Problem

Introduction

A linear network of temperature sensors has been deployed along a pipeline. These sensors are designed to monitor temperature variations and detect anomalies.

The network aims to relay temperature readings from the first sensor (at position 0) to the last sensor (at position \(n-1\)). Each sensor has a limited communication range, defining how far its data can travel directly to other sensors in the network.

The objective is to determine if it is possible to transmit temperature data from the first sensor to the last sensor by relaying through intermediate sensors.

Part 1: Reachability

We are given an integer vector ranges, where each element \(ranges[i]\) represents the communication range of the \(i\)-th sensor.

The task is to determine if it is possible to relay temperature data from the first sensor to the last sensor.

Expected Output:
The function should return:

true: If it is possible to transmit temperature data from the first sensor to the last sensor.
false: If it is not possible to relay data to the last sensor.

Examples

Example Input (ranges) Output Explanation
1 [2, 3, 1, 1, 4] true The first sensor (index 0) can send data to sensors at indices 1 and 2. From sensor 1, the data can hop directly to sensor 4. Therefore, it is possible to transmit data to the last sensor using sensors 0, 1, and 4.
2 [3, 2, 1, 0, 4] false The first sensor can send data to sensors up to index 3, but sensor 3 has a communication range of 0. This makes it impossible to relay data to the last sensor.
(Note: if there are no zero range sensors, the last sensor is always reachable. The question of reachability is interesting when there are some zeros.)

Code

The following code provides an empty function (to be implemented by you), and a main that tests it.


                

Part 2: The Shortest Relay Path

We wish to not only find if data can be relayed to the last sensor, but also, in case data can be relayed, wish to known the shortest relay path (the one involving the least number of sensors) that data can follow from the first to the last sensor. In this task, we may assume that the path exists (i.e., can_relay has returned true for the given input).

Expected Output:

The function should return:

A vector containing the positions (indices) of the sensors used in the shortest relaying path.

Code

The following code provides an empty function (to be implemented by you), and a main that tests it. Use your can_relay function in the main.