Problem 203. fly fly away
A fly moves following a predefined sequence of discrete jumps (defined by the vectors dx and dy) repeating the same sequence over and over (note that after each sequence of movements the fly may or may not be at the same position where it started).
The fly always starts at the coordinates (0,0) and your job is to determine whether it will ever reach a target coordinate ( x , y ).
note: dx and dy are always equal-sized row vectors and x and y are always scalars. Your function should return true if the trajectory passes through the target.
Example
dx = [0,1,0,-1];
dy = [1,0,1,0];
x = 1;
y = 5;
The fly follows the trajectory:
(0,0) (0,1) (1,1) (1,2) (0,2) (0,3) (1,3) (1,4) (0,4) (0,5) (1,5) (1,6) ...
The trajectory passes through the target point (1,5) so the function should return true.
note: in the test suite, if the target is reachable it will be reachable in at most 100 repetitions of the movement sequence. You get unscored bonus points if your solution does not require this limit to be relatively small (i.e. if the algorithm computational time does not increase linearly with the number of repetitions)
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers71
Suggested Problems
-
Determine if input is a Narcissistic number
199 Solvers
-
189 Solvers
-
Sum the elements in either diagonal of a square matrix
212 Solvers
-
734 Solvers
-
Area of an equilateral triangle
6219 Solvers
More from this Author38
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!