#C7371. Robotic Arm Target Reachability
Robotic Arm Target Reachability
Robotic Arm Target Reachability
In this problem, you are given a grid with n rows and m columns. A robotic arm is initially positioned at cell (x1, y1) and needs to reach the target cell (x2, y2).
You are also provided with a sequence of commands represented as a string consisting of the characters L
(left), R
(right), U
(up) and D
(down). The robotic arm moves one cell per command in the corresponding direction.
The movement is only valid if the robotic arm remains within the grid boundaries. Formally, if the arm moves out of the grid after any command, the process is terminated and the answer is "NO". Otherwise, if after executing all commands the arm's position is exactly the target cell, print "YES"; else, print "NO".
Mathematically, if we define the initial position as \( (x, y) = (x_1,y_1) \) and update it for each command, the movement rules are given by:
\[ \begin{aligned} \text{if } c &= 'L' : &y &\gets y - 1,\\ \text{if } c &= 'R' : &y &\gets y + 1,\\ \text{if } c &= 'U' : &x &\gets x - 1,\\ \text{if } c &= 'D' : &x &\gets x + 1. \end{aligned} \]Your task is to simulate the movements and determine whether the target cell is reached.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains six space-separated integers:
n m x1 y1 x2 y2
representing the grid dimensions and the starting and target cell positions. - The second line contains a string of commands composed only of the characters
L
,R
,U
,D
.
outputFormat
Output a single line via standard output (stdout) containing either YES
if the robotic arm can successfully reach the target cell while remaining within the grid boundaries, or NO
otherwise.
5 5 1 1 3 3
RRDD
YES