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. AlphaGo 已经战胜了李世石,而你还不知道什么是机器学习?

    谷歌人工智能 AlphaGo 与韩国棋手李世石 3 月 15 日进行了最后一场较量,最终比赛结果为 AlphaGo 4:1 胜李世石,人机围棋大战巅峰对决至此落幕.我不知道大家有没有被震撼到,反正我的 ...

  2. Notifications Nagios

    Introduction I've had a lot of questions as to exactly how notifications work. This will attempt to ...

  3. linux pts/0的含义

    pts是所谓的伪终端或虚拟终端,具体表现就是你打开一个终端,这个终端就叫pts/0,如果你再打开一个终端,这个新的终端就叫pts /1.比如用who命令查询当前登录的用户,可以看到每个用户的TTY设备 ...

  4. struts2 权限拦截器 拦截没有登陆的请求

    假设有这样的登陆: ActionContext.getContext().getSession().put("UserMsg", userMsg); 则可以这样判断是否登陆: im ...

  5. codeforces #305 div1 done

    总算搞定了这一场比赛的题目,感觉收获蛮大 其中A,B,C都能通过自己的思考解决掉 D题思路好神,E题仔细想想也能想出来 以后坚持每两天或者一天做一场CF的div1的全套题目 除非有实在无法做出来的题目 ...

  6. POJ2506——Tiling

    Tiling Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a samp ...

  7. java事务管理(二)

    数据库事务和Spring事务是一般面试都会被提到,很多朋友写惯了代码,很少花时间去整理归纳这些东西,结果本来会的东西,居然吞吞吐吐答不上来. 下面是我收集到一些关于Spring事务的问题,希望能帮助大 ...

  8. 编写高效的C程序与C代码优化

    本文地址:http://www.cnblogs.com/archimedes/p/writing-efficient-c-and-code-optimization.html,转载请注明源地址. 说明 ...

  9. Monitor vs WaitHandle

    http://stackoverflow.com/questions/1355398/monitor-vs-waithandle-based-thread-sync A problem with Mo ...

  10. Lua从入门到精通

    1. 入门指南 http://www.cnblogs.com/linbc/archive/2009/06/02/1494622.html