Problem 237: Tours on a 4 x n playing board

The tour consists of moves that are up, down, left, or right one square.

The tour visits each square exactly once.

The tour ends in the bottom left corner.

The diagram shows one tour over a 4 * 10 board:

T(10) is 2329. What is T(10^12) mod 10^8?

My Algorithm

A nice problem that you can easily understand within a few seconds. But it took a few days to come up with a solution ...

Of course I immediately wrote a bruteForce algorithm and it solves the T(10) case. Anything beyond that is impossible.

The main realization was to split the whole board in its columns.
I identified 15 different columns (A - O) which can have a unique "flow" on their left and right border.
The term "flow" means the chronological way how a piece moves across the board.

The arrows symbolize the "flow" in and out of a column while hash signs stand for "no border crossing":

Unfortunately it's not sufficient to represent the flow by arrows because they are still ambigious:
there are three different chronological orders how the "flow" can pass through column A.

If I look at each column's left and right border then there are just 6 patterns for these borders.
With a proper labelling of the flow's chronological order (indicated by 1,2,3,4 and a hash means "no crossing") I get 8 different borders:

111##13##221#42###32131#2#4#224#

The function fill() stores the borders of each column, e.g. column C isneighbors.insert( { "1##2", "1234" } );
Trust me, getting all this stuff right was a lot of work: I made tons of mistakes !

I wrote two algorithms: a simple one that verifies T(10) and a much faster one to solve T(10^12).slow() linearly goes through all borders that are allowed on the right side of the current border and stops if it reaches the right side of the board.
Assuming that there are about 3 borders that are compatible in such a way, the routine analyzes 3^width combinations.
There's no way it can solve T(10^12) - but I really needed this algorithm to get my borders right.
When the output finally matched the results of bruteForce I went on to write a faster (and more complex) algorithm.

fast() is a divide-and-conquer approach:

I treat a group of columns as a blackbox where I only knows its left and right border

if I cut through this blackbox at an arbitrary point then any of the 8 borders could be found

well, that's not quite right, since the 8th border is reserved for the right-most border of the board → only 7 borders "inside" the blackbox

then the number of combinations of a blackbox is the product of its left and right half

if I keep doing this until the blackbox contains only a single column then I check whether this type of column is valid

This isn't much faster than what slow() does ... but when the blackbox becomes smaller, I process the same kinds of blackboxes over and over again.
Thus memoization drastically reduced the number of different blackboxes. At the end, cache contains 3417 values.

Alternative Approaches

I was blown away by the simple solutions found by others: they discovered a relationship between T(n) and T(n-1), ..., T(n-4).
Incredible stuff - or maybe just looked up in OEIS A181688.

Note

Even though the result is found within about 0.02 seconds, I felt that dividing each blackbox in the middle isn't optimal:
if I try to divide the blackbox in such a way that at least one half's size is a power of two (that means 2^i) then cache contains only 2031 values.
Moreover, the program runs about 50% faster.

Replacing the std::string by plain integers would be still faster but I think it would be much harder to understand the code.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent toecho 10 | ./237

Output:

(please click 'Go !')

Note: the original problem's input 1000000000000cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

#include<iostream>

#include<set>

#include<map>

#include<vector>

#include<tuple>

// assuming that the route could exceed the left border I get these states for the left-most and right-most borders:

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !