Problem 96. Knight's Tour Checker
Given a matrix a, determine whether or not a legal knight's tour is present. The knight's tour always follows the pattern 1, 2, 3, ... but it need not fill the entire matrix. Any unused squares contain zeros. Your function should return true if the counting sequence from 1 to n represents a knight's tour, and false if not.
Example
The matrix a as given below is a legal knight's tour. The middle square is unreachable, but since it contains a zero, it satisfies the condition. The function should return TRUE.
7 2 5 4 0 8 1 6 3
Here is another legal (if short) knight's tour. The test suite will always contain at least one move (i.e. the counting sequence [1 2]). Note the matrix is not required to be square.
1 0 0 0 0 2
Here is an illegal knight's tour. Everything is fine up until the jump from 14 to 15, which is illegal because it jumps from row 4 to row 1.
15 5 12 3 0 2 9 6 8 11 4 13 1 14 7 10
Solution Stats
Problem Comments
-
4 Comments
I think several of the solutions to this problem would return a false positive on the vector [1 0 0 2].
I like this problem and I like chess.
Fantastic problem since it offers introductions to graph theory, specifically Hamiltonian path problems, time complexity, neural network solutions, and Warnsdorf's Rule. Thanks
easier than I expected
Solution Comments
Show commentsProblem Recent Solvers1393
Suggested Problems
-
15768 Solvers
-
Back to basics 8 - Matrix Diagonals
924 Solvers
-
"Low : High - Low : High - Turn around " -- Create a subindices vector
540 Solvers
-
Construct an index vector from two input vectors in vectorized fashion
390 Solvers
-
Generate a vector like 1,2,2,3,3,3,4,4,4,4
11367 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!