簡體   English   中英

計算 am*n 矩陣中的連接數

[英]Counting Connections in a m*n matrix

我試圖找到以下程序的預期輸出..但我收到錯誤

  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
  at programbasics.CountingConnections.count(CountingConnections.java:7)
  at programbasics.CountingConnections.main(CountingConnections.java:26)

我的問題是關於矩陣 m*n 矩陣中的元素填充有值 1 和 0

1表示正在建立連接, 0表示未建立連接。

我們需要垂直、水平和對角連接可用的相鄰位置,並計算建立的不同連接的數量

我的一段代碼是

  package programbasics; 
  class CountingConnections
  {
    static int count(int a[][], int i, int j) {
    int rows = a.length;
    int cols = a[0].length;
    if(a[i][j] == 0)  return 0;
    if (i == rows - 1 && j == cols - 1)
        return a[i][j];
    else if (i == rows - 1)
        return a[i][j + 1];
    else if (j == cols - 1)
        return a[i + 1][j];
    else if (a[i][j] == 1)
        return count(a, i + 1, j) + count(a, i, j + 1);
    else
        return 0;
   }
  public static void main(String[]args)
   {
   int a[][] = {{1,0,0,1},
             {0,1,1,1},
             {1,0,0,1}};
      int i = 3;
      int j = 4;
      System.out.println(count(a, i, j));;
    }
 }

預期輸出為8 喜歡的位置連接如下
1)(0,0) -> (1,1)
2)(2,0) -> (1,1)
.
.
.
.
8) (0,3) -> (1,3)

它無法獲得預期的輸出 8


         public static int count(int[][] a) {
         int[][] paths = new int[a.length][a[0].length];
         if ((paths[0][0] = a[0][0]) == 0) {
         return 0;
          }
         for (int c = 1; c < a[0].length; c++) {
           paths[0][c] = a[0][c] * paths[0][c - 1];
           }
            for (int r = 1; r < a.length; r++) 
           { 
         paths[r][0] = a[r][0] * paths[r - 1][0];
           for (int c = 1; c < a[r].length; c++) 
             {
        paths[r][c] = a[r][c] * (paths[r - 1][c] + paths[r][c - 1]);
          }
        }
           return paths[a.length - 1][a[0].length - 1];
        }
public static int count(int[][] a, int m, int n) {

    int count = 0;

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {

            if (a[i][j] == 1) {
                if (i - 1 >= 0 && j - 1 >= 0 && a[i - 1][j - 1] == 1) {
                    count = count + 1;

                }
                if (i - 1 >= 0 && a[i - 1][j] == 1) {
                    count = count + 1;
                }
                if (i - 1 >= 0 && j + 1 < n && a[i - 1][j + 1] == 1) {
                    count = count + 1;
                }
                if (j + 1 < n && a[i][j + 1] == 1) {
                    count = count + 1;
                }
            }
        }
    }


    return count;
}

您在代碼中調用if(a[i][j] == 0) ,其中將3作為i傳遞,將4作為j傳遞。 但是Array的索引為零,因此當您嘗試調用a[3][4]您正在嘗試調用

     0    1    2    3    4

0   {1,   0,   0,   1}

1   {0,   1,   1,   1}

2   {1,   0,   0,   1}

3                         X

4

X所在的索引。 顯然,這不是Array的有效索引。

此外,您在不同點的方法調用a[i + 1][j]a[i][j + 1]這意味着在確保代碼保持在邊界內時,您必須考慮到這一點。

至於您的實際方法,您的邏輯似乎有點偏離。 if(a[i][j] == 0) return 0; 將返回0並停止遞歸並返回0而不檢查是否有更多連接。 你的邏輯應該更像這樣:

  1. 從 0,1 開始。
  2. 如果索引是1向右看一個索引,一個向下和向右(對角線)向下一個,向下一個向左(第二個對角線)。
    如果這些索引處的任何數字是1 ,則向上計數。
  3. 繼續遍歷矩陣,但請記住,您正在反復檢查一行,因此您只會循環直到行和列的長度都小於 -1。 還要確保您從索引a[i][1]因為您需要檢查a[a+1][j-1]並且如果j == 0您將嘗試調用a[a+1][-1]這將導致另一個索引越界
private int countConnections(int[][] a, int rows, int columns) {
    //cartesian plane coordinates around a point
    final int[] x = {1, 1, 1, -1, -1, -1, 0, 0};
    final int[] y = {1, -1, 0, 1, -1, 0, 1, -1};
    int count = 0;
    boolean[][] visited = new boolean[rows][columns];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            for (int k = 0; k < 8; k++) {
                int l = i + x[k];
                int m = j + y[k];
                //check for connections only if the given cell has value 1
                if (a[i][j] == 1 && canVisit(l, m, rows, columns, visited) && a[l][m] == 1) {
                    count++;
                }

            }
            visited[i][j] = true;
        }
    }
    return count;
}

private boolean canVisit(int i, int j, int rows, int columns, boolean [][] visited) {
    return i < rows && j < columns && i >= 0 && j >= 0 && !visited[i][j];
}

檢查單元格值為 1 的單元格周圍的所有 8 個單元格,並在遍歷時將其標記為已訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM