【问题标题】:Traversing matrix values in diagonal "zig-zags"以对角线“之字形”遍历矩阵值
【发布时间】:2021-03-09 13:50:24
【问题描述】:

我有一个定义大小为 n x n 的矩阵。在矩阵网格的每个索引处,都有一个整数值。网格是通过每隔 n 个值包装整数来创建的。请参阅下面的 4x4 示例,从字符串输入“6 4 7 5 3 8 9 2 1 5 1 7 1 6 2 8”创建:

我想遍历网格,只打印对角线左侧和/或右侧的值,然后从新找到的值中打印它们的对角线。

示例网格的一些示例路线(上面标有 * 的一条):

index[0][0]: [6,8,1,6], [6,8,1,6], [6,8,1,8]

index[0][1]: [4,3,5,1], [4,3,5,2], [4,9,5,2], [4,9,5,1], [4,9,7,2] *

路线将输出为:

4351, 4352, 4952 etc.

基本上,我想在网格中以“之字形”的方式找到所有路线。

我目前的代码如下:

public static void main(String[] args)
{
        int s = 4; //For demo purposes
        int k = 0;
        

        String grid[][] = new String[s][s]; //Grid will always be n x n
        
        Scanner sc = new Scanner(System.in); //Handles a whitespace delimited string
        String[] in = sc.nextLine().split("\\s+");


        for (int i = 0; i < size; i++) { //Creates the grid
                for (int j = 0; j < size; j++) {
                        grid[i][j] = in[k];
                        k++;
                }
        }
        
        for (int j = 0; j < size; j++) { //Route finder
                for (int i = 0; i < size; i++) { 
                    //find a diagonal value and append the value to the route
                }
                //Print the route integers for this specific route
        }
}

我将如何在矩阵中找到这些路线?还可以解决万能的 IndexOutOfBounds 错误吗?

【问题讨论】:

  • 如果您在循环中return,您将没有机会找到任何替代路线。
  • @ScottHunter 编辑了代码 sn-p 以改为读取打印。
  • @ScottHunter 我在第二个循环中有边界检查器,但省略了它们以允许其他用户的任何想法解决它。循环本身不会给出边界错误,但会检查对角线值是否存在。执行grid[i+1][j+1]grid[i+1][j-1] 会直接在运行时出错,因为后者会尝试检查-1 索引列。 This 是我指的 IndexOutOfBounds 错误。如果不清楚,请致歉。

标签: java arrays for-loop matrix indexing


【解决方案1】:

这是一个递归解决方案:

  public static void main(String[] args) {
    int k = 0;
    int size = 4; // For demo purposes

    String grid[][] = new String[size][size]; //Grid will always be n x n
    String[] in = "6 4 7 5 3 8 9 2 1 5 1 7 1 6 2 8".split("\\s+"); // For demo purposes

    for (int i = 0; i < size; i++) { //Creates the grid
      for (int j = 0; j < size; j++) {
        grid[i][j] = in[k];
        k++;
      }
    }

    ArrayList<String> paths = new ArrayList<String>();
    for(int c = 0; c < size; c++) {
      // start at each column in the first row
      findPaths(0, c, grid, "", paths);
    }

    // output the paths found
    // *note that for the demo grid, there are "duplicate paths" taking different physical routes in the grid
    for(String path : paths) {
      System.out.println(path);
    }
  }    

  public static void findPaths(int row, int col, String[][] grid, String currentPath, ArrayList<String> paths) {
    // are we within the grid?
    if ((row >=0) && (row < grid.length) && (col >= 0) && (col < grid[row].length)) {
      currentPath = currentPath + grid[row][col];

      // did we hit the bottom?
      if (row == (grid.length - 1)) {
        paths.add(currentPath);
      } 
      else {
        // go left
        findPaths(row+1, col-1, grid, currentPath, paths);
        // go right
        findPaths(row+1, col+1, grid, currentPath, paths);
      } 
    }
  }

制作:

6816
6816
6818
4351
4352
4951
4952
4972
7816
7816
7818
7216
7218
5951
5952
5972

【讨论】:

  • 欣赏解决方案!今天晚些时候会看看它,看看我是否遇到任何问题。
  • 更新:效果很好!与扫描仪的交互有点奇怪,当用户输入字符串时,它需要很长时间才能写入网格。但是,当字符串在代码中时没有问题。
  • 很高兴听到它运行良好。不知道为什么在使用扫描仪时它会变慢......必须看看你的新代码。
  • Scanner sc = new Scanner(System.in); String[] in = sc.nextLine().split("\\s+"); 理想情况下,程序会读取多行输入(每行值对应一行),但我正在研究解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多