25

Consider an $n$ by $n$ square subdivided into unit squares. What is the shortest path you can take through the square that touches every unit square? Touching the edges/vertices of the squares is sufficient, the path can be of any shape, with any start and end point.

For $n < 3$ the answer is trivially $0$.

We want to maximize the # of diagonals and minimize the # of orthogonal moves as $\frac{3}{\sqrt{2}}>\frac{2}{1}$, thanks to @Zoe Allen for pointing this out.

Here are ALL solutions for $n < 11$ brute-forced: 3-10

The rest of the current best solutions: $11-14$, $15-18$

The unions of all line segments of the current best solutions: $7-12$

OEIS sequence A383980

The website I use to draw the solutions: virtual-graph-paper.com, thanks to @Zoe Allen.

And a GitHub repository for anyone that wishes to optimize my C++ code or run it themselves.

Recursive examples $(n > 4)$ and their lengths:

recursive

# of orthogonal moves: \begin{cases} O(n)&=6+O(n-3), &n>7\\ O(6)&=10\\ O(7)&=15\\ O(5)&=9 \end{cases} $$O(n)=6k+O(n-3k)=\\ =\begin{cases} 6\left(\frac{n-6}{3}\right)+10&=2n-2, &n\equiv0\mod{3}\\ 6\left(\frac{n-7}{3}\right)+15&=2n-2+3, &n\equiv1\mod{3}\\ 6\left(\frac{n-5}{3}\right)+9&=2n-2+1, &n\equiv2\mod{3}\\ \end{cases}$$

# of diagonal moves: \begin{cases} D(n)&=2n-7+D(n-3), &n>7\\ D(6)&=4\\ D(7)&=5\\ D(5)&=1 \end{cases} $$D(n)=\sum_{j=0}^{k-1}\Bigl(2\left(n-3j\right)-7\Bigr)+D(n-3k)\\ \sum_{j=0}^{k-1}\Bigl(2\left(n-3j\right)-7\Bigr)= 2nk-7k-6\sum_{j=0}^{k-1}j=\\ =(2n−7)k−6\left(\frac{k(k−1)}{2}\right)=k((2n−7)−3(k−1))\\ D(n)=k((2n−7)−3(k−1))+D(n-3k)=\\ =\begin{cases} \left(\frac{n-6}{3}\right)\left((2n-7)-3\left(\frac{n-6}{3}-1\right)\right)+4&= \frac{n(n-4)}{3}, &n\equiv0\mod{3}\\ \left(\frac{n-7}{3}\right)\left((2n-7)-3\left(\frac{n-7}{3}-1\right)\right)+5&= \frac{n(n-4)-6}{3}, &n\equiv1\mod{3}\\ \left(\frac{n-5}{3}\right)\left((2n-7)-3\left(\frac{n-5}{3}-1\right)\right)+1&= \frac{n(n-4)-2}{3}, &n\equiv2\mod{3} \end{cases}$$ Path length: $$S(n)=O(n)+\sqrt{2}D(n)$$ I suspect that there aren't any solutions better than these but I don't know how to prove that the recursion is always optimal in the limit. I have since posted what I deem to be a proof for $n\equiv0\mod{3}$ as an answer, is it correct?

I also conjecture that all cases of $n\equiv2\mod{3}$ have exactly $8$ solutions for $n>10$.

The problem could be equated to a domino tiling problem, where we want to use the smallest amount of dominos possible, this is lacking rigor for now and the constraints on them may be problematic.

dominos

Orthogonal moves only:

$n < 5$ is the same as above.

There are $n^2$ squares, the starting position can touch up to $4$ squares, every orthogonal line can touch up to $2$ new squares per unit length. From this we get $S(n)=\frac{n^2-4}{2}$. Such a path is always possible for $n\equiv0\mod{2}$, here's a recursive example:

0mod2

For $n\equiv1\mod{2}$, an optimal solution's path segments are all optimal except for one, since $n^2-4\equiv1\mod{2}$, thus: $S(n)=\frac{n^2-4+1}{2}$. Here's a recursive example:

1mod2

  • 1
    I posted a question then subsequently figured ut a proof for it here https://math.stackexchange.com/a/5036971/1107685 which is of interest for this question. – Zoe Allen Feb 19 '25 at 02:08

4 Answers4

8

I noted in a comment that your suggestion is inefficient, because it gains an extra $2$ squares for every $1$ unit of length, whereas a diagonal path can gain an extra $3$ squares every $\sqrt{2}$ units of length, and $\frac{3}{\sqrt{2}} > 2$.

I illustrate what I mean for $9 \times 9$ square. $n$ being of the form $6a + 3$ gives the simplest case for applying this pattern of motion, as if $n$ isn't a multiple of $3$ there are squares left around the edges that must be treated separately, and if $n$ is even the path ends up on the wrong side after each diagonal section.

enter image description here

For $n = 9$ you can see that there are $3$ diagonal stretches, each containing $7$ diagonals, $2$ lines of length $1$ at the ends of the path, and $2$ lines of length $4$ connecting the stretched.

For general $n$ of the form $6a + 3$ this would be $\frac13 n$ diagonal stretches each containing $n-2$ diagonals, with $2$ lines of length $1$ at the ends and $n-1$ lines of length $4$ connecting the stretches. In total this gives a path length of $\frac13 n(n-1)\sqrt{2} + 4n - 2$.

