需要 N 皇后计划的帮助(检查对角线)
我正在开发一个 N Queens 程序,该程序将允许用户以字符串形式输入 Queen 配置。例如, 当出现提示时,用户可能会输入类似 Q....Q.....Q..Q 的内容。当显示为板时,它看起来像:
Q . . .
. Q . .
. . . Q
. . Q .
Is not a solution!
该程序很简单,因为它假设用户将输入有效信息。我希望在返回并添加错误处理之前让程序的主要部分正常工作。
对于那些不熟悉 N 个皇后拼图的人来说,基本上 N x N 板上有 N 个皇后。每排有一张皇后。如果没有两个皇后共享相同的行、列或对角线,则填充棋盘是一种解决方案。
我已经成功地实现了对行和列的检查。然而,我对如何检查所有对角线感到困惑。我知道如何检查两条主对角线,就像井字棋一样,但我真的无法想象如何检查所有可能的对角线?
有人可以提供帮助吗?
这是我的代码:
import java.util.Scanner;
public class NQueens {
public static void main(String[] args) {
Scanner sc = new Scanner( System.in );
int qCount;
boolean solution = true;
System.out.println( "Enter the String to test:" );
board = sc.nextLine();
int boardLen = board.length();
int maxDim = (int) Math.sqrt(boardLen);
char[][] gameBoard = new char[maxDim][maxDim];
int counter = 0;
for ( int i = 0; i < maxDim; i++ )
{
for ( int j = 0; j < maxDim; j++ )
{
gameBoard[ i ][ j ] = board.charAt( counter );
counter++;
}
}
System.out.println("");
System.out.println("");
//check rows
for ( int i = 0; i < maxDim; i++ )
{
int queenCount = 0;
for ( int j = 0; j < maxDim; j++ )
{
if ( gameBoard[ i ][ j ] == 'Q' )
{
queenCount++;
if ( queenCount > 1 )
{
solution = false;
break;
}
}
}
}
// check columns
for ( int i = 0; i < maxDim; i++ )
{
int queenCount = 0;
for ( int j = 0; j < maxDim; j++ )
{
if ( gameBoard[ j ][ i ] == 'Q' )
{
queenCount++;
if ( queenCount > 1 )
{
solution = false;
break;
}
}
}
}
// print the board
for( int i = 0; i < maxDim; i++ )
{
for ( int j = 0; j < maxDim; j++ )
{
System.out.print( gameBoard[ i ][ j ] + " " );
}
System.out.println();
}
// print whether or not the placement of queens is a solution
if ( solution )
{
System.out.println( "Is a solution!" );
}
else
{
System.out.println( "Is not a solution!" );
}
}//end main
}//end class
谢谢 阅读更多:需要 N Queens 计划的帮助
I'm working on an N Queens program that will allow the user to enter a Queen configuration as a String. For example,
when prompted, the user might enter something like Q....Q.....Q..Q. which when displayed as a board would look like:
Q . . .
. Q . .
. . . Q
. . Q .
Is not a solution!
This program is simple in that it assumes that the user will enter valid information. I would like to get the main part of the program working before I go back and add error handling.
For those not familiar with the N Queens puzzle, basically you have N Queens on an N x N board. You have one Queen per row. A populated board is a solution if no two Queens share the same row, column or diagonal.
I have successfully implemented checks for the rows and columns. However, I am stumped as to how I can check all the diagonals. I know how to check the two main diagonals, like in tic tac toe, but I really can't visualize how I can check all possible diagonals?
Can anyone offer help?
Here is my code:
import java.util.Scanner;
public class NQueens {
public static void main(String[] args) {
Scanner sc = new Scanner( System.in );
int qCount;
boolean solution = true;
System.out.println( "Enter the String to test:" );
board = sc.nextLine();
int boardLen = board.length();
int maxDim = (int) Math.sqrt(boardLen);
char[][] gameBoard = new char[maxDim][maxDim];
int counter = 0;
for ( int i = 0; i < maxDim; i++ )
{
for ( int j = 0; j < maxDim; j++ )
{
gameBoard[ i ][ j ] = board.charAt( counter );
counter++;
}
}
System.out.println("");
System.out.println("");
//check rows
for ( int i = 0; i < maxDim; i++ )
{
int queenCount = 0;
for ( int j = 0; j < maxDim; j++ )
{
if ( gameBoard[ i ][ j ] == 'Q' )
{
queenCount++;
if ( queenCount > 1 )
{
solution = false;
break;
}
}
}
}
// check columns
for ( int i = 0; i < maxDim; i++ )
{
int queenCount = 0;
for ( int j = 0; j < maxDim; j++ )
{
if ( gameBoard[ j ][ i ] == 'Q' )
{
queenCount++;
if ( queenCount > 1 )
{
solution = false;
break;
}
}
}
}
// print the board
for( int i = 0; i < maxDim; i++ )
{
for ( int j = 0; j < maxDim; j++ )
{
System.out.print( gameBoard[ i ][ j ] + " " );
}
System.out.println();
}
// print whether or not the placement of queens is a solution
if ( solution )
{
System.out.println( "Is a solution!" );
}
else
{
System.out.println( "Is not a solution!" );
}
}//end main
}//end class
Thanks
Read more: Need Help With N Queens Program
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不想检查所有对角线,但您可以检查所有皇后。您可以通过检查两个 Q 的行和列之间的差异来检查两个皇后是否在同一条对角线上。如果差异相同,则它们位于同一条对角线上。 (基本上,如果两个皇后之间的直线的斜率为 +-1,则它们位于同一条对角线上。)
例如,假设您有两个皇后 Q1 和 Q2。计算它们的行和列差异的绝对值。
如果 deltaRow == deltaCol ,则皇后位于同一条对角线上。
对所有 N 个皇后都这样做。
I don't think you want to check all the diagonals, but you can check all the queens instead. You can check to see if two queens are on the same diagonal by checking the differences between the rows and columns of the two Qs. If the differences are the same, they're on the same diagonal. (Basically, if the slope of the line between the two queens is +-1, they're on the same diagonal.)
For example, say you have two queens Q1 and Q2. Calculate the absolute value of the differences in their rows and columns.
If
deltaRow == deltaCol
, the queens are on the same diagonal.Just do that for all N queens.
如果连接两个皇后的线的斜率为 1 或 -1,我们可以说皇后位于同一条对角线上。
令
q1
和q2
为保存每个皇后的 x 和 y 位置的数据结构:所以
if (Math.abs(slope) == 1)
那么它们在同一条对角线上。We can say that the queens are on the same diagonal if the slope of the line connecting the 2 queens is either 1 or -1.
Let
q1
andq2
be data structures holding the x and y positions of each queen:So
if (Math.abs(slope) == 1)
then they are on the same diagonal.对角线可以用 y = x + c 或 y = c - x 的形式表示。您的 4x4 棋盘总共有 10 条对角线,每个方程 5 条。找出 c(它们根据电路板尺寸而变化)并循环 x,计算 y。
您必须检查小于零的坐标,只需根据需要将棋盘放大 2 倍(在每个方向上)即可避免上限 - 其他方块始终为空,因此检查它们不会造成任何伤害。
我什至在没有任何棋盘的情况下实现了 N 皇后问题——跟踪行、列和对角线。当时这更快,因为加法比乘法快得多,但现在情况不再如此。
Diagonals can be expressed in the form of y = x + c or y = c - x. Your 4x4 board will have a total of 10 diagonals, 5 for each equation. Figure out the c's (they vary based on the board size) and loop on x, calculate the y's.
You'll have to check for coordinates less than zero, the upper bounds can be avoided by simply making the board 2x as big (in each direction) as need be--the other squares are always empty so checking them causes no harm.
I have even implemented the N queens problem with no board at all--track the rows, columns and diagonals. At the time this was faster because addition was a lot faster than multiplication but that's no longer the case.