Consider a knight on an infinite chessboard labeled with numbers spiraling outward. A knight starting on the square labeled 1 can reach 8 squares, marked in green below (i.e., squares 10, 12, 14, 16, 18, 20, 22, and 24). Take the smallest of these numbers, or 10. Repeating the step while avoiding squares already visited puts the knight at squares 3, 6, 9, 4, 7, 2, 5, etc. This tour continues until step 2016, when the knight reaches square 2084. At that point the knight is trapped: it has visited all eight possible squares.
Write a function that takes the starting square and returns the sequence of squares visited by the knight on the tour.
Solution Stats
Problem Comments
2 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers7
Suggested Problems
-
1713 Solvers
-
Get the area codes from a list of phone numbers
1071 Solvers
-
Natural numbers in string form
1849 Solvers
-
List the nth term of Rozhenko’s inventory sequence
11 Solvers
-
Count the unitary divisors of a number
13 Solvers
More from this Author321
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
my solution (using brute force) is giving the right answer (on the test cases I checked) but it's failing in Cody due to time constraints. I hate it when it does that.
in test case - 5, tic-toc is giving me 5s. I might be able to optimize the code a little more but I am not sure that would be enough. I think it's failing on the loop test cases due to runtime.
is it necessary to use loop cases as they tend to take a long time? why not just random settings?
First off, why doesn't the MATLAB GUI find spiral when you search for it? I spent a lot of time building this myself before going on the internet for it.
Second, sorting a list and keeping the indices (second output argument from sort())is much faster than repeated finds when you're in the tens of thousands of finds and tens of thousands of array elements.