【LeetCode】329. Longest Increasing Path in a Matrix (Hard)

来源:互联网 发布:web前端后端数据交互 编辑:程序博客网 时间:2024/04/24 18:34

【题目】

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

【解】

建图,如果a < b,则a到b有一条有向边。然后拓扑排序。

然后从拓扑排序排在后面的节点开始用动态规划算从这个节点开始最大的路径长度。然后再找最大的。。。。。。

class Solution {public:    int longestIncreasingPath(vector<vector<int>>& matrix) {        int r = matrix.size();        if (r == 0) return 0;        int c = matrix[0].size();        this->row = r;        this->col = c;        vector<pair<int, int>> top_order(row * col);        vector<vector<int>> path_len(row, vector<int>(col, 0));        top_sort(matrix, top_order);        int result = 0;        for (int i = row * col - 1; i >= 0; i--) {            int m = 1;            for (int j = 0; j < 4; j++) {                int x = top_order[i].first + dx[j], y = top_order[i].second + dy[j];                if (x < 0 || x >= row || y < 0 || y >= col) continue;                if (matrix[x][y] > matrix[top_order[i].first][top_order[i].second]) {                    if (1 + path_len[x][y] > m) {                        m = 1 + path_len[x][y];                    }                }            }            path_len[top_order[i].first][top_order[i].second] = m;            if (result < m) result = m;        }        return result;    }    void top_sort(vector<vector<int>>& matrix, vector<pair<int, int>>& top_order) {        int n = row * col - 1;        vector<vector<bool>> visited(row, vector<bool>(col, false));        for (int i = 0; i < row; i++) {            for (int j = 0; j < col; j++) {                if (!visited[i][j]) {                    visited[i][j] = true;                    dfs(matrix, top_order, i, j, n, visited);                }            }        }    }    void dfs(vector<vector<int>>& matrix, vector<pair<int, int>>& top_order, int sx, int sy, int& n, vector<vector<bool>>& visited) {        for (int i = 0; i < 4; i++) {            int x = sx + dx[i], y = sy + dy[i];            if (x < 0 || x >= row || y < 0 || y >= col || visited[x][y] || matrix[x][y] <= matrix[sx][sy]) continue;            visited[x][y] = true;            dfs(matrix, top_order, x, y, n, visited);        }        top_order[n] = make_pair(sx, sy);        n--;    }    int dx[4] = { 0, 1, 0, -1 };    int dy[4] = { 1, 0, -1, 0 };    int row;    int col;};


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高三学习不好怎么办 高考孩子焦虑症怎么办 孩子高三厌学怎么办 高三厌学家长怎么办 高中生学习压力大怎么办 孩子反应有点慢怎么办 小学计算总出错怎么办 孩子粗心丢分怎么办 总是粗心丢分怎么办 卷子落在教室了怎么办 孩子总是丢题怎么办 孩子老师落题怎么办 孩子做题不认真马虎怎么办 工作犯了大错怎么办 工作上总是出错怎么办 工作中总是出错怎么办 晚上睡觉容易惊醒怎么办 每天早上被吵醒怎么办 孩子好动不爱学习怎么办 自习课不写作业怎么办 不爱学习的小孩怎么办 一年级小孩不爱学习怎么办 小孩不听话不爱学习怎么办 父母犯错我们该怎么办 孩子学习不爱动脑筋怎么办 孩子考试太马虎怎么办 小孩做作业马虎怎么办 同学抄我作业怎么办 别人抄我答案怎么办 学生考试前紧张怎么办 科目一考试紧张怎么办 1年级不认真学习怎么办 学生考试时紧张怎么办 小朋友不写作业怎么办 面对作业多应该怎么办 小学学生作业多怎么办 二年级学生马虎怎么办 孩子做数学题粗心怎么办 小孩拉的屎很粗怎么办 生地会考没及格怎么办 小孩大便特别粗怎么办