Problem 54780. Trap a knight
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
-
1 Comment
Asif Newaz
on 13 Aug 2022
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?
Solution Comments
Show commentsProblem Recent Solvers3
Suggested Problems
-
26234 Solvers
-
1223 Solvers
-
161 Solvers
-
Getting the indices from a vector
8919 Solvers
-
213 Solvers
More from this Author250
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!