📜  在 NxN 板上打印设置 N 件的所有独特组合

📅  最后修改于: 2022-05-13 01:56:05.388000             🧑  作者: Mango

在 NxN 板上打印设置 N 件的所有独特组合

给定一个整数N ,任务是打印将N块放在NxN板上的所有唯一组合

注意:打印(“*”)表示碎片,(“-”)表示空白。

例子:

方法:这个问题可以通过使用递归生成所有可能的解决方案来解决。现在,请按照以下步骤解决此问题:

  1. 创建一个名为allCombinations的函数,它将生成所有可能的解决方案。
  2. 它将采用一个整数piecesPlaced表示放置的总件数,整数N表示需要放置的件数,两个整数rowcol表示将要放置当前件的行和列,以及一个字符串ans for存储放置片段的矩阵作为参数。
  3. 现在,对allCombinations的初始调用将传递0作为piecesPlacedN00作为rowcol以及一个空字符串作为ans
  4. 在每次调用中,检查基本情况,即:
    • 如果 row 变为N并且所有棋子都已放置,即piecesPlaced=N 。然后打印答案并返回。否则,如果piecesPlaced不是N ,则只需从此调用返回。
  5. 现在打两个电话:
    • 一种是在当前位置添加一个'*' ,另一种是离开该位置并添加'-'
  6. 在此之后,递归调用将打印所有可能的解决方案。

下面是上述方法的实现。

C++
// C++ Program for the above approach
#include 
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 + "*
";     y = ans + "-
";   }     // 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 let N = 2; allCombinations(0, N, 0, 0, "");   // This code is contributed by Saurabh Jaiswal



输出:
*    *
-    -

*    -
*    -

*    -
-    *

-    *
*    -

-    *
-    *

-    -
*    *

时间复杂度: O(2^M),其中 M=N*N
辅助空间: O(1)