好的,所以我的问题基本上是,例如,我有一个矩阵
010
101
111
只是随机的 1 和 0。所以我有数组rowcountand colcount,它计算每行和每列中的个数。所以rowcount对于 this is{1,2,3}和colcountis {2,2,2}。现在在另一种方法中,我得到了数组rowcountand colcount,在该方法中,我应该创建一个计数在rowcountandcolcount中的矩阵,但最终矩阵可以不同。比原来的。我想我应该用尽所有排列,直到矩阵起作用。基本情况必须保持不变。
注意:不能使用 Math.random。
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; 
  }
public static void count(int[][] matrix, int[] rowcount, int[] colcount)
{
  for(int x=0;x<matrix.length;x++)
     for(int y=0;y<matrix[0].length;y++)
     {
        if(matrix[x][y]==1)
        {
           rowcount[x]++;
           colcount[y]++;
        }
     }
  }