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 );
}
}
}
}