原文:

给定一个整数 n ,任务是打印将 n 件放入 nxn 板的所有独特组合

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

示例:

输入: n = 2 输出: * * –

– –

––

–– * 说明:空格总数为 22=4,要设置的棋子为 2,所以有 4c2 组合((4!/(2!*2!))=6)可能,如上所述。

输入:n = 1 t3】输出:*

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

  1. 创建一个名为 allcombinations 的函数,它将生成所有可能的解。
  2. 它将取一个整数pieced表示放置的总件数,整数 n 表示需要放置的件数,两个整数 rowcol 表示当前件将要放置的行和列,以及一个字符串 ans 作为参数,用于存储放置件的矩阵。
  3. 现在,对所有组合的初始调用将通过 0 作为分段n00 作为,以及一个空字符串作为
  4. 在每次通话中,检查基本情况,即:
    • 如果行变成 n 并且所有的片都被放置,即片被放置=n 。然后打印 ans 返回。否则如果不是 n ,那么就从这个调用返回。
  5. 现在打两个电话:
    • 一个是在当前位置加一个' ,一个是离开该位置加 '-'* 。
  6. 之后,递归调用将打印所有可能的pg电子试玩链接的解决方案。

下面是上述方法的实现。

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

python 3

# 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.

java 描述语言

// 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

output: 

*    *
-    -
*    -
*    -
*    -
-    *
-    *
*    -
-    *
-    *
-    -
*    *

时间复杂度: o(2^m),其中 m = n * n t3】辅助空间:t5】