source: found as a demo task in interactive interview platform of T-Bank (2024), for QA-automation interview.
"Turtle" robot moves on 2-dimensional space - actually, rectangular grid. Its route is defined by the string, consisting of letters U
, D
, L
, R
- they mean that robot should move 1
cell up, down, left or right. For example the string RRUUURRDLD
defines the following route:
5 > 6 > 7
^ v
4 9 < 8
^ v
3 10
^
0 > 1 > 2
For convenience we use numbers to mark steps of the route, 0
is from where robot had started.
Given such a string with sequence of moves, we want to know, at which step the robot crosses its own path.
Input: number T
of testcases is in the first line.
Next T
lines follow, each containing a sequence of commands for the robot, in the form of the string as described above. (these sequences
are independent, robot starts anew each time)
Answer: T
space-separated numbers - counts of steps which robot makes in each testcase before re-visiting some already-visited place. Output
0
if the pass is not self-crossing.
Example:
input:
1
RRUUURRDLLLU
answer:
10