This problem of candy in a rectangular grid is the Knight's Tour problem in disguise, where a knight must visit each square exactly once. A tour can be open or closed, a closed tour being one where the knight can move once between the two ends of a tour. I will first find the general solutions, then specialize to the statement of the problem here. I will use size convention m*n, where m <= n. The general solutions are stated in these three papers:
Knight's Tour Revisited by Paul Cull and Jeffrey De Curtins, 1978
Every board with m >= 5 has a general tour, and every board with m >=5 and at least one of m and n even has a closed tour.
Which Rectangular Chessboards Have a Knight's Tour? by Allen J. Schwenk, Mathematics Magazine, Vol. 64, No. 5 (Dec., 1991), pp. 325-332
A board has a closed tour unless at least one of the following conditions holds:
- both m and n are odd
- m = 1, 2, or 4
- m = 3 and n = 4, 6, or 8
Knight's Tour, by Kevin McGown, Ananda Leininger. Advisor: Paul Cull
A board has a general tour if
- m = 3 and n = 4, 7, or higher
- m = 4 and n = 5 or higher
Some of the are rather complicated, with some of them involving constructing solutions and assembling larger solutions from them. Some of them are simple, however, like both dimensions being odd meaning no closed tours, and one dimension being 1 or 2 meaning no tours of any kind.
For a 3*5 board, there are no tours. Here is a proof. For 3*5, the square coordinates range from (1,1) to (3,5). Consider squares (2,1) and (2,5). Square (2,1) has tour neighbors (1,3) and (3,3), but square (2,5) has those tour neighbors also. So once one reaches (1,3) and goes to (2,1), one must go to (3,3), making (2,5) inaccessible.
For a 4*5 board, there is a tour. The third paper has a construction of it: (1,2), (3,1), (4,3), (3,5), (1,4), (2,2), (4,1), (3,3), (4,5), (2,4), (3,2), (4,4), (2,5), (1,3), (2,1), (4,2), (3,4), (1,5), (2,3), (1,1)
The smallest board with a tour is a 3*4 one. For smaller dimension 1, the knight has no move in the board. For smaller dimension 2, each corner square has only one possible move out of it, meaning that a tour must start or end at a corner. However, there are 4 corners, making a tour impossible. For 3*3, the center square is inaccessible from the other. That leaves 3*4, and a tour for that one is (1,1), (2,3), (3,1), (1,2), (2,4), (3,2), (1,3), (2,1), (3,3), (1,4), (2,2), (3,4). All other boards with tours have sizes greater than it, at least 3*7 or 4*4.
The smallest square board with a tour is a 5*5 one. That is because a 4*4 one does not have a tour, and because a tour does exist for 5*5, as stated in the first paper. The third paper has a proof that the 4*4 one does not have a tour. It involves domino tilling for 4*n boards, where squares (1,even), (2,even), (3,odd), and (4,odd) have the same color, as do squares (1,odd), (2,odd), (3,even), (4,even). The paper has a proof of a theorem that the knight must visit all of one color in sequence, then all of the other color in sequence, though the proof is rather complicated. With that proof, there is a simple proof that 4*4 does not contain a tour. After doing the first color, we are left with the second color. The only tours possible in it are (1,1), (2,3), (4,4), (3,2) and (2,1), (1,3), (3,4), (4,2), both of them closed. So a complete tour is not possible.
In summary:
- 3*5 - no tour
- 4*5 - tour
- smallest with tour: 3*4
- smallest square with tour: 5*5