2016-03-05 47 views
-1

如果我有一個二維數組,如下圖所示,我需要獲取最大行的最大值和最左列(一個值)並存儲該值的座標(I,J)。然後,我需要將該子數組視爲該數值的右下角,並再次從最頂端的行或最左邊的列中找到最大值。這將繼續貫穿整個二維數組,以便最終獲得所有值的Nx2座標數組。 (0,0),(1,1),(3,2),(4,3),((3),(4), 5,4)。如何查找沿2d數組中子數組最頂行和最左列的最大值?

4 2 2 1 0 
2 3 2 1 0 
2 2 1 1 1 
1 1 2 1 0 
1 1 0 1 0 
0 0 1 0 0 

我曾嘗試以下,但每個子陣列的左上角總是從對角線(即(0,0),(1,1)......),而不是正下方的價值和在前一個最大值的右側。例如,如果先前的最大值具有座標(3,2),則下一個子陣列的左上角應該是(4,3)。

// Top left corner of subarray set each time. 

for(int p = 0; p < myMatrix.length; p++){ 
    int maximum = myMatrix[p][p]; 
     coordinates[p][0] = p; 
     coordinates[p][1] = p; 

// Iterates through leftmost column of subarray. 
    for(int i = p; i < table_data.length; i++){ 
     if(maximum <= myMatrix[i][p]){    
      maximum = myMatrix[i][p]; 
      coordinates[p][0] = i; 
      coordinates[p][1] = p; 
     } 
    } 

//Iterates through topmost row of subarray. 

    for(int j = p; j < myMatrix[0].length; j++){ 
     if(maximum <= table_data[p][j]){   
     maximum = myMatrix[p][j];     
     coordinates[p][0] = p;      
     coordinates[p][1] = j; 
     } 
    } 
} 

回答

0

首先,我不明白myMatrixtable_data之間的關係。我想你不需要兩個。

此外,你迭代他們是獨立的,而我猜他們應該是相同的矩陣。

此外,您呈現的示例數據沒有相同數量的行和列。我不知道這是否是故意的,但如果是這樣的話,您還沒有指定搜索停止的位置;在行數或列數中。

無論如何,考慮到這一點是一個工作解決方案。

int[][] myMatrix = {{4, 2, 2, 1, 0} , 
         {2, 3, 2, 1, 0}, 
         {2, 2, 1, 1, 1}, 
         {1, 1, 2, 1, 0}, 
         {1, 1, 0, 1, 0}, 
         {0, 0, 1, 0, 0} }; 
    int coordinates[][] = new int[myMatrix.length][2]; 
    int maximum[] = new int[myMatrix.length]; 
    int m = Math.min(myMatrix.length, myMatrix[0].length); 
    for(int p = 0; p < m; p++){ 
     maximum[p] = myMatrix[p][p]; 
     coordinates[p][0] = p; 
     coordinates[p][1] = p; 
     for(int j = p; j < myMatrix[p].length; j++){ 
      if(maximum[p] <= myMatrix[p][j]){   
       maximum[p] = myMatrix[p][j];     
       coordinates[p][0] = p;      
       coordinates[p][1] = j; 
      } 
     } 
     for(int i = p; i < myMatrix[p].length; i++){ 
      if(maximum[p] <= myMatrix[i][p]){    
       maximum[p] = myMatrix[i][p]; 
       coordinates[p][0] = i; 
       coordinates[p][1] = p; 
      } 
     } 
+0

謝謝!你是對的,我只需要myMatrix,table_data變量是一個錯誤。 –

0

問題是你總是從第p次迭代的點p,p開始。 相反,你可以使用兩個變量

int top = coordinates[p][0] + 1; 
int left = coordinates[p][1] + 1; 

和寫入使用它們,東西環路像

for(int i = left; i < table_data.length; i++){ 
    if(maximum <= myMatrix[i][top]){   

(我可能會倒x和y,但是這是想法)。

然後你還需要修改你的封閉循環,它不總是有迭代。相反,檢查頂部和左側仍處於矩陣範圍內的「while」循環將起作用。

0

試試這個:

所有的
public static void main(String[] a) throws Exception { 
    int[][] aa = { { 4, 2, 2, 1, 0 }, { 2, 3, 2, 1, 0 }, { 2, 2, 1, 1, 1 }, { 1, 1, 2, 1, 0 }, { 1, 1, 0, 1, 0 }, 
      { 0, 0, 1, 0, 0 } }; 
    int[][] temp = aa; 
    printArray(aa); 
    System.out.println("---------------------"); 

    while ((temp = getArray(temp)) != null) { 
     printArray(temp); 

     System.out.println("---------------------"); 
    } 

} 

static int[][] getArray(int[][] aa) { 
    int row = aa.length; 
    int col = aa[0].length; 
    if (row == 2 || col == 2) { // we can also try if (row == 1 || col == 1) to get the last array 
     return null; 
    } 
    int[][] temp = new int[row - 1][col - 1]; 

    for (int i = 1; i < row; i++) { 
     for (int j = 1; j < col; j++) { 
      temp[i - 1][j - 1] = aa[i][j]; 
     } 
    } 
    return temp; 
} 

static void printArray(int aa[][]) { 
    int row = aa.length; 
    int col = aa[0].length; 
    for (int i = 0; i < row; i++) { 
     for (int j = 0; j < col; j++) { 
      System.out.print(aa[i][j] + " "); 
     } 
     System.out.println(); 
    } 
}