java - Matrix pattern generation -
hello
i working on java , have generate possible patterns(combinations) of m-by-n matrices such in same row there should not more single 1, same column may contain more single 1, taking example of 3*3 matrix, matrices generated should like:
1 0 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0
..... , on.
as have said progrom should flexible can generate such possible patterns value of m , n.
please me..
thanks!
here's 1 solution. ideone.com demo.
i bet can done half number of rows though.
public static set<int[][]> choosefrom(list<int[]> choices, int choose) { set<int[][]> results = new hashset<int[][]>(); if (choose == 1) { (int[] choice : choices) results.add(new int[][] { choice }); } else { (int[] choice : choices) { (int[][] submatrix : choosefrom(choices, choose - 1)) { int[][] matrix = new int[choose][]; matrix[0] = choice; (int r = 1; r < choose; r++) matrix[r] = submatrix[r-1]; results.add(matrix); } } } return results; } public static set<int[][]> matrices(int m, int n) { list<int[]> possiblerows = new arraylist<int[]>(); (int = 0; < n; i++) { int[] row = new int[n]; row[i] = 1; possiblerows.add(row); } return choosefrom(possiblerows, m); }
output 2x3 case:
[ 0 0 1 ] [ 0 1 0 ] [ 1 0 0 ] [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ] [ 1 0 0 ] [ 0 1 0 ] [ 1 0 0 ] [ 0 1 0 ] [ 0 1 0 ] [ 1 0 0 ] [ 1 0 0 ] [ 0 1 0 ] [ 0 0 1 ] [ 1 0 0 ] [ 0 1 0 ]
Comments
Post a Comment