You can see this grows like $\frac{\sqrt{2}}{3} n^2$, whereas your suggestion grows like $\frac12 n^2$. You can obtain the same growth rate for $n$ not a multiple of $3$, as the additional length to fill in the squares missed around the edges grows proportional to $n$, so is negligible for large $n$.

The smallest $n$ for which my path is shorter than yours is $129$, however. My path is not optimal either.

Zoe Allen
  • 7,939
6

Here is a 17 x 17 square with total path length $50+65\sqrt 2\approx 141.92$, which is shorter than $\frac{n^2-3}{2}=143$:

enter image description here

If $n$ is of the form $6k+5$, this pattern has length $$20k+10+(2k+1)(6k+1)\sqrt 2$$ which grows like $\frac{\sqrt 2}{3}n^2$, just like Zoe Allen's pattern.

Note that the path length in this pattern can be reduced by a small constant $2(1+\sqrt 2-\sqrt 5)\approx 0.178$, by joining points $(1,2)$ and $(3,3)$ with a straight line, and similarly points $(16,15)$ and $(14,14)$.

TonyK
  • 68,059
  • Now the questions are: when do different methods become better than the orthogonal walk and what's the best method: zig-zag / spiral into the middle / a corner. I got 36+75sqrt2~142.07 with something close to @ZoeAllen 's method. – Fülöp Tamás Feb 18 '25 at 20:47
  • @TonyK I can see the picture in the link, so I'm afraid the problem is probably on your side. – Zoe Allen Feb 18 '25 at 20:49
  • 2
    Ah, I can see them if I disable my VPN! – TonyK Feb 18 '25 at 20:50
  • Alright, I believe the minimum n where using diagonals is better is 5, I see no way to improve n=4, here are my attempts for 5-8: link. (8+2sqrt2<11, 10+4sqrt2<16, 14+6sqrt2<23, 15+10sqrt2<30) – Fülöp Tamás Feb 18 '25 at 21:38
  • I was able to improve this to about $140.91$ by shifting all the diagonals down one line, but then I noticed that the question has been updated with an example of a $17\times17$ square with a path of length $33 + 73 \sqrt2 \approx 136.24$ (which I verified in python) and at this time I have no idea how to improve on that. – David K Apr 12 '25 at 05:06
  • @tomka700 I was commenting on this answer. Still, I share your optimism that your formulas can be proved inductively. Is any shorter path possible? I don't know. I suspect maybe not. – David K Apr 15 '25 at 00:56
3

Proof of optimality of the $n = 3$ and $n = 4$ cases:

Any path in an $n \times n$ grid can be squeezed onto the interior $(n-2) \times (n-2)$ grid without reducing the number of squares it touches, by taking the minimum of each coordinate with $n-1$ and the maximum of each coordinate with $1$. So there must be an optimal path contained within the interior square.

There is only one point in the interior $(n-2) \times (n-2)$ square intersecting with each corner square, so the path must go through these $4$ points. The points are $(1,1),(1,n-1),(n-1,1),(1-1,n-1)$. The path then contains at least $3$ segments, where we start a new segment after reaching a new corner point. The length of the segment between any two corner points is at least the distance between the two points, which is $n-2$.

Hence the total path length is at minimum $3(n-2)$, and this is what is obtained for $n = 3$ and $4$.

$n = 5$ will be significantly trickier, but the same ideas will be useful.

Zoe Allen
  • 7,939
1

This is a proof for $n\equiv0\mod{3}$ being optimal ($n>3$).

It relies heavily on this proof, which proves that (1,1) and (1,0) moves are the most efficient.

One of the cases with the lowest amount of orthogonal connections to diagonal sections is a $3$-wide band configuration. (The spiral case is equally optimal, but it is harder to reason with.)

This doesn't yet touch all squares so replace the endings with $2$ orthogonal lines each, then connect these complete bands with the shortest paths possible to form a single continuous path.

Notice that:

  • The orthogonal lines in the first step are necessary and minimal in reaching the missed squares.
  • Both cases of connecting the bands are equally optimal, as shown below.
  • The whole procedure only depends on the bands fitting perfectly ($\mod{3}$).

(The case with the highest # of diagonals looks like this and has $6$ more diagonals, but it requires $6$ orthogonal connections of length $5$ instead of $3$ of length $4$ and among other issues, the corners are problematic.)

($n=3$ is a special case where we start off with a single diagonal in the middle that is both the start and the end of the band so we have to replace it twice with $2$ orthogonal lines, one of the $4$ being reused.)

n=12

There are $\frac{n}{3}$ many bands of $n-4$ diagonals. $$D(n)=\frac{n}{3}(n-4)=\frac{n(n-4)}{3}$$

There are $\frac{n}{3}-1$ connections of length $2$ and $\frac{n}{3}\times2$ band endings get turned into $2$ orthogonal lines each. $$O(n)=\left(\frac{n}{3}-1\right)\times2+\left(\frac{n}{3}\times2\right)\times2 =2n-2$$

These are the same formulas as the ones defined above for $n\equiv0\mod{3}$, so those paths are also optimal.