Problem 1494. Hungry Snake
Who hasn't played the Snake game when they were little? It's quite hard to finish this simple game; nonetheless Chuck Norris has accomplished the task in the most difficult level, naturally. Now, since the game was too easy for him, he did it with the highest number of turns possible. We shall now inspect Chuck Norris' solution.
Suppose you have a 2ª×2ª matrix M (with an integer a ≥ 0). The path of the snake is denoted with consecutive numbers 1÷4ª. The matrix M must obey the following conditions:
- All the numbers between 1 to 4ª exist once in a 2ª×2ª matrix.
- These numbers form a snake; i.e., each number n must be adjacent to both n -1 and n +1 (with the obvious exception of 1 and 4ª).
- There cannot be more than 4 consecutive numbers in a row or a column.
Hints
- Since Chuck Norris can draw an infinite fractal, you may want to check the Hilbert Curve or other Space-filling curves.
Examples
hungry_snake(0) ans = 1
hungry_snake(1) ans = 1 2 4 3
hungry_snake(2) ans = 1 4 5 6 2 3 8 7 15 14 9 10 16 13 12 11
Bad Solutions
- a=1; eye(2^a) — doesn't have all the numbers 1 to 4.
- a=2; reshape(1:4^a,2^a,2^a) — 4 and 5 aren't adjacent.
- a=3; spiral(2^a) — has 8 consecutive numbers in a row.
The usual cheats are not allowed!
Solution Stats
Solution Comments
Show commentsProblem Recent Solvers8
Suggested Problems
-
1596 Solvers
-
Program an exclusive OR operation with logical operators
716 Solvers
-
Determine the number of odd integers in a vector
731 Solvers
-
103 Solvers
-
70 Solvers
More from this Author18
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!