ZUBREACH - Editorial

PROBLEM LINK:

Practice

Author: Kazi Hasan Zubaer

Editorialist: Bhavya Agarwal

DIFFICULTY

CAKEWALK

PREREQUISITES:

Math

PROBLEM:

The aim is to find the final position with the information about his movements.

EXPLANATION:

As mentioned in the problem the initially the person is at the origin and the information about his movements is given in the form of :

U - Up
D - Down
R - Right
L - Left

Or, more clearly

U indicates a move that increases position along y-axis by 1
D indicates a move that decreases position along y-axis by 1
R indicates a move that increases position along x-axis by 1
L indicates a move that decreases position along x-axis by 1

So, first we need to declare two variables px and py to track the movement of the person in x-axis and y-axis respectively axis and assign them to 0 as initial position is 0. With the given constraints they can be int.

px = 0;
py = 0;

Then for the length of movement, l and a character x.

    for i = 1 to l do
       read x
       if (x = U) then
          py = py + 1
       if (x = D) then
          py = py - 1
       if (x = R)
          px = px + 1
       if (x = L)
          px = px - 1

After calculating the final position we need to find at which part the person is that is at

Relative’s Position(rx, ry)

Dangerous Position(x, y) where x > m or x < 0 or y > n or y < 0

Somewhere(x, y) where 0 ≤ x ≤ m and 0 ≤ y ≤ n

Since we also need to output the no. of case we can initialize a variable i to 1 and increment it after every test case.

EDITORIALIST’S SOLUTION:

Can be found here.