Problem 42705. Is It a Snake?
Given an m-by-n matrix, return true if the elements of the matrix are a connected "snake" shape from 1 to m*n. Otherwise return false.
Snakes are vectors that move through the grid of a matrix in a 4-connected sense. So the number 1 can be anywhere, but the number 2 must be north, south, east, or west of 1. And the number 3 must be north, south, east, or west of 2. And so on.
Examples
[ 1 2 3 4 5 ] is a snake
[ 2 1 3 4 ] is NOT a snake
[ 1 2 3 2 1 ] is NOT a snake
[ 6 5 4 3 ] is NOT a snake
[ 6 1 2 5 4 3 ] is a snake
[ 1 2 3 4 ] is NOT a snake
[ 7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13 ] is a snakeNOTE: Answers to this problem helped me write the test suite for Cody Problem 42708, Placing Beads Neatly in a Box. Thanks!
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers49
Suggested Problems
-
1697 Solvers
-
Project Euler: Problem 2, Sum of even Fibonacci
2609 Solvers
-
Back to basics 12 - Input Arguments
612 Solvers
-
middleAsColumn: Return all but first and last element as a column vector
628 Solvers
-
Find the sides of an isosceles triangle when given its area and height from its base to apex
2067 Solvers
More from this Author50
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!