首页 > 试题广场 >

矩阵最长递增路径

[编程题]矩阵最长递增路径
  • 热度指数:52775 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个 n 行 m 列矩阵 matrix ,矩阵内所有数均为非负整数。 你需要在矩阵中找到一条最长路径,使这条路径上的元素是递增的。并输出这条最长路径的长度。
这个路径必须满足以下条件:

1. 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外。
2. 你不能走重复的单元格。即每个格子最多只能走一次。

数据范围:
进阶:空间复杂度 ,时间复杂度

例如:当输入为[[1,2,3],[4,5,6],[7,8,9]]时,对应的输出为5,
其中的一条最长递增路径如下图所示:

示例1

输入

[[1,2,3],[4,5,6],[7,8,9]]

输出

5

说明

1->2->3->6->9即可。当然这种递增路径不是唯一的。       
示例2

输入

[[1,2],[4,3]]

输出

4

说明

 1->2->3->4   

备注:
矩阵的长和宽均不大于1000,矩阵内每个数不大于1000
#define max(a, b) (a>b ? a : b)
int run(int** matrix, int matrixRowLen, int matrixColLen, int row, int col, int lastMVal) {    
    int res=0;
    if(matrix[row][col]<=lastMVal)
        return 0;
    if(row<matrixRowLen-1) 
        res = max(res, run(matrix, matrixRowLen, matrixColLen, row+1, col, matrix[row][col]));
    if(row>0) 
        res = max(res, run(matrix, matrixRowLen, matrixColLen, row-1, col, matrix[row][col]));
    if(col<matrixColLen-1) 
        res = max(res, run(matrix, matrixRowLen, matrixColLen, row, col+1, matrix[row][col]));
    if(col>0) 
        res = max(res, run(matrix, matrixRowLen, matrixColLen, row, col-1, matrix[row][col]));   
    return res+1;
}

int solve(int** matrix, int matrixRowLen, int* matrixColLen ) {
    int  res=0, i, j;
    for(i=0; i<matrixRowLen; i++) 
        for(j=0; j<*matrixColLen; j++) 
            res = max(res, run(matrix, matrixRowLen, *matrixColLen, i, j, -1)); 
    return res;
}

发表于 2024-03-23 22:01:09 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 递增路径的最大长度
 * @param matrix int整型二维数组 描述矩阵的每个数
 * @param matrixRowLen int matrix数组行数
 * @param matrixColLen int* matrix数组列数
 * @return int整型
 */

#include <stdbool.h>
#include <stdlib.h>
struct Node {
    int m;
    int n;
    int val;
    int idx;
    struct Node* next;
};


bool ifcollision(int m, int n, struct Node* pfirst, int maxm, int maxn) {

    while (pfirst != NULL) {
        if (m == pfirst->m && n == pfirst->n) return true;
        pfirst = pfirst->next;
    }
    if (m >= maxm || n >= maxn || m < 0 || n < 0) {
        return true;
    }
    return false;
}






void walk(int** matrix, int matrixRowLen, int matrixColLen, struct Node* pfirst,
          struct Node* pplastnode, int* maxlen) {

    struct Node* curlast =  (struct Node*)malloc(sizeof(struct Node));
    int lastm = pplastnode->m, lastn = pplastnode->n;
    if (pplastnode->idx > *maxlen) *maxlen = pplastnode->idx;
    for (int i = 1; i >= -1; i--) {
        for (int j = 1; j >= -1; j--) {
            if ((i != 0 && j != 0) || (i == 0 && j == 0)) continue;
            if ( !ifcollision(lastm + i, lastn + j, pfirst, matrixRowLen, matrixColLen) &&
                    (matrix[lastm + i][lastn + j] > pplastnode->val)) {
                //printf(">> curm = %d curn = %d val = %d ", curm, curn, curlast->val);
                //printf("next val =%d\n", matrix[curm + i][curn + j]);
                curlast->m = lastm + i;
                curlast->n = lastn + j;
                curlast->idx = pplastnode->idx + 1;
                curlast->val = matrix[lastm+i][lastn+j];
                curlast->next = NULL;
                pplastnode->next = curlast;
                walk(matrix, matrixRowLen,  matrixColLen, pfirst, curlast, maxlen);
            }
        }
    }
    free(curlast);
}

int solve(int** matrix, int matrixRowLen, int* matrixColLen ) {

    int m, n;
    int maxlen = 0;
    struct Node* pfirstnode;
    struct Node* firstnode = (struct Node*)malloc(sizeof(struct Node));
    for (int row = 0; row < matrixRowLen; row++) {
        for (int col = 0; col < *matrixColLen; col++) {
            //printf("matrix[%d][%d] = %d\n", row, col, matrix[row][col]);
            pfirstnode = firstnode;
            m = row;
            n = col;
            firstnode->m = m;
            firstnode->n = n;
            firstnode->idx = 1;
            firstnode->val = matrix[row][col];
            firstnode->next = NULL;
            walk(matrix, matrixRowLen, *matrixColLen, pfirstnode, firstnode, &maxlen);
            //printf("%d\n", maxlen);
            //break;
        }
        //break;
    }
    free(firstnode);
    //printf("%d %d %d\n",m,n, minnumb);
    return maxlen;
}

内存不能自动释放真麻烦

发表于 2023-03-06 21:48:56 回复(0)