And the c++ source code:

]]>And here are is my c++ source code for the solution:

]]>And my c++ source code:

]]>And the c++ source code for my solution:

]]>My second attempt at a solution used backtracking. The result was simpler code and the ability to solve the hardest sudoku puzzles. The algorithm stops at every space sequentially and tries every number until it finds a number that results in a valid board. If it gets to a square where no number will work then it backtracks to the previous square and continues its search through the numbers. Here is a nice visualisation of backtracking from wikipedia:

Here is my c++ source code for the solution:

]]>Here is my c++ source code:

]]>So it turns out this algorithm was first discovered a long time ago. According to Knuth (in The Art Of Computer Programming 7.2.1.2), it goes back to the 18th century. This is a nice writeup of the algorithm:

https://www.nayuki.io/page/next-lexicographical-permutation-algorithm

Here is my c++ source code for the solution:

]]>And here is the c++ source code for the solution:

https://gist.github.com/adamkorg/9f6326b58db32210d9542bd1287133f9

And here is my c++ source code for the solution:

https://gist.github.com/adamkorg/fb698c0785845576ff0b42a091547691

Initially I started off with what I thought was a simple inefficient way of finding the next smallest node but the implementation was actually a little complicated. The multimap version is more time efficient and possibly has less complicated edge cases. I think an even more efficient way to keep track of the smallest node would be to use a priority queue.

]]>Recursion is a very natural fit for this problem. Iterate through each letter of each digit and recursively go into each of those letters to the next digit. If we are at the last digit then we add the current whole combination while we iterate the letters. We need to keep track of the progress of each digit through recursion. I used a vector size of digits to keep track of progress. Here is a gist of the source code:

]]>