Complete the grid-filling meander
JavaScript (ES7), &nbsp;236 ... 193&nbsp; 185 bytes

Outputs by modifying the input matrix.

m=&gt;(g=(d,x,y,v,r=m[y],h=_=&gt;++r[x]&lt;9?g(d,x,y,v)||h():r[x]=0)=&gt;r&amp;&amp;1/(n=r[x])?x|y|!v?n?g(d='21100--13203-32-21030321'[n*28+d*3+7&amp;31],x+--d%2,y+--d%2,v+=n&lt;7||.5):h():!m[v**.5|0]:0)(0,0,0,0)


Try it online!

(includes some post-processing code to print the result both as a matrix and as a flat list compatible with the visualization tool provided by the OP)

Results


Test case #1
Test case #2
Test case #3


How?

Variables

\$g\$ is a recursive function taking the current direction \$d\$, the current coordinates \$(x,y)\$ and the number of visited cells \$v\$.

The following variables are also defined in the scope of \$g\$:


\$r\$ is the current row of the matrix.

r = m[y]

\$h\$ is a helper function that tries all values from \$1\$ to \$8\$ for the current cell and invokes \$g\$ with each of them. It either stops as soon as \$g\$ succeeds or sets the current cell back to \$0\$ if we need to backtrack.

h = _ =&gt; ++r[x] &lt; 9 ? g(d, x, y, v) || h() : r[x] = 0



Initial checks

We first make sure that our current location is valid and we load the value of the current cell into \$n\$:

r &amp;&amp; 1 / (n = r[x]) ? ... ok ... : ... failed ...


We test whether we're back to our starting position, i.e. we're located at \$(0,0)\$ and we've visited at least a few cells (\$v&gt;0\$):

x | y | !v ? ... no ... : ... yes ...


For now, let's assume that we're not back to the starting point.

Looking for a path

If \$n\$ is equal to \$0\$, we invoke \$h\$ to try all possible values for this tile.

If \$n\$ is not equal to \$0\$, we try to move to the next tile.

The tile connections are encoded in a lookup table, whose index is computed with \$n\$ and \$d\$, and whose valid entries represent the new values of \$d\$.

d = '21100--13203-32-21030321'[n * 28 + d * 3 + 7 &amp; 31]


The last 8 entries are invalid and omitted. The other 4 invalid entries are explicitly marked with hyphens.

For reference, below are the decoded table, the compass and the tile-set provided in the challenge:

   | 1 2 3 4 5 6 7 8
---+-----------------
 0 | 0 - - 1 3 - 3 1          1
 1 | - 1 - - 2 0 2 0        0 + 2
 2 | 2 - 1 - - 3 1 3          3
 3 | - 3 0 2 - - 0 2




We do a recursive call to \$g\$ with the new direction and the new coordinates. We add \$1/2\$ to \$v\$ if we were on a tile of type \$7\$ or \$8\$, or \$1\$ otherwise (see the next paragraph).

g(d, x + --d % 2, y + --d % 2, v += n &lt; 7 || .5)


If \$d\$ is invalid, \$x\$ and \$y\$ will be set to NaN, forcing the next iteration to fail immediately.

Validating the path

Finally, when we're back to \$(0,0)\$ with \$v&gt;0\$, it doesn't necessarily mean that we've found a valid path, as we may have taken a shortcut. We need to check if we've visited the correct number of cells.

Each tile must be visited once, except tiles \$7\$ and \$8\$ that must be visited twice. This is why we add only \$1/2\$ to \$v\$ when such a tile is visited.

In the end, we must have \$v = N^2\$. But it's also worth noting that we can't possibly have \$v &gt; N^2\$. So, it's enough to test that we don't have \$v &lt; N^2\$, or that the \$k\$th row of the matrix (0-indexed) does not exist, where \$k=\lfloor\sqrt{v}\rfloor\$.

Hence the JS code:

!m[v ** .5 | 0]


Formatted source

m =&gt; (
  g = (
    d,
    x, y,
    v,
    r = m[y],
    h = _ =&gt; ++r[x] &lt; 9 ? g(d, x, y, v) || h() : r[x] = 0
  ) =&gt;
    r &amp;&amp; 1 / (n = r[x]) ?
      x | y | !v ?
        n ?
          g(
            d = '21100--13203-32-21030321'[n * 28 + d * 3 + 7 &amp; 31],
            x + --d % 2,
            y + --d % 2,
            v += n &lt; 7 || .5
          )
        :
          h()
      :
        !m[v ** .5 | 0]
    :
      0
)(0, 0, 0, 0)