Accidental double post. Could mod please delete?
Say you have a 4x4 grid. How would you generate a path so that every tile is touched once?

Rules:
- You can start and end anywhere, just not the same tile
- You can go off one side and resume on the opposite side. You don't have to use this but you're encouraged to (this is also easily fixed by shifting the path either vertically or horizontally)
- You must use all 16 tiles once and only once (no crossing paths)
I could probably do something like this with a recursive function but the problem is that sometimes you'd end up with the following situation where you'd trap yourself:

How would you prevent this? It's easy enough to detect; you haven't generated a path on all 16 tiles. You can brute force it then by starting over and hoping it doesn't happen often. But what's the proper way to do this?
