Coding | Round 1 | Uber | Onsite

Anonymous User
1613
Apr 25, 2022
Apr 25, 2022

I was asked the following question, does amybody know how to solve this?

You a captain of a ship. Initially you are standing in a point (𝑥1,𝑦1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (𝑥2,𝑦2).

You know the weather forecast — the string 𝑠 of length 𝑛, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side 𝑠1, the second day — 𝑠2, the 𝑛-th day — 𝑠𝑛 and (𝑛+1)-th day — 𝑠1 again and so on.

Ship coordinates change the following way:

if wind blows the direction U, then the ship moves from (𝑥,𝑦) to (𝑥,𝑦+1);
if wind blows the direction D, then the ship moves from (𝑥,𝑦) to (𝑥,𝑦−1);
if wind blows the direction L, then the ship moves from (𝑥,𝑦) to (𝑥−1,𝑦);
if wind blows the direction R, then the ship moves from (𝑥,𝑦) to (𝑥+1,𝑦).
The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point (𝑥,𝑦) it will move to the point (𝑥−1,𝑦+1), and if it goes the direction U, then it will move to the point (𝑥,𝑦+2).

You task is to determine the minimal number of days required for the ship to reach the point (𝑥2,𝑦2).
Comments (10)