# 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:

1. All the numbers between 1 to 4ª exist once in a 2ª×2ª matrix.
2. 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ª).
3. There cannot be more than 4 consecutive numbers in a row or a column.

Hints

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

• 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

66.67% Correct | 33.33% Incorrect
Last Solution submitted on Jun 05, 2023

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!