algorithm - Given a matrix of ints, find the longest consecutive snake of incrementing by 1 numbers -


this question has answer here:

basically, have this:

0 9 5 3' 4 1 5' 4' 5 7' 6' 9 2 8' 5 10 

in case, longest snake 3 -> 4 -> 5 -> 6 -> 7 -> 8. put ' behind numbers in show visually.

you can go both horizontally , vertically. matrix can n x m, there isn't limit number of rows , columns.

what optimal way figure out?

i've thought starting @ position n/2 , m/2, recursively doing breadth-first search , keeping track of max interval can find. i'm not sure how best tackle it.

you create graph nodes matrix positions , vertices pointing number n n+1 neighbour.

once graph built, problem amounts finding 1 of longest paths in graph.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -