Print all unique combinations of setting N pieces on an NxN board

Last Updated : 6 Dec, 2021

Given an integer N, the task is to print all the unique combinations of putting N pieces in an NxN board.

Note: Print ("*") for pieces and ("-") for an empty space.

Example:

Input: N = 2
Output:
* *
- -

* -
* -

* -
- *

- *
* -

- *
- *

- -
* *
Explanation: The total number of empty spaces are 2*2=4 and the pieces to be set is 2 so there are 4C2 combinations ((4!/(2!*2!))=6) possible which is represented above.

Input: N = 1
Output: *

 

Approach: This problem can be solved by using recursion to generate all possible solutions. Now, follow the steps below to solve this problem:

  1. Create a function named allCombinations, which will generate all possible solutions.
  2. It will take an integer piecesPlaced denoting the number of total pieces placed, integer N denoting the number of pieces needed to be placed, two integers row and col denoting the row and column where the current piece is going to be placed and a string ans for storing the matrix where pieces are placed, as arguments.
  3. Now, the initial call to allCombinations will pass 0 as piecesPlaced, N, 0 and 0 as row and col and an empty string as ans.
  4. In each call, check for the base case, that is:
    • If row becomes N and all pieces are placed, i.e. piecesPlaced=N. Then print the ans and return. Else if piecesPlaced is not N, then just return from this call.
  5. Now make two calls:
    • One to add a '*' at the current position, and one to leave that position and add '-'.
  6. After this, the recursive calls will print all the possible solutions.

Below is the implementation of the above approach.

C++
// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to print all
// combinations of setting N
// pieces in N x N board
void allCombinations(int piecesPlaced, int N, int row,
                     int col, string ans)
{
  
    // If the total 2d array's space
    // is exhausted then backtrack.
    if (row == N) {

        // If all the pieces are
        // placed then print the answer.
        if (piecesPlaced == N) {
            cout << ans;
        }
        return;
    }
    int nr = 0;
    int nc = 0;

    // Declare one string
    // that will set the piece.
    string x = "";

    // Declare one string that
    // will leave the space blank.
    string y = "";

    // If the current column
    // is out of bounds then
    // increase the row
    // and set col to 0.
    if (col == N - 1) {
        nr = row + 1;
        nc = 0;
        x = ans + "*\n";
        y = ans + "-\n";
    }

    // Else increase the col
    else {
        nr = row;
        nc = col + 1;
        x = ans + "*\t";
        y = ans + "-\t";
    }
  
    // Set the piece in the
    // box and move ahead
    allCombinations(piecesPlaced + 1, N, nr, nc, x);

    // Leave the space blank
    // and move forward
    allCombinations(piecesPlaced, N, nr, nc, y);
}

// Driver Code
int main()
{
    int N = 2;
    allCombinations(0, N, 0, 0, "");
    return 0;
}

    // This code is contributed by rakeshsahni.
Java
// Java Program for the above approach

import java.io.*;
import java.util.*;

public class main {

    // Function to print all
    // combinations of setting N
    // pieces in N x N board
    public static void allCombinations(
        int piecesPlaced,
        int N, int row,
        int col, String ans)
    {
        // If the total 2d array's space
        // is exhausted then backtrack.
        if (row == N) {

            // If all the pieces are
            // placed then print the answer.
            if (piecesPlaced == N) {
                System.out.println(ans);
            }
            return;
        }
        int nr = 0;
        int nc = 0;

        // Declare one string
        // that will set the piece.
        String x = "";

        // Declare one string that
        // will leave the space blank.
        String y = "";

        // If the current column
        // is out of bounds then
        // increase the row
        // and set col to 0.
        if (col == N - 1) {
            nr = row + 1;
            nc = 0;
            x = ans + "*\n";
            y = ans + "-\n";
        }

        // Else increase the col
        else {
            nr = row;
            nc = col + 1;
            x = ans + "*\t";
            y = ans + "-\t";
        }
        // Set the piece in the
        // box and move ahead
        allCombinations(
            piecesPlaced + 1, N,
            nr, nc, x);

        // Leave the space blank
        // and move forward
        allCombinations(piecesPlaced, N,
                        nr, nc, y);
    }

    // Driver Code
    public static void main(String[] args)
        throws Exception
    {
        int N = 2;

        allCombinations(0, N, 0, 0, "");
    }
}
Python3
# Python Program for the above approach

# Function to print all
# combinations of setting N
# pieces in N x N board
def allCombinations(piecesPlaced, N, row, col, ans):
  
    # If the total 2d array's space
    # is exhausted then backtrack.
    if row == N:
      
        # If all the pieces are
        # placed then print the answer.
        if piecesPlaced == N:
            print(ans)
        return;
    
    nr = 0
    nc = 0
    
    # Declare one string
    # that will set the piece.
    x = ""
    
    # Declare one string that
    # will leave the space blank.
    y = ""
    
    # If the current column
    # is out of bounds then
    # increase the row
    # and set col to 0.
    
    if col == N - 1:
        nr = row + 1
        nc = 0
        x = ans + "*\n"
        y = ans + "-\n"
        
    # Else increase the col
    else:
        nr = row
        nc = col + 1
        x = ans + "*    "
        y = ans + "-    "
    
    # Set the piece in the
    # box and move ahead
    allCombinations(piecesPlaced + 1, N, nr, nc, x);
    
    # Leave the space blank
    # and move forward
    allCombinations(piecesPlaced, N, nr, nc, y);

# Driver Code
N = 2
allCombinations(0, N, 0, 0, "")

# This code is contributed by rdtank.
C#
// C# Program for the above approach
using System;
public class main {

    // Function to print all
    // combinations of setting N
    // pieces in N x N board
    public static void allCombinations(int piecesPlaced,
                                       int N, int row,
                                       int col, String ans)
    {
        // If the total 2d array's space
        // is exhausted then backtrack.
        if (row == N) {

            // If all the pieces are
            // placed then print the answer.
            if (piecesPlaced == N) {
                Console.WriteLine(ans);
            }
            return;
        }
        int nr = 0;
        int nc = 0;

        // Declare one string
        // that will set the piece.
        String x = "";

        // Declare one string that
        // will leave the space blank.
        String y = "";

        // If the current column
        // is out of bounds then
        // increase the row
        // and set col to 0.
        if (col == N - 1) {
            nr = row + 1;
            nc = 0;
            x = ans + "*\n";
            y = ans + "-\n";
        }

        // Else increase the col
        else {
            nr = row;
            nc = col + 1;
            x = ans + "*\t";
            y = ans + "-\t";
        }
      
        // Set the piece in the
        // box and move ahead
        allCombinations(piecesPlaced + 1, N, nr, nc, x);

        // Leave the space blank
        // and move forward
        allCombinations(piecesPlaced, N, nr, nc, y);
    }

    // Driver Code
    public static void Main(string[] args)

    {
        int N = 2;

        allCombinations(0, N, 0, 0, "");
    }
}

// This code is contributed by ukasp.
JavaScript
// Javascript Program for the above approach

// Function to print all
// combinations of setting N
// pieces in N x N board
function allCombinations(piecesPlaced, N, row, col, ans) {

  // If the total 2d array's space
  // is exhausted then backtrack.
  if (row == N) {

    // If all the pieces are
    // placed then print the answer.
    if (piecesPlaced == N) {
      document.write(ans);
    }
    return;
  }
  let nr = 0;
  let nc = 0;

  // Declare one string
  // that will set the piece.
  let x = "";

  // Declare one string that
  // will leave the space blank.
  let y = "";

  // If the current column
  // is out of bounds then
  // increase the row
  // and set col to 0.
  if (col == N - 1) {
    nr = row + 1;
    nc = 0;
    x = ans + "*<br>";
    y = ans + "-<br>";
  }

  // Else increase the col
  else {
    nr = row;
    nc = col + 1;
    x = ans + "*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    y = ans + "-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  }

  // Set the piece in the
  // box and move ahead
  allCombinations(piecesPlaced + 1, N, nr, nc, x);

  // Leave the space blank
  // and move forward
  allCombinations(piecesPlaced, N, nr, nc, y);
}

// Driver Code
let N = 2;
allCombinations(0, N, 0, 0, "");

// This code is contributed by Saurabh Jaiswal

 
 


Output: 
*    *
-    -

*    -
*    -

*    -
-    *

-    *
*    -

-    *
-    *

-    -
*    *

 


 

Time Complexity: O(2^M), where M=N*N
Auxiliary Space: O(1)


 

Comment