一个二维数组
比如
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
找出其中的最长路径,每次只能向值比自己小的方向走,且每次只能向上或者向右走。
如24为起点:24→17→2 输出3
如13为起点:13→12→11→10→9→8→7→6→5 输出9
思想(动态规划):其实这个和滑雪路径一样的,只是要求的方向不再是4个,而是两个方向。
f[i][j]保存m[i][j]这个点为终点的最长路径,那么f[i][j]的确定可以是上或右两个方向滑向m[i][j],取max(f[i-1][j],f[i][j+1])再+1就可以了。
#include<iostream>
#include<vector>
#include<memory.h>
using namespace std;
inline int max(int a, int b) {
return(a > b ? a : b);
}
int dfsTwoDirection(vector<vector<int>> &m, vector<vector<int>> &f, int R, int C, int row, int col, int h)
{
if (row < 0 || col < 0 || ro