Efficient hamiltonian path through neighboring squares

50 Views Asked by At

My knowledge of Number/Graph theory is very limited. I'm sorry if someone can find this answer posted elsewhere quickly, I spent some time searching but don't know enough to know what to search.

The following video describes exactly the problem I am trying to solve. https://www.youtube.com/watch?v=G1m7goLCJDY

ex: for N = 15 the solution would be

8, 1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9 

However, I have a twist.

I've written some code to solve this via brute force, for anything below 30 it is plenty fast, anything above takes more time than I'm allowed.

My question is, instead of generating a list of all pairs that would cooperate together, and mucking with it until it works. Is there an extremely fast way to find the solution?

I'm fairly certain there's a simple trick to this puzzle I've not seen or do not know enough about math to find myself.

Any help would be amazing!