That's right, you have 8 possible ways to place tokens in a column:
ABCDEFGH e * * * m * * p * * t * * * y
Now you can only have specific columns following other columns. For instance:
A can be a neighbor on any column,- Only
A can be a neighbor to himself, B may be a neighbor to C and G , but may not be adjacent to another B or F or H ,H can only be a neighbor to A , C or D , etc.
It should be noted that A can be useful if this column is in contact with F and G
So, we have a (non-oriented) graph:
ABCDEFGH A + + + + + + + + B + - + + + - + - C + + - + + + - + D + + + - + - + + E + + + + - + - - F + - + + + - + - G + + - + - + - - H + - + + - - - -
The incident matrix is presented above.
After that, we define A(i) as the maximum possible amount that we can get from the first columns of i if column i th finished placing the marker of type A The same goes for B , C , ..., H
Then you have a recursive formula:
X(i+1) = {max Y(i) where X and Y can be neighboring columns} + {sum of the cells in the i+1 column for placement X}
here X runs through all possible placements A , B , C , ..., H
The initial values are A(0) = 0, B(0) = 0, ..., H(0) = 0 .
The final answer is max{ A(N), B(N), C(N), D(N), ..., H(N) } .
Note:
The above solution or idea, implementation may be different. For example, you can hard-code everything (provided that Board[i][j] is the value placed on the board, the indices start at 0 ):
F(i+1) = max{ A(i), C(i), E(i), G(i) } + // This is from the matrix above Board[0][i+1] + Board[2][i+1] // This is from the definition of F type column
And similarly for each letter. You do not have to have an incidence matrix as a real entity in the program, just keep this in mind when building expressions.