码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 329. Longest Increasing Path in a Matrix

时间:2016-04-29 14:21:49      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

 

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

 

leetcode 329. Longest Increasing Path in a Matrix

标签:

原文地址:http://www.cnblogs.com/julyfrost/p/5445780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!