关闭

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

56人阅读 评论(0) 收藏 举报
分类:

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;
    }
};
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:124568次
    • 积分:6900
    • 等级:
    • 排名:第3535名
    • 原创:589篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    最新评论