0

I need help with this exercise:
Give the pseudo-code that receives in input an integer n and prints the all possible matrixs nxn where the number of a is greater than or equals to the number of b , for example with n=2: ( the order isn't important ) output:
aa aa aa ab ab ba ba
ab ba aa aa ba aa ab
The algorithm's complexity would be O($n^2*S(n)$). Where S(n) is the number of matrix that should be printed.

Now i'm not very knowledgeable on technique algorithm of backtracking , because I'm studying it... so please if someone can help me with the exercise and with the backtracking .... otherwise I would never be pass this exam . Thank you guys ;)

exercise(char[][] M,int i,int j,int[] arrRB,int[] arrCB, int n , int nelem )
{
    if(nelem == n*n)
    {
        for (int r=0 ; r<n ; r++)
        {
            for(int c=0 ; c<n ;c++)
                System.out.print(M[r][c]);
            System.out.print("\n");
        }
        System.out.print("\n");
    }
    else
    {

        if(i<n && j<n)
        {
            int r=i,c=j;
            if (arrRB[i] <= n-(i+1) && arrCB[j] <= n-(j+1))
            {
                M[i][j] = 'b';
                arrRB[i]++;
                arrCB[j]++;
                exercise(M,i,j+1,arrRB ,arrCB ,n,nelem+1); 
                exercise(M,i+1,j,arrRB ,arrCB ,n,nelem+1 ); 
                arrRB[r]--;
                arrCB[c]--;
            }
            M[r][c] = 'a';
            exercise(M,r+1,c,arrRB ,arrCB ,n, nelem+1);
            exercise(M,r,c+1,arrRB ,arrCB ,n,nelem+1 ); 
        }
    }
}
}
thinker.92
  • 51
  • 8
  • I'm thinking that I've to put an 'b' in M[i,j], if in the row 'i' and the column 'j' the number of 'b' is less than remainder (n-(i+1)) and (n-(j+1)) . Where n is the size i,j index . I'm using two arrays for memorize the number of b for each row and column , so for examble arrayBrow[i] contains that in the row 'i'. But my problem is manage the index with recursion. – thinker.92 Dec 28 '14 at 22:35
  • You can actaully fill the matrix in the order row by row and column by column inside each row. Something like: put a or b to (i, j) cell and then fill (i, j + 1) or (i + 1, 0) cell(depending on wether the i row is over) recursively. – kraskevich Dec 28 '14 at 22:39
  • sorry , but how can i insert the code inside the comment?? because I want to show you my code. – thinker.92 Dec 28 '14 at 22:45
  • Why don't you add it to your question? – kraskevich Dec 28 '14 at 22:45
  • ok...I've added it ;) – thinker.92 Dec 28 '14 at 22:54

1 Answers1

0

It look like this(it is a pseudo code):

// Generates all matrices
void generate(char[][] matrix, int i, int j, int[] cntBRow, int[] cntBColumn) 
    if (i == n) // if the matrix is filled completely
        print(matrix) // just prints it
    else
        // Finds the next empty cell
        int nextI = i
        int nextJ = j + 1
        if (nextJ == n)
            nextI = i + 1
            nextJ = 0
        matrix[i][j] = 'a' // it is always possible to add 'a'
        generate(matrix, nextI, nextJ, cntBRow, cntBColumn)
        if ((cntBRow[i] + 1) * 2 <= matrix.length 
                && (cntBColumn[j] + 1) * 2 <= matrix.length) // if it is still possible to add 'b'
            cntBRow[i]++
            cntBColumn[j]++
            matrix[i][j] = 'b' // puts 'b' and continues generation
            generate(matrix, nextI, nextJ, cntBRow, cntBColumn)

 // Creates a new matrix and starts generation
 char[][] matrix = new char[n][n]
 generate(matrix, 0, 0, new int[n], new int[n])

The idea is to fix the order in which the cells are traversed when the matrix is filled recursively(in the code above the cells are ordered by a pair (row number, column number)).

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • ahhh okok I understand the idea ... just only thing, I don't understand the function canHaveEnoughA.... why you do 2 * curB <= n * n ?? – thinker.92 Dec 28 '14 at 23:10
  • @GabrieleSaturni To stop generation if the number of b is already to big(so that the number of b is greater than the number of a). – kraskevich Dec 28 '14 at 23:11
  • ok yes , first of all thank you so much . So I understand what's the meaning of canHaveEnoughA ... but I can't understand why it should be work , why you wrote 2*curB <= n*n ? I need to control the number of b for each column and row , not for all matrix . For example : ba ba isn't allowed – thinker.92 Dec 29 '14 at 13:34
  • I misread your question(it is sorta confusing that the question itself says that just the number of 'a' should be >= number of 'b', but the examples show that is should hold true for each row and column). – kraskevich Dec 29 '14 at 14:47
  • @GabrieleSaturni I have edited my answer according to this condition. – kraskevich Dec 29 '14 at 14:52
  • ok , thanky you so much , i traduced this in java code and it works , great!!! . I have just only one last question , why (numOfBinRow + 1) * 2 <= matrix.length works and (numOFBinrow +1) <= matrix.length - (rowindex+1) doesn't works? – thinker.92 Dec 29 '14 at 15:14
  • Why should it work? The number of 'b' in a row does not depend on a row index. – kraskevich Dec 29 '14 at 15:35
  • ok I understand the problem , I did a trace and I understand why is correct (numOfBinRow + 1) * 2 <= matrix.length . But I'm not satisfied and I tried that (numOfBinRow + 1) <= ( matrix.length - numOfBinRow ) and it works...however thank you , and happy new year :) . – thinker.92 Dec 29 '14 at 15:35