2017-07-06 51 views
0

对于nXn矩阵,我有以下问题: ,我们将定义一个大小为k的“蠕虫”,它是一系列具有连续数字的相邻单元格。相邻单元格是当前单元格的右\左\上\下单元格(而不是对角线)。 我需要编写一个递归函数,其返回的最长蠕虫在阵列 例如:在矩阵:递归 - 找到矩阵中最大的蠕虫

{{3,4,5,6}, 
{5,6,2,7}, 
{12,13,14,15}, 
{19,18,17,16}}; 

最长蠕虫是8个细胞。从[2] [0]开始到[3] [0]结束。

到目前为止,我已经写了下面的代码:

public static int longestWarm(int[][] arr, int row, int col) { 
    if (row < 0 || row >= arr.length || col < 0 || col >= arr.length) return 0; 
    if (col >= arr.length || row >= arr.length) return 0; 

    int sum1 = 1, sum2 = 1, sum3 = 1, sum4 = 1; 

    if (row > 0 && arr[row][col] == arr[row - 1][col] - 1) 
      sum1 = 1 + longestWarm(arr, row - 1,col); 
    else if (row < arr[0].length - 1 && arr[row][col] == arr[row + 1][col] - 1) 
      sum2 = 1 + longestWarm(arr, row + 1, col); 
    else if (col > 0 && arr[row][col] == arr[row][col - 1] - 1) 
      sum3 = 1 + longestWarm(arr, row, col - 1); 
    else if (col < arr[0].length - 1 && arr[row][col] == arr[row][col + 1] - 1) 
      sum4 = 1 + longestWarm(arr, row, col + 1); 

    int max1 = Math.max(sum1, sum2); 
    int max2 = Math.max(sum3, sum4); 

    return Math.max(max1, max2); 
} 

的问题是我得到的第一个蠕虫病毒,我在矩阵找到,而不是最大的一个。我的输出是5而不是8. 请帮助找到我做错了什么。

+1

欢迎堆栈溢出!它看起来像你需要学习使用调试器。请帮助一些[互补调试技术](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之后仍然遇到问题,请随时返回一个[最小,完整且可验证的示例](http://stackoverflow.com/help/mcve),以说明您的问题。 –

+1

我想你想摆脱'else's。此外,在顶层,您需要调用您为每个数组元素提交一次的方法,并取得最大的结果。 –

+0

@JohnBollinger感谢他的工作,现在我创建了另一种方法,检查当前计数是否大于maxcount,然后返回最大 – Meni

回答

0

我使用的方法is_tail只修剪检查蠕虫开始在一个单元格的邻居不小于1。如果存在这样一个邻居,那么它显然不是最长蠕虫的尾巴。从每个尾巴中,您可以找到从该细胞生长的最大尺寸的蠕虫,与您所做的相似。

import java.util.ArrayList; 

//a pair of ints, row and col, that indicate a cell in a matrix 
class Cell { 
    int row; 
    int col; 

    public Cell(int row, int col) { 
     this.row = row; 
     this.col = col; 
    } 

    public int get_row() { 
     return row; 
    } 

    public int get_col() { 
     return col; 
    } 

    public String toString() { 
     return "row: " + row + " col: " + col; 
    } 
} 

// a class to hold the matrix data and methods 
// so that every call doesnt require passing the pointer m 
class Matrix { 
    // m used for the matrix so that i dont have to type matrix repeatedly 
    int[][] m; 
    int height; 
    int width; 

    public Matrix(int[][] m) { 
     this.m = m; 
     height = m.length; 
     width = m[0].length; 
    } 

    public int get_height() { 
     return height; 
    } 

    public int get_width() { 
     return width; 
    } 

    // checks if the cell at row,col is a tail 
    // a tail is a cell with 0 neighbors that are 1 less than its value 
    public boolean is_tail(int row, int col) { 
     if (row < 0 || row >= m.length || col < 0 || col >= m[0].length) 
      return false; 

     int curVal = m[row][col]; // Value in the cell being checked 

     // check if neighbor is out of bounds, then if it is smaller 
     if (row - 1 >= 0 && m[row - 1][col] == curVal - 1) 
      return false; 
     if (row + 1 < m.length && m[row + 1][col] == curVal - 1) 
      return false; 
     if (col - 1 >= 0 && m[row][col - 1] == curVal - 1) 
      return false; 
     if (col + 1 < m[0].length && m[row][col + 1] == curVal - 1) 
      return false; 

     return true; 
    } 

    public ArrayList<Cell> longestWorm(int row, int col) { 
     if (row < 0 || row >= m.length || col < 0 || col >= m[0].length) 
      return new ArrayList<Cell>(); 

     int curVal = m[row][col]; 
     ArrayList<Cell> longest_path = new ArrayList<Cell>(); 

     if (row - 1 >= 0 && m[row - 1][col] == curVal + 1) { 
      ArrayList<Cell> path_left = longestWorm(row - 1, col); 
      if (path_left.size() > longest_path.size()) 
       longest_path = path_left; 
     } 

     if (row + 1 < m.length && m[row + 1][col] == curVal + 1) { 
      ArrayList<Cell> path_right = longestWorm(row + 1, col); 
      if (path_right.size() > longest_path.size()) 
       longest_path = path_right; 
     } 

     if (col - 1 >= 0 && m[row][col - 1] == curVal + 1) { 
      ArrayList<Cell> path_up = longestWorm(row, col - 1); 
      if (path_up.size() > longest_path.size()) 
       longest_path = path_up; 
     } 

     if (col + 1 < m[0].length && m[row][col + 1] == curVal + 1) { 
      ArrayList<Cell> path_down = longestWorm(row, col + 1); 
      if (path_down.size() > longest_path.size()) 
       longest_path = path_down; 
     } 

     longest_path.add(new Cell(row, col)); 
     return longest_path; 
    } 
} 

class Main { 

    public static void main(String[] args) { 
     int[][] matrix = { { 3, 4, 5, 6 }, { 5, 6, 2, 7 }, { 12, 13, 14, 15 }, { 19, 18, 17, 16 } }; 

     ArrayList<Cell> longest_worm = new ArrayList<Cell>(); 

     Matrix myMatrix = new Matrix(matrix); 
     for (int i = 0; i < myMatrix.get_height(); i++) { 
      for (int j = 0; j < myMatrix.get_width(); j++) { 
       if (myMatrix.is_tail(i, j)) { 
        ArrayList<Cell> new_worm = myMatrix.longestWorm(i, j); 
        if (new_worm.size() > longest_worm.size()) { 
         longest_worm = new_worm; 
        } 
       } 
      } 
     } 

     for (int i = longest_worm.size() - 1; i >= 0; i--) { 
      System.out.println(longest_worm.get(i)); 
     } 
    } 
} 
+0

感谢您的评论,我忘记告诉的是,我不能在任何地方循环使用递归。 – Meni

0

你的程序工作正常。 加入此项。

public static int longestWarm(int[][] arr) { 
    int len = arr.length; 
    int max = 0; 
    for (int row = 0; row < len; ++row) { 
     for (int col = 0; col < len; ++col) { 
      int length = longestWarm(arr, row, col); 
      if (length > max) 
       max = length; 
     } 
    } 
    return max; 
} 

并尝试这个

int[][] arr = { 
    {3, 4, 5, 6}, 
    {5, 6, 2, 7}, 
    {12, 13, 14, 15}, 
    {19, 18, 17, 16}}; 
System.out.println("lngest warm=" + longestWarm(arr)); 
+0

感谢您的评论,我忘记告诉的是,我不能在任何地方使用循环只是递归 – Meni