2013-06-26 43 views
0

該程序的第一部分是隨機產生一個尺寸在2到6之間的矩陣。然後我必須隨機地用1和0填充這個矩陣。使用這個矩陣,我做了2個一維數組,其中每行和每列包含1的數目。表示行號的矩陣索引,以及表示計數的單元格中的數字。我做了這些數組中的2個:一個用於行數和一個用於列數。 這是我的代碼。如何重新創建一個(0-1)矩陣,每行和每列有1個數?

public static void count(int[][] matrix, int[] rowcount, int[] colcount) 
    { 
    for(int x = 0; x < rowcount.length; x++) 
     for(int y = 0; y < colcount.length; y++) 
     { 
      if (matrix[x][y] == 1) 
      { 
       rowcount[x] = rowcount[x] + 1; 
       colcount[y] = colcount[y] + 1; 
      } 
     } 
    } 

現在我面臨的問題是重新使用這些計數矩陣。 通過重新創建,我的意思是創建另一個滿足一維數組計數的矩陣,不需要生成這些計數從中導出的確切矩陣。 這是我迄今爲止的代碼,我一直在爲這個程序工作2天,我找不到一個算法來爲所有情況生成一個矩陣。

下面是該

public static void re_create(int[] rowcount, int[] colcount) 
    { 
    int[][] recreated = new int[rowcount.length][colcount.length]; 
    recur(recreated, rowcount, colcount, 0, 0); 
    } 
    private static void recur(int[][] m, int[] rowcount, int[] colcount, int r, int c) //recursive helper method 
    { 
    if(compare(m, rowcount, colcount)) //base case: if new matrix works 
    { 
     System.out.println(); 
     System.out.println("RECREATED"); 
     display(m, rowcount, colcount); //we're done! 
     System.exit(0); 
    } 
    else 
    { 
     int[] temp_r = new int[m.length]; 
     int[] temp_c = new int[m[0].length]; 
     count(m, temp_r, temp_c); 
     if(rowcount[r] > temp_r[r] && colcount[c] > temp_c[c]) 
      m[r][c] = 1; 
     if(r+1 < m.length) 
      recur(m,rowcount,colcount,r+1,c); 
     if(rowcount[r] < temp_r[r] || colcount[c] < temp_c[c]) 
      m[r][c] = 0; 
     if(c+1 < m[0].length) 
      recur(m,rowcount,colcount,r,c+1);  
    } 
    } 
    private static boolean compare(int[][] m, int[] rowcount, int[] colcount) 
    { 
    int[] temp_r = new int[m.length]; 
    int[] temp_c = new int[m[0].length]; 
    count(m, temp_r, temp_c); 

    for (int x = 0; x < temp_r.length; x++) 
    { 
     if(temp_r[x] != rowcount[x]) 
      return false; 
    } 

    for (int y = 0; y < temp_c.length; y++) 
    { 
     if(temp_c[y] != colcount[y]) 
      return false; 
    } 

    return true; 
    } 

該方案是從學校的方法,所以我已經給出的方法頭和基本情況的遞歸方法,因此那些必須保持不變。其他的一切都是我寫的。我無法找到一個好的算法來生成這些矩陣。我認爲我應該在矩陣中生成1和0的每個置換,直到匹配基本情況,但我不明白這是如何在repeat方法中給出參數的情況下工作的。

回答

0

沒有適用於所有情況的解決方案。考慮:

0 1 0 0 | 1 0 0 1 0 | 1 
1 0 1 0 | 2 0 1 0 1 | 2 
0 1 0 1 | 2 1 0 1 0 | 2 
0 0 1 0 | 1 0 1 0 0 | 1 
-------  ------- 
1 2 2 1  1 2 2 1 
+0

嗯,實際上這兩個解決方案都可以工作,所以這些解決方案都可以被程序接受。正如我上面所說的,程序工作是重新創建任何矩陣的計數。我的解決方案的意思是,有時程序結束時沒有找到基本情況。它只是打印原始矩陣而沒有一個解決方案。程序結束時沒有達到基本情況 – user2524624

0

這是我想出的解決方案。看起來對我來說是正確的,但我沒有給它提供很多測試數據。

public static void re_create(int[] rowcount, int[] colcount) { 
    int[][] recreated = new int[rowcount.length][colcount.length]; 
    for (int y = 0; y < rowcount.length; y++) { 
     for (int x = 0; x < colcount.length; x++) { 
      if (rowcount[y] > 0 && colcount[x] > 0) { 
       recreated[x][y] = 1; 
       rowcount[y]--; 
       colcount[x]--; 
      } else { 
       recreated[x][y] = 0; 
      } 
     } 
     if (rowcount[y] != 0) { 
      throw new IllegalArgumentException("Impossible! y = " + y); 
     } 
    } 
    for (int x = 0; x < colcount.length; x++) { 
     if (colcount[x] != 0) { 
      throw new IllegalArgumentException("Impossible! x = " + x); 
     } 
    } 
    System.out.println(Arrays.deepToString(recreated)); 
} 
+0

這很好^,但是我需要使用recur方法來解決這個問題。 – user2524624

+0

重建方法僅用於創建空矩陣,調用重複方法並傳遞計數。 – user2524624

+0

好吧,我錯過了重複方法的第一行已經給出並且必須被使用。我會再試一次。 – Lemming

相關問題