leetcode: Memoization:Longest Increasing Path in a Matrix(329)

原创 2016年08月31日 14:48:48

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


class Solution {
public:
    bool checkRange(vector<vector<int> >& matrix, int x, int y) {
        int n = matrix.size(), m = matrix[0].size();
        if(x < 0 || y < 0 || 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 = -1;
        for(int i=0; i<dir.size(); ++i) {
            int nx = x + dir[i][0], ny = y + dir[i][1];
            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) + 1);
                vis[nx][ny] = false;
            }
        }

        if(MAX == -1) return 1;
        dp[x][y] = MAX;
        return dp[x][y];
    }

    int longestIncreasingPath(vector<vector<int>>& matrix) {
        int n = matrix.size();
        if(n == 0)  return 0;

        int m = matrix[0].size();
        vector<vector<int> > dp(n, vector<int>(m, 0));
        vector<vector<bool> > vis(n, vector<bool>(m, false));
        vector<vector<int> > dir(4);
        dir[0].push_back(-1);  dir[0].push_back(0);
        dir[1].push_back(0);  dir[1].push_back(1);
        dir[2].push_back(1);  dir[2].push_back(0);
        dir[3].push_back(0);  dir[3].push_back(-1);

        int res = -1;
        for(int i=0; i<n; ++i) {
            for(int j=0; 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

-题目Equations are given in the format A / B = k, where A and B are variables represented as strings, ...

#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 ei...

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 eith...
  • toyijiu
  • toyijiu
  • 2016年05月08日 22:53
  • 125

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 ei...

leetcode解题之329. Longest Increasing Path in a Matrix Java版 (最长上升路径)

leetcode解题之329. Longest Increasing Path in a Matrix Java版 (最长上升路径)

[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[hard]

题目: 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

329. Longest Increasing Path in a MatrixDifficulty: Hard Given an integer matrix, find the length o...

[Leetcode] 329. Longest Increasing Path in a Matrix 解题报告

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

LeetCode #329 - Longest Increasing Path in a Matrix - Hard

ProblemGiven an integer matrix, find the length of the longest increasing path.From each cell, you c...
  • Arcome
  • Arcome
  • 2016年12月21日 20:29
  • 165
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode: Memoization:Longest Increasing Path in a Matrix(329)
举报原因:
原因补充:

(最多只允许输入30个字)