zoukankan      html  css  js  c++  java
  • Longest Continuance Increasing sub-sequence in matrix

    一个矩阵,求最长连续的序列长度

    [1 2 3
       4 5 6
       7 8 9]
    我的解法时用dfs遍历矩阵,如果便利过的元素就标记为false,不再遍历。
    邻居 就是上下左右
    e.g.
    1 2 3
    6 5 4 -> 7
    7 9 8
    ===================
    5 7 9
    1 2 3 -> 3
    4 6 8
    ===================
    9 8 7
    4 5 6 ->9
    3 2 1
    题目一. 只能上下左右,走到边界处止步
    题目二. 每一行/列都是一个循环,走到边界处譬如第 0 列,可以继续走到另一边界,譬如 n-1
     
     1 /*
     2  * Graph DFS
     3  * 
     4  * Find the Longest Continuance Increasing sub-sequence in 2d matrix
     5  * You could go up, down, left, right in the matrix
     6  * 
     7  */
     8 
     9 public class LIS2d {
    10     public int findLongest(int[][] matrix) {
    11         int max = 0;
    12         int m = matrix.length, n = matrix[0].length;
    13         boolean[][] visit = new boolean[m][n];
    14         for (int i = 0; i < m; i++) {
    15             for (int j = 0; j < n; j++) {
    16                 int len = helper(matrix, visit, i, j, 1);
    17                 max = max > len ? max : len;
    18             }
    19         }
    20         return max;
    21     }
    22     public int helper(int[][] matrix, boolean[][] visit, int i, int j, int step) {
    23         visit[i][j] = true;
    24         int max = step;
    25         int m = matrix.length, n = matrix[0].length;
    26 //        if (i > 0 && matrix[i-1][j] == matrix[i][j]+1 && !visit[i-1][j]) {
    27 //            int len = helper(matrix, visit, i-1, j, step+1);
    28 //            max = max > len ? max : len;
    29 //        }
    30 //        if (i < m-1 && matrix[i+1][j] == matrix[i][j]+1 && !visit[i+1][j]) {
    31 //            int len = helper(matrix, visit, i+1, j, step+1);
    32 //            max = max > len ? max : len;
    33 //        }
    34 //        if (j > 0 && matrix[i][j-1] == matrix[i][j]+1 && !visit[i][j-1]) {
    35 //            int len = helper(matrix, visit, i, j-1, step+1);
    36 //            max = max > len ? max : len;
    37 //        }
    38 //        if (j < n-1 && matrix[i][j+1] == matrix[i][j]+1 && !visit[i][j+1]) {
    39 //            int len = helper(matrix, visit, i, j+1, step+1);
    40 //            max = max > len ? max : len;
    41 //        }
    42         int r = (i-1+m) % m;
    43         int c = j;
    44         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
    45             int len = helper(matrix, visit, r, c, step+1);
    46             max = max > len ? max : len;
    47         }
    48         r = (i+1+m) % m;
    49         c = j;
    50         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
    51             int len = helper(matrix, visit, r, c, step+1);
    52             max = max > len ? max : len;
    53         }
    54         r = i;
    55         c = (j-1+n) % n;
    56         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
    57             int len = helper(matrix, visit, r, c, step+1);
    58             max = max > len ? max : len;
    59         }
    60         r = i;
    61         c = (j+1+n) % n;
    62         if (matrix[r][c] == matrix[i][j]+1 && !visit[r][c]) {
    63             int len = helper(matrix, visit, r, c, step+1);
    64             max = max > len ? max : len;
    65         }
    66         visit[i][j] = false;
    67         return max;
    68     }
    69     public static void main(String[] args) {
    70         int[][] matrix = {{5, 6, 7},
    71                           {4, 10, 9},
    72                           {3, 2, 8}};
    73         LIS2d lis = new LIS2d();
    74         System.out.println(lis.findLongest(matrix));
    75     }
    76 }
  • 相关阅读:
    获取当前季的js
    C#获取文件大小
    SQL Server 2005 Express Edition 傻瓜式安装
    SET XACT_ABORT ON
    Resignation letter
    Exchange Web Services Managed API 1.0 入门
    Please let us know in case of any issues
    33条C#、.Net经典面试题目及答案
    c# 修饰词public, protected, private,internal,protected的区别
    EXEC DTS
  • 原文地址:https://www.cnblogs.com/joycelee/p/5348982.html
Copyright © 2011-2022 走看看