leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)
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 + 记忆化搜索)的更多相关文章
- 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 ...
- [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 ...
- [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 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 ...
- [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 ...
- 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 ...
- 329. Longest Increasing Path in a Matrix
最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...
- Java实现 LeetCode 688 “马”在棋盘上的概率(DFS+记忆化搜索)
688. "马"在棋盘上的概率 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有 ...
随机推荐
- 分析jQuery中的each方法
在看jQuery源码是怎么实现each方法之前,我们看一下js的原生实现. ECMAScript 5为数组定义了一个forEach方法,该方法接受两个参数:第一个参数是要在每一个数组项上运行的函数,第 ...
- c++调用matlab生成的Dll动态连接库
点击打开链接http://download.csdn.net/detail/nuptboyzhb/4228429 c++调用matlab生成的Dll动态连接库 实验平台: matlab 7.0(R ...
- json中jobject
Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...
- [矩阵快速幂]HDOJ4565 So Easy!
题意:给a, b, n, m 求 $\left \lceil ( a+ \sqrt b )^n \right \rceil$ % m 看到 $( a+ \sqrt b )^n$ 虽然很好联想到共轭 但 ...
- TCP长连接与短连接的区别
http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html 1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,se ...
- JavaScript基本程序结构
条件判断 JavaScript使用if () { ... } else { ... }来进行条件判断.例如,根据年龄显示不同内容,可以用if语句实现如下: var age = 20; if (age ...
- python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
文件夹里有多个RM格式的视频文件,现需要把它们的文件名都提取出来,并去掉文件的扩展名,以便放到需要的网页里. 源代码: # --- picknames.py --- import os filenam ...
- JavaScript DOM高级程序设计 5动态修改样式和层叠样式表1(源代码)--我要坚持到底!
W3C DOM2样式规范 现在这边贴出本章要的源代码,注意要结合前面用到的ADS库http://vdisk.weibo.com/s/Dq8NU CSSStyleSheet对象属性: type :始终是 ...
- Git教程(11)把本地的项目传到远程
1,在远程建立仓库 得到远程仓库地址,如: https://github.com/paulboone/ticgit 2,进入到项目根目录,初始化一个本地仓库 $ git init 3,为本地仓库添加 ...
- 【HDOJ】4418 Time travel
1. 题目描述K沿着$0,1,2,\cdots,n-1,n-2,n-3,\cdots,1,$的循环节不断地访问$[0, n-1]$个时光结点.某时刻,时光机故障,这导致K必须持续访问时间结点.故障发生 ...