I have a matrix like:
0 | 1 | 2 | 3
-------------
7 | 6 | 5 | 4
-------------
8 | 9 | A | B
-------------
F | E | D | C
in code:
var matrix = new char[][]
{
new char[] { '0', '1', '2', '3' },
new char[] { '7', '6', '5', '4' },
new char[] { '8', '9', 'A', 'B' },
new char[] { 'F', 'E', 'D', 'C' }
};
I need to get all possible combination of this matrix with 5 chars each, but every char must be the next immediate neighbor, for instance, 0,5 and 4,8 are invalid but 0,6 and 3,4 are valid.
This matrix will not be static as is in the sample, it is generated with the hex numbers in any position every time.
Thanks!
Just for fun – Delphi code. ‘shl’ is left shift (<<), strings are one-based. Borders are added around the matrix as sentinels to simplify checks.
Some additional comments: I use 6×6 matrix – 4×4 core with digits, and one-cell width outer border. 36 bits of Int64 are used as sentinels. Bit value of 1 denotes border or visited cell. Procedure StartTravel initiates path finding from some starting cell of core. Path finding goes like DFS (deep-first search), but interrupts when path length becomes 5.