SnakeA "snake" is defined as a sequence of adjacent consecutively increasing integers (incrementing by one) embedded in and twisting through a larger matrix. The sequence can turn north, south, east, or west, but not diagonally. A snake sequence may also be straight. Given the matrix a, return the longest snake sequence in the variable b. a is an array containing positive integers. b is a column vector containing the linear index of this sequence, beginning with the smallest number in the snake sequence. For example, when a = [ 14 16 13
15 15 14
16 10 9]create b = [ 7
8
5
4 ]Another example is a = [ 1 2 3
4 7 4
5 6 5]b = [ 1
4
7
8
9
6
5 ]Each matrix will have exactly one snake sequence longer than all others. In addition, a will never be a vector. That is, the smallest dimension of a will always be greater than one. Note: Linear (or absolute) indexing is a way to index into a 2-D matrix with one number. Linear indexing is column-oriented, so if
a = [ 14 16 13
15 15 14
16 10 9]
then a(1) is 14, a(4) is 16 and a(8) is 14. To learn more about linear indexing, read this MATLAB Digest article.
Leaders
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||