longest consecutive path

Given a 2D matrix containing all positive and distinct integers in the range [1, N^2], compute the longest path containing consecutive integers in this matrix. return the length of the longest path.
[2,1,3]
[4,9,5]
[8,7,6]
7,8 len=2 7-->2
traverse from 6, 6->7 len of 6 = 2+1=3
The answer is 4 (path is [5,6,7,8]).
N=3 3*3=9

[1,3,5,8]
[2,4,6,7]
[11,12,13,15]
[10,9,14,16]

The answer is 6 (path is [9,10,11,12,13,14]).

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9627590.html