0

わかりましたので、私の問題は基本的にです。たとえば、マトリックスがあります

010
101
111

ランダムな 1 と 0 だけです。したがって、各行と列の 1 の数をカウントするrowcountとの配列があります。colcountしたがってrowcount、これは{1,2,3}であり、colcountです{2,2,2}。別の方法では、配列rowcountとが与えられます。その方法では、とcolcountのカウントを含む行列を作成することになっていますが、最終的な行列は異なる場合があります。オリジナルより。マトリックスが機能するまで、すべての順列を使い果たすことになっていると思います。基本ケースは同じままである必要があります。rowcountcolcount

注: 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]++;

        }

     }

  }
4

2 に答える 2