All paths of n = 3, n = 4, n = 5, n = 6. The thing is, I could just look at every node and find every path, but that is very expensive! It feels like there should be some kind of way to make that substantially more efficient? I can't think how though. =/
You don't have to search the whole grid every time. You can initially copy the data stored in the grid into arrays of x,y positions of each number 1, 2, 3, etc. Then, when trying to find paths of a certain number, you are limiting the search to only that certain number, ignoring the rest.
You can use easily recursion, running from i=0 to n-1 and j=0 to m-1,
assume a new array B contain 0s size = A's.
if B[i][j] = 0 then process A[i][j], which function is checking for the same value as A[i][j] around (i,j) by recursive. And after check, you'll know how many of them around, put it on all of B[-][-] you run through.
You can initially copy the data stored in the grid into arrays of x,y positions of each number 1, 2, 3, etc.
One variant of this:
-Reserve a vector for N*M pairs (value,count).
-Create a N*M matrix B of pointers to those pairs.
-Iterate your data matrix.
A(i,j) has value
IF any in A(i-1,j-1), A(i,j-1), A(i+1,j-1) has same value
THEN set B(i,j) to point to existing pair** and increment the count of that pair
ELSE append new pair(value,1) and set B(i,j) to point to it.
**If there are more than one existing pair, then point to the one with largest count.
Now the vector has list of paths, but it only knows value and length for each path.
Replace the count with a list of x,y pairs. Length of list is still the count, but now you know the elements of each path.
You still have a problem with the Y-shaped paths. For example, there is a path of 3 and path of 4 that both are elongated by A(i,j) (into 4 and 5). My algorithm would leave the 3 as 3 and turn the 4 into 5 (and potentially more, if lower rows continue it).