https://leetcode.com/problems/longest-increasing-path-in-a-matrix/

Given an integer matrix, find the length of the longest increasing path.

From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

Example 1:

nums = [
[9,9,4],
[6,6,8],
[2,1,1]
]

Return 4
The longest increasing path is [1, 2, 6, 9].

Example 2:

nums = [
[3,4,5],
[3,2,6],
[2,2,1]
]

Return 4
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

class Solution {
public:
bool checkRange(vector<vector<int> >& matrix, int x, int y) {
int n = matrix.size(), m = matrix[].size();
if(x < || y < || x >= n || y >= m) return false;
return true;
} int dfs(vector<vector<int> >& matrix, vector<vector<int> >& dp, vector<vector<bool> >& vis, vector<vector<int> >& dir, int x, int y) {
if(dp[x][y]) return dp[x][y]; int MAX = -;
for(int i=; i<dir.size(); ++i) {
int nx = x + dir[i][], ny = y + dir[i][];
if(checkRange(matrix, nx, ny) && !vis[nx][ny] && matrix[nx][ny] > matrix[x][y]) {
vis[nx][ny] = true;
MAX = max(MAX, dfs(matrix, dp, vis, dir, nx, ny) + );
vis[nx][ny] = false;
}
} if(MAX == -) return ;
dp[x][y] = MAX;
return dp[x][y];
} int longestIncreasingPath(vector<vector<int>>& matrix) {
int n = matrix.size();
if(n == ) return ; int m = matrix[].size();
vector<vector<int> > dp(n, vector<int>(m, ));
vector<vector<bool> > vis(n, vector<bool>(m, false));
vector<vector<int> > dir();
dir[].push_back(-); dir[].push_back();
dir[].push_back(); dir[].push_back();
dir[].push_back(); dir[].push_back();
dir[].push_back(); dir[].push_back(-); int res = -;
for(int i=; i<n; ++i) {
for(int j=; j<m; ++j) {
vis[i][j] = true;
dp[i][j] = dfs(matrix, dp, vis, dir, i, j);
res = max(res, dp[i][j]);
vis[i][j] = false;
}
} return res;
}
};

leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)的更多相关文章

  1. LeetCode #329. Longest Increasing Path in a Matrix

    题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ...

  2. [LeetCode] 329. Longest Increasing Path in a Matrix ☆☆☆

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  3. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question

    在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.

  4. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  5. 329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...

  6. [LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  7. 329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  8. 329. Longest Increasing Path in a Matrix

    最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...

  9. Java实现 LeetCode 688 “马”在棋盘上的概率(DFS+记忆化搜索)

    688. "马"在棋盘上的概率 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有 ...

随机推荐

  1. Windows下的Memcache安装与测试教程

    Windows下的Memcache安装 1.下载memcache for windows. 下载地址:http://splinedancer.com/memcached-win32/,推荐下载bina ...

  2. spoj 247

    不管行列   总是先切割切割费用大的  代码比较烂 ...... #include <iostream> #include <cstdio> #include <cstr ...

  3. hdu 3778

    简单的dfs  但繁琐的可以了 0.0 #include<cstdio> #include<cstring> #include<algorithm> using s ...

  4. objective-c宏定义

    1.先来几个常用的: // 是否高清屏 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? ...

  5. BZOJ 2806 cheat

    首先这个题目显然是要二分转换成判断可行性的 之后我们考虑DP 设f(i)表示 1->i 熟悉的子串的长度的最大值 那么对于i这个点,要么不在熟悉的子串中,要么在熟悉的子串中 所以得到 f(i)= ...

  6. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  7. SQL盲注修订建议

    一般有多种减轻威胁的技巧: [1] 策略:库或框架 使用不允许此弱点出现的经过审核的库或框架,或提供更容易避免此弱点的构造. [2] 策略:参数化 如果可用,使用自动实施数据和代码之间的分离的结构化机 ...

  8. Python之函数篇

    函数形参 函数取得的参数是你提供给函数的值,这样函数就可以利用这些值 做 一些事情.这些参数就像变量一样,只不过它们的值是在我们调用函数的时候定义的,而非在函数本身内赋值. 参数在函数定义的圆括号对内 ...

  9. Android EditText属性

    1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...

  10. CodeIgniter类库之Benchmarking Class ,计算代码的执行时间

    CodeIgniter中有个Benchmarking类库,它是被系统自动被加载的,不需要手工加载.Benchmarking类库能够计算出任意两个被标记点之间的代码执行时间.通过这个数值,可以评估程序员 ...