TITLE: count the ways to fill a $4\times n$ board with dominoes
QUESTION [21 upvotes]: After solving this problem from SPOJ (count the ways to fill a 4xn board with 2x1 dominoes) I found a different solution while searching on internet.
This solution uses the recurrence relation $f(n) = f(n-1)+5f(n-2)+f(n-3)-f(n-4)$ where $f(n)$ denotes the number of ways to fill the $4\times n$ board with $2 \times 1$ dominoes.
I don't fully understand how someone gets that kind of relation (I don't know almost anything of combinatorics or recurrence relations) but I think I understand something. The $f(n-1)$ term comes from observing that there is an unique way to fill the last column and the $5f(n-2)$ term comes from observing there are 5 different ways to fill the last two columns.

But i don't get where the terms $f(n-3)-f(n-4)$ come from. So I have two questions, the first one is where these terms come from and second I'd like to ask you for a reference to learn combinatorics and recurrence relations.
May you help me? Thanks in advance.
-edit-
For my solution I used a binary number to represent what configurations of the i-th column was valid. For example 1001 represent that rows 1 and 4 are blocked in the i-th row because in the (i-1)th there is an horizontal domino in that positions. I calculated all the valid transitions such binary numbers could go to (I did this by hand for all the binary numbers from 0000 to 1111). Then I got a function which I programmed using dynamic programming and a technique called bitmask to represent the binary numbers as integers. The function is:
$f(i,mask) = \begin{cases} 0 &\mbox{if } i = n, mask \neq 0 \\
1 & \mbox{if } i = n, mask = 0. \\
\sum f(i+1,mask') &\mbox{else} \end{cases}$
Where mask' is taken from the set of all valid masks from where mask could transition to, also i=n means the first column outside the board as I considered 0-indexing. The code for that is here (this is not actually mine, it's from a mate but it's the exactly same idea).

REPLY [2 votes]: Let's split the tilings on N into 5 subtilings:

These are all possible board endings. Each tiling belongs to one and only one of these endings. 
Number of tilings with ending of 1 and 5 are trivial to write recursively. As 2 and 3 are symmetrical, number of tilings in both of these groups is equal, denoted by g(N). Let number of tilings with ending 4 be denoted with h(N). So we have:
f(N) = f(N-2) + f(N-1) + 2*g(N) + h(N)

For type 2 (and 3) the following recursive relation holds:
g(N) = f(N-2) + g(N-1)
First part if the hole is filled with vertical tile, second if it is filled with 2 horizontal tiles (giving the symmetrical tiling on N-1).

For type 4 the following recursive relation holds:
h(N) = f(N-2) + h(N-2) 
First part if the hole is filled with vertical tile, second if it is filled with 2 horizontal tiles (forcing horizontal tiles to be placed above and below the "bulge", giving repetition of the same structure on N-2).

Now we have a system of 3 equations of 3 recursive functions, which can be simplified as shown here: Simplify system of 3 equations of 3 recursive functions
Giving:
$$f(n) = f(n - 1)+ 5·f(n - 2)+ f(n - 3) - f(n - 4)  $$
P.S. Big thanks to @Sudix for simplifying the system of 3 equations on the referenced question. :)