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 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:
    struct node
    {
        int x;
        int y;
    };
    queue<node> dict;
    
    int checkru(vector<vector<int>>& matrix,int a,int b,int m,int n)
    {
        int r=0;
        if(a>0)
        {
            if(matrix[a][b]>matrix[a-1][b])
            r++;
        }
        if(a<m-1)
        {
            if(matrix[a][b]>matrix[a+1][b])
            r++;
        }
        if(b>0)
        {
            if(matrix[a][b]>matrix[a][b-1])
            r++;
        }
        if(b<n-1)
        {
            if(matrix[a][b]>matrix[a][b+1])
            r++;
        }
        return r;
    }
    
    int longestIncreasingPath(vector<vector<int>>& matrix) {
        int m  = matrix.size();
        int n;
        int i,j;
        vector<vector<int>> ru;
        vector<vector<int>> maxpath;
        int max=1;
        if(m==0)
        return 0;
        n = matrix[0].size();
        if(n==0)
        return 0;
        for(i=0;i<m;i++)
        {
            vector<int> k;
            vector<int> path;
            for(j=0;j<n;j++)
            {
                node nd;
                int uu;
                nd.x=i;
                nd.y=j;
                uu = checkru(matrix,i,j,m,n);
                k.push_back(uu);
                path.push_back(1);
                if(uu==0)
                dict.push(nd);
                
            }
            ru.push_back(k);
            maxpath.push_back(path);
        }
        while(!dict.empty())
        {
            node nd = dict.front();
            node kk;
            int a = nd.x;
            int b = nd.y;
            if(a>0)
            {
                if(matrix[a][b]<matrix[a-1][b])
                {
                    kk.x=a-1;kk.y=b;
                    if(maxpath[a][b]+1>maxpath[a-1][b])
                    maxpath[a-1][b] = maxpath[a][b]+1;
                    ru[a-1][b]--;
                    if(ru[a-1][b]==0)
                    dict.push(kk);
                    if(maxpath[a-1][b]>max)
                    max = maxpath[a-1][b];
                }
            }
             if(b>0)
            {
                if(matrix[a][b]<matrix[a][b-1])
                {
                    kk.x=a;kk.y=b-1;
                    if(maxpath[a][b]+1>maxpath[a][b-1])
                    maxpath[a][b-1] = maxpath[a][b]+1;
                    ru[a][b-1]--;
                    if(ru[a][b-1]==0)
                    dict.push(kk);
                    if(maxpath[a][b-1]>max)
                    max = maxpath[a][b-1];
                }
            }
             if(a<m-1)
            {
                if(matrix[a][b]<matrix[a+1][b])
                {
                    kk.x=a+1;kk.y=b;
                    if(maxpath[a][b]+1>maxpath[a+1][b])
                    maxpath[a+1][b] = maxpath[a][b]+1;
                    ru[a+1][b]--;
                    if(ru[a+1][b]==0)
                    dict.push(kk);
                     if(maxpath[a+1][b]>max)
                    max = maxpath[a+1][b];
                }
            }
             if(b<n-1)
            {
                if(matrix[a][b]<matrix[a][b+1])
                {
                    kk.x=a;kk.y=b+1;
                    if(maxpath[a][b]+1>maxpath[a][b+1])
                    maxpath[a][b+1] = maxpath[a][b]+1;
                    ru[a][b+1]--;
                    if(ru[a][b+1]==0)
                    dict.push(kk);
                    if(maxpath[a][b+1]>max)
                    max = maxpath[a][b+1];
                }
            }
            dict.pop();
        }
        return max;
    }
};

点赞