Skip to content

Instantly share code, notes, and snippets.

@rjvdw
Last active December 28, 2025 22:16
Show Gist options
  • Select an option

  • Save rjvdw/e13ee4d6e48b93fc548cf88387b9353d to your computer and use it in GitHub Desktop.

Select an option

Save rjvdw/e13ee4d6e48b93fc548cf88387b9353d to your computer and use it in GitHub Desktop.
A Java application that solves the eight queens puzzle (https://en.wikipedia.org/wiki/Eight_queens_puzzle)
import java.util.*;
void main() {
int size = 8;
Board board = new Board(size);
int nrSolutions = solve(board, 0);
IO.println("Found " + nrSolutions + " solutions.");
}
/// Solve the [Eight queens puzzle](https://en.wikipedia.org/wiki/Eight_queens_puzzle)
///
/// @param board The board on which to solve the puzzle.
/// @param row The current row on which a queen is being placed (0-indexed).
/// @return The number of solutions that were found.
int solve(Board board, int row) {
if (row == board.size()) {
IO.println("Found a solution:\n" + board);
return 1;
}
int count = 0;
for (int col = 0; col < board.size(); col += 1) {
if (board.place(col, row)) {
count += solve(board, row + 1);
board.pop();
}
}
return count;
}
/// Represents a chess board on which a single queen can be placed per row.
class Board {
/// The size of the board. The board is assumed to be square.
private final int size;
/// The placement of the queens. Assumes one queen per row, this array stores their column indexes.
private final int[] queens;
/// The number of queens already placed.
private int idx = 0;
Board(int size) {
this.size = size;
queens = new int[size];
}
/// Return the size of the board.
///
/// @return The size of the board.
int size() {
return size;
}
/// Place a queen at the specified location.
///
/// @param col The column at which the queen is being placed.
/// @param row The row in which the queen is being placed.
/// @return `true` if it was possible to place the queen, `false` otherwise.
/// @throws IndexOutOfBoundsException If either the board is already full, or the row does not match the current value.
boolean place(int col, int row) {
if (idx == size || row != idx) throw new IndexOutOfBoundsException();
if (isCovered(col, row)) return false;
queens[idx] = col;
idx += 1;
return true;
}
/// Remove the most recently placed queen.
///
/// @throws IndexOutOfBoundsException If there are no queens to be removed.
void pop() {
if (idx == 0) throw new IndexOutOfBoundsException();
idx -= 1;
}
/// Checks if any of the currently placed queens can attack the specified location.
///
/// @param col The column to check.
/// @param row The row to check.
/// @return `true` if there is a queen that can attack the specified location, `false` otherwise.
private boolean isCovered(int col, int row) {
for (int row1 = 0; row1 < idx; row1 += 1) {
int col1 = queens[row1];
if (col1 == col) return true;
if (col - row == col1 - row1) return true;
if (size - col - row == size - col1 - row1) return true;
}
return false;
}
/// Returns a string representation of the board.
///
/// Squares containing a queen are marked with a `Q`, empty squares are marked with an underscore (`_`).
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int row = 0; row < size; row += 1) {
for (int col = 0; col < size; col += 1) {
sb.append(queens[row] == col ? "Q " : "_ ");
}
sb.append('\n');
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment