gpt4 book ai didi

java - N * N 皇后算法获取坐标

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:44 35 4
gpt4 key购买 nike

我正在尝试实现 N*N queen 算法,稍微改变一下。在这个版本中,女王也可以像骑士一样移动......

一切正常,但我正在尝试获取所有可能解决方案的坐标。问题是,如果我将它放在 col == n 中,它只会打印最后一个。关于如何解决这个问题的任何想法?

  static void placement(int col, int queens[], int n){
//int solution =0;
for (int row = 1; row <= n; row++) {
queens[col] = row;
if((check_Queen(row,col,queens)) == true)
{
if((check_KnightMove(row,col,queens)) == true)
{
if(col == n)
{
System.out.println("("+row + "," + col);
System.out.println("solution=" + solution);
solution++;
}
else
{
placement(col+1,queens,n);
}
}
}
}
queens[col] = 0;
}

public static void main(String[] args) {
int solution =0;
Scanner scanner=new Scanner(System.in);
System.out.print("Please enter N");
int n = scanner.nextInt();// TODO Auto-generated method stub
int queens[] = new int[n+1];
placement(1,queens,n);
System.out.println("nQueens: solution=" + solution);
}
static boolean check_Queen(int row, int col, int queens[])
{

//boolean flag = false;
for(int i =1; i<col; i++)
{
if (queens[col-i] == row ||
queens[col-i] == row-i ||
queens[col-i] == row+i) {
//flag = false;
return false;
}

}
return true;


}
static boolean check_KnightMove(int row, int col, int queens[])
{
if(col>=2&&(queens[col-2] == (row -1) || queens[col-2] == (row+1) || queens[col-1] == (row-2) || queens[col-1] == (row+2)))
{
return false;
}
return true;

}


}

最佳答案

如果不知道 check_Queencheck_KnightMove 是如何定义的,就很难说出您的解决方案有什么问题。以下是我将如何解决这个任务:

public class Queens
{
static void printSolution (int [] queens)
{
int l = queens.length;
for (int i = 0; i < l; i++)
{
for (int j = 0; j < l; j++)
{
System.out.print (queens [i] == j ? 'Q' : '.');
System.out.print (' ');
}
System.out.println ();
}
System.out.println ();
}

static int placement (int [] queens, int c)
{
if (c == queens.length)
{
printSolution (queens);
return 1;
}
else
{
int solutionCount = 0;
int l = queens.length;

for (int r = 0; r < l; r++)
{
boolean flag = false;
for (int i = 0; i < c; i++)
{
int xd = c - i;
int yd = Math.abs (r - queens [i]);

if (yd == 0 || xd == yd)
{
flag = true;
break;
}

// Knight move support
if ((xd == 1 && yd == 2) || (xd == 2 && yd == 1))
{
flag = true;
break;
}
}

if (!flag)
{
queens [c] = r;
solutionCount += placement (queens, c + 1);
}
}

return solutionCount;
}
}

public static void main (String [] args)
{
System.out.println (
"Total solutions found: " + placement (new int [11], 0));
}
}

在您的解决方案中,方法 check_KnightMove 不正确。当 col == 2 它不允许 Queen 放在第 1 行,因为它相信 (col: 0, row: 0) 总是有一个 Queen ,即在板外。这是更正后的版本:

static boolean check_KnightMove (int row, int col, int queens[])
{
if (col >= 3
&& (queens [col - 2] == (row - 1) || queens [col - 2] == (row + 1)))
{
return false;
}

if (col >= 2
&& (queens [col - 1] == (row - 2) || queens [col - 1] == (row + 2)))
{
return false;
}
return true;
}

关于java - N * N 皇后算法获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888055/

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