Package at.jku.ssw.fp.sect03_3
Class Demo2_NQueens
- java.lang.Object
-
- at.jku.ssw.fp.sect03_3.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()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static boolean
inDiag(int i, int j, int row, int col)
Checks if positions i/ j and row/col are in one diagonal.private static boolean
isSafe(boolean[][] board, int row, int col)
Checks if position is safe for setting queen.private static boolean
isSafe(FList<Pos> board, int row, int col)
Checks if position is safe for setting queen.static void
main(String[] args)
private static void
print(boolean[][] board)
Prints the board.private static void
print(FList<Pos> board)
Prints the board.(package private) static boolean
solveNQueens(boolean[][] board, int col)
Solves N-Queens problem with mutable array.(package private) static Optional<FList<Pos>>
solveNQueens(FList<Pos> board, int col)
Solves N-Queens problem using immutable lists
-
-
-
Field Detail
-
N
private static final int N
- See Also:
- Constant Field Values
-
-
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 boardcol
- 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 boardcol
- 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 boardrow
- the row indexcol
- 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 setrow
- the row indexcol
- 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 positionj
- the column coordinate of the first positionrow
- the row coordinate of the second positioncol
- 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
-
-