Software

Grid path (count) finder

I saw a puzzle in a magazine a while ago, asking how many valid paths there are in a 4x4 grid going from the top left corner to the top right corner. In this case "valid" meant that the route must visit all cells exactly once (the top left cell being the first and top right cell being the last, naturally). An example of a valid solution is shown in the image below.

An example solution

It didn't take very long to find some (or all, as it turned out) solutions with just pen and paper, but I couldn't give the puzzle a rest until I had proven that no other solutions exist. I checked the web for existing programs for solving the problem but couldn't find any, and the work didn't seem that big, so I ended up doing a very simple program that finds all the paths.

The agile way would have been to make the program only solve the very problem that I was facing and skip all kinds of additional features (i.e. "maximizing the work not done"). However, to make the program even a bit more useful generally, the Grid class can generate a square grid of any size, and the start and goal cells can be changed quite easily.

The program still isn't very user-friendly, and only prints out the valid paths as index numbers of the cells. Printing graphical solutions would be a lot better, but that would also change the scope of the program a lot, and I already got what I wanted, so at least for now, no GUI is available.

The algorithm is also very simple, basic depth-first search. With small grid sizes it's definitely quick enough, so there was no need to develop a more optimized approach.

Feel free to use and modify the code. It can be found here.