#C5255. Robot Pathfinding in a Grid
Robot Pathfinding in a Grid
Robot Pathfinding in a Grid
You are given a grid with \( n \) rows and \( m \) columns. Each cell in the grid is either empty (denoted by 0) or contains an obstacle (denoted by 1). A robot is initially placed at a starting cell \( (x_1, y_1) \) and needs to reach a destination cell \( (x_2, y_2) \). The robot can move in four directions: up, down, left, and right, but it cannot move into obstacles.
Your task is to determine if there exists a valid path from the starting cell to the destination cell without passing through any obstacles. Note that if the starting cell is the same as the destination, the answer is automatically "YES".
Constraints: The input guarantees that the grid dimensions and coordinates are valid, and grid cells are only 0 or 1.
inputFormat
The input is read from standard input (stdin) and is organized as follows:
- The first line contains two integers \( n \) and \( m \), representing the number of rows and columns in the grid.
- The next \( n \) lines each contain \( m \) space-separated integers (either 0 or 1) representing the grid cells.
- The following line contains two integers \( x_1 \) and \( y_1 \), representing the starting cell coordinates (0-indexed).
- The last line contains two integers \( x_2 \) and \( y_2 \), representing the destination cell coordinates (0-indexed).
outputFormat
Output a single line to standard output (stdout) containing either YES
if a path exists from the starting cell to the destination cell, or NO
otherwise.
4 5
0 0 1 0 0
0 1 0 1 0
0 1 1 0 0
0 0 0 0 0
0 0
3 4
YES