Jump to content

Need Help With N Queens Program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Codebug

Codebug

    Newbie

  • Members
  • Pip
  • 7 posts
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

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Do the Ys count as 1 diagonal, or 2?
  .  .  Y  .
  .  .  .  Y
  Y  .  .  .
  .  Y  .  .