gpt4 book ai didi

java - 我们有多少种方法可以将 k 车安全地放在 nxn 棋盘上

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:15 26 4
gpt4 key购买 nike

给定k白嘴鸦和一个n by n棋盘,车可以安全地放在棋盘上W不同的方式,在哪里

W = k!(n C k)^2
written differently W = n!n!/(k!(n-k)!(n-k)!)

问题陈述:

编写一个程序来运行 n by n棋盘和计算所有的方式 k车可以安全地放在棋盘上。

我的研究:

在网上搜索后,我终于找到了一个 nQueensSolution Geekviewpoint 上的代码我修改如下。但是,我的代码仅在 k = n 时有效.有谁知道如何为 k<n 解决这个问题吗? ?

这是我的代码:

static int kRooksPermutations(int[] Q, int col, int k, int kLimit) {
int count = 0;
for (int x = 0; x < Q.length && col < Q.length; x++)
if (safeToAdd(Q, x, col)) {
if (k == kLimit - 1) {
count++;
Q[col] = -1;
} else {
Q[col] = x;
count += kRooksPermutations(Q, col + 1, k + 1, kLimit);
}
}
return count;
}//

static boolean safeToAdd(int[] Q, int r, int c) {
for (int y = 0; y < c; y++)
if (Q[y] == r)
return false;
return true;
}//

这里是测试代码

public static void main(String... strings) {
kRooksPermutations(8,5);
}

最佳答案

知道了!

  // Empty
static final int MT = -1;

static int kRooksPermutations(int[] Q, int col, int rooksInHand) {
// Are we at the last col?
if (col >= Q.length) {
// If we've placed K rooks then its a good'n.
return rooksInHand == 0 ? 1 : 0;
}

// Count at this level starts at 0
int count = 0;
// Have we run out of rooks?
if (rooksInHand > 0) {
// No! Try putting one in each row in this column.
for (int row = 0; row < Q.length; row++) {
// Can a rook be placed here?
if (safeToAdd(Q, row, col)) {
// Mark this spot occupied.
Q[col] = row;
// Recurse to the next column with one less rook.
count += kRooksPermutations(Q, col + 1, rooksInHand - 1);
// No longer occupied.
Q[col] = MT;
}
}
}
// Also try NOT putting a rook in this column.
count += kRooksPermutations(Q, col + 1, rooksInHand);

return count;
}

static boolean safeToAdd(int[] Q, int row, int col) {
// Unoccupied!
if (Q[col] != MT) {
return false;
}
// Do any columns have a rook in this row?
// Could probably stop at col here rather than Q.length
for (int c = 0; c < Q.length; c++) {
if (Q[c] == row) {
// Yes!
return false;
}
}
// All clear.
return true;
}

// Main entry - Build the array and start it all going.
private static void kRooksPermutations(int N, int K) {
// One for each column of the board.
// Contains the row number in which a rook is placed or -1 (MT) if the column is empty.
final int[] Q = new int[N];
// Start all empty.
Arrays.fill(Q, MT);
// Start at column 0 with no rooks placed.
int perms = kRooksPermutations(Q, 0, K);
// Print it.
System.out.println("Perms for N = " + N + " K = " + K + " = " + perms);
}

public static void main(String[] args) {
kRooksPermutations(8, 1);
kRooksPermutations(8, 2);
kRooksPermutations(8, 3);
kRooksPermutations(8, 4);
kRooksPermutations(8, 5);
kRooksPermutations(8, 6);
kRooksPermutations(8, 7);
kRooksPermutations(8, 8);
}

打印:

Perms for N = 8 K = 1 = 64
Perms for N = 8 K = 2 = 1568
Perms for N = 8 K = 3 = 18816
Perms for N = 8 K = 4 = 117600
Perms for N = 8 K = 5 = 376320
Perms for N = 8 K = 6 = 564480
Perms for N = 8 K = 7 = 322560
Perms for N = 8 K = 8 = 40320

关于java - 我们有多少种方法可以将 k 车安全地放在 nxn 棋盘上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com