Class Demo2_NQueens


  • public class Demo2_NQueens
    extends Object
    Demo showing difference of solutions to NQueens problem using mutable and immutable data structures.
    Author:
    Herbert Praehofer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int N  
    • Constructor Summary

      Constructors 
      Constructor Description
      Demo2_NQueens()  
    • Constructor Detail

      • Demo2_NQueens

        public Demo2_NQueens()
    • Method Detail

      • main

        public static void main​(String[] args)
      • solveNQueens

        static boolean solveNQueens​(boolean[][] board,
                                    int col)
        Solves N-Queens problem with mutable array.
        Parameters:
        board - mutable array representing the board
        col - next column
        Returns:
        true if successful
      • solveNQueens

        static Optional<FList<Pos>> solveNQueens​(FList<Pos> board,
                                                 int col)
        Solves N-Queens problem using immutable lists
        Parameters:
        board - the list of positions representing current board
        col - next column
        Returns:
        optional with list of positions representing solution with empty optional if no solution found
      • isSafe

        private static boolean isSafe​(boolean[][] board,
                                      int row,
                                      int col)
        Checks if position is safe for setting queen.
        Parameters:
        board - the mutable array representing the board
        row - the row index
        col - the column index
        Returns:
        true if position is safe
      • isSafe

        private static boolean isSafe​(FList<Pos> board,
                                      int row,
                                      int col)
        Checks if position is safe for setting queen.
        Parameters:
        board - the list of position currently set
        row - the row index
        col - the column index
        Returns:
        true if position is safe
      • inDiag

        private static boolean inDiag​(int i,
                                      int j,
                                      int row,
                                      int col)
        Checks if positions i/ j and row/col are in one diagonal.
        Parameters:
        i - the row coordinate of the first position
        j - the column coordinate of the first position
        row - the row coordinate of the second position
        col - the column coordinate of the second position
        Returns:
        true if the positions are in one diagonal
      • print

        private static void print​(boolean[][] board)
        Prints the board.
        Parameters:
        board - the board in form of a mutable array
      • print

        private static void print​(FList<Pos> board)
        Prints the board.
        Parameters:
        board - the board in form of a immutable list