#C3861. Can Robot Reach Destination?
Can Robot Reach Destination?
Can Robot Reach Destination?
You are given a grid of size (N \times M) with some obstacles placed on certain cells. The robot starts at position ((Sx, Sy)) and needs to reach the destination at ((Dx, Dy)). The robot can only move one cell at a time in one of the four directions: up, down, left, or right. The grid indices are 1-indexed. There are (K) obstacles and each obstacle is located at a specific cell in the grid. Determine whether the robot can reach the destination without colliding with any obstacles.
The input format is as follows:
(N) (M) (Sx) (Sy) (Dx) (Dy) (K) (obs_1_x) (obs_1_y) (obs_2_x) (obs_2_y) ... (obs_K_x) (obs_K_y)
The output should be a single line:
- Print "ROBOT REACHED" if the destination is reachable.
- Otherwise, print "OBSTACLE DETECTED".
This problem can be solved using a breadth-first search (BFS) on the grid. Moves to a cell with an obstacle are not allowed and revisiting cells should be avoided.
inputFormat
The input is provided on a single line via standard input (stdin). The first six integers represent the grid size (N), (M), the starting coordinates (Sx), (Sy) and the destination coordinates (Dx), (Dy). The next integer (K) represents the number of obstacles. This is followed by (K) pairs of integers, each representing the coordinates of an obstacle.
outputFormat
Output a single line to standard output (stdout) with the result. The output should be exactly either "ROBOT REACHED" if a path exists from the start to the destination without encountering an obstacle, or "OBSTACLE DETECTED" if no such path exists.## sample
5 5 1 1 5 5 3 1 2 2 3 3 4
ROBOT REACHED