C# 对于已经放置了k个皇后的给定棋盘,找到N个皇后问题的解决方案
这是典型的8皇后问题,但在这种情况下,给出了一个nxn板,其中已经放置了一定数量的皇后。你必须填补董事会的剩余部分 样本输入:C# 对于已经放置了k个皇后的给定棋盘,找到N个皇后问题的解决方案,c#,algorithm,C#,Algorithm,这是典型的8皇后问题,但在这种情况下,给出了一个nxn板,其中已经放置了一定数量的皇后。你必须填补董事会的剩余部分 样本输入: .Q.. ...Q .... .... 预期产量 .Q.. ...Q Q... ..Q. 下面的代码是基于回溯的,只有当电路板是空的(即没有任何皇后放置在上面)时才有效。我如何修改它,使其放置未放置的皇后,使它们不会相互攻击 using System; using System.IO; using System.Linq; using System.Collecti
.Q..
...Q
....
....
预期产量
.Q..
...Q
Q...
..Q.
下面的代码是基于回溯的,只有当电路板是空的(即没有任何皇后放置在上面)时才有效。我如何修改它,使其放置未放置的皇后,使它们不会相互攻击
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace CSharp_Shell
{
public static class Program
{
static void printBoard(string [, ] board) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Console.Write(board[i, j] + " ");
}
Console.Write("\n");
}
}
static bool isSafe(string [,] board, int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row, i] == "Q")
return false;
/* Check upper diagonal on left side */
for (i=row, j=col; i>=0 && j>=0; i--, j--)
if (board[i, j] == "Q")
return false;
/* Check lower diagonal on left side */
for (i=row, j=col; j>=0 && i<4; i++, j--)
if (board[i, j] == "Q")
return false;
return true;
}
static bool solve8queen(string[,] board, int col)
{
//base case
if(col >= 4)
return true;
//loop over rows
for(int row = 0; row < 4; row++)
{
//check if queen can be placed
if(isSafe(board, row, col))
{
//place the queen
board[row, col] = "Q";
//explore next solution
if(solve8queen(board, col + 1))
return true;
//Backtrack
board[row, col] = ".";
}
}
return false;
}
public static void Main()
{
string [,] board = {
{".", "Q", ".", "."},
{".", ".", ".", "Q"},
{".", ".", ".", "."},
{".", ".", ".", "."}
};
solve8queen(board, 0);
printBoard(board);
}
}
}
我相信你可以跳过那些已经占据女王宝座的专栏。您还需要修改isSafe函数以查看整个视图,而不仅仅是放在后面的视图 因此:
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace CSharp_Shell
{
public static class Program
{
static void printBoard(string [, ] board) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Console.Write(board[i, j] + " ");
}
Console.Write("\n");
}
}
static bool isSafe(string [,] board, int row, int col)
{
int i, j;
/* Check this row completely */
for (i = 0; i < 4; i++)
if (board[row, i] == "Q")
return false;
/* Check upper diagonal on left side */
for (i=row, j=col; i>=0 && j>=0; i--, j--)
if (board[i, j] == "Q")
return false;
/* Check bottom diagonal on right side */
for (i=row, j=col; i<4 && j<4; i++, j++)
if (board[i, j] == "Q")
return false;
/* Check lower diagonal on left side */
for (i=row, j=col; j>=0 && i<4; i++, j--)
if (board[i, j] == "Q")
return false;
/* Check upper diagonal on right side */
for (i=row, j=col; j<4 && i>=0; i--, j++)
if (board[i, j] == "Q")
return false;
return true;
}
static bool solve8queen(string[,] board, int col)
{
//base case
if(col >= 4) {
printBoard(board);
return true;
}
bool res = false;
//loop over rows
for(int row = 0; row < 4; row++)
{
//check if queen can be placed
if(isSafe(board, row, col))
{
//place the queen
board[row, col] = "Q";
// Skip evaluating column 1
if(col + 1 == 1)
col++;
// Skip evaluating column 3
if(col + 1 == 3)
col++;
//explore next solution
res = solve8queen(board, col + 1) || res;
//Backtrack
board[row, col] = ".";
}
}
return false;
}
public static void Main()
{
string [,] board = {
{".", "Q", ".", "."},
{".", ".", ".", "Q"},
{".", ".", ".", "."},
{".", ".", ".", "."}
};
solve8queen(board, 0);
//printBoard(board);
}
}
}
另一种可能的方法是修改N queen问题以打印出所有可能的移动。并从列表中选择适当的输出。如果没有匹配项,则解决方案不存在。提示:使用q而不是q作为皇后。在ISSAFE中考虑Q和Q都是皇后。
. Q . .
. . . Q
Q . . .
. . Q .