Consider a 3x3 grid of squares. You control a piece that can navigate through the grid by either moving up, down, left or right. If your piece starts in the top left corner of the grid, how many different ways can you navigate this grid so that you touch upon every square of the grid exactly once?
I think I could brute-force this by drawing out all scenarios, but I am not sure how to do this mathematically. I could code it out and use DFS as well but I want to learn how to do it via hand mathematically.
Thank you.
There are $8$ paths. There are $2$ paths (RD and DR) that reach the center square in $2$ moves. If you choose either of those starts, the rest of the path is forced. That accounts for $2$ paths.
Otherwise, there are $2$ ways (RR and DD) to end in an adjacent corner after $2$ moves. If you find yourself in an adjacent corner after two moves, your next move, of course, is forced because there are only two squares directly connected to a corner square. Thus, your start is either RRD or DDR.
If you start RRD and your next move is L, the rest of your path is forced. Similarly, if you start DDR and your next move is U, the rest of your path is forced. That accounts for $2$ more paths.
Finally, if you start RRDD, you have remaining a $2 \times 2$ block and your next move is forced to be L. You can traverse the rest of the $2 \times 2$ block either clockwise or counterclockwise, accounting for $2$ more paths. Similarly, there are $2$ paths that begin DDRR.
There are, therefore, a total of $8$ paths.
You can simplify the presentation of the proof a bit by just counting paths that begin R and then (by symmetry) multiplying by $2$.