What Is 8 Queen Problem in Data Structure?


Scott Campbell

The 8 Queen Problem is a classic puzzle in the field of data structure. It is a well-known problem in the realm of chess where the objective is to place eight queens on an 8×8 chessboard in such a way that no two queens threaten each other. In other words, no two queens should share the same row, column, or diagonal.

Understanding the Problem

In order to solve this problem, we need to find a solution that satisfies all the constraints mentioned above. This can be achieved by utilizing various data structures and algorithms.


One common approach to solving the 8 Queen Problem is through backtracking. Backtracking is a technique where we systematically explore all possible solutions by making choices and then undoing them if they lead to an invalid solution.

The Algorithm

Let’s break down the algorithm for solving this problem step-by-step:

  1. Create an empty chessboard of size 8×8.
  2. Start with the first row and place a queen in the first column.
  3. Moving onto the next row, check if placing a queen in any column of that row violates any of the constraints (row, column, diagonal).
  4. If it does violate any constraint, move to the next column and repeat step 3.
  5. If none of the positions violate any constraint, mark that position as part of a valid solution so far and move on to the next row recursively.
  6. If all rows are successfully filled with queens without violating any constraint, we have found a valid solution. Print or store it for further analysis.
  7. If at any point during recursion we reach a dead-end (i.e., no valid position can be found in the current row), backtrack to the previous row and continue from the last valid position.
  8. Repeat steps 3-7 until all possible solutions are found.

Implementing the Solution

Now let’s take a look at how we can implement this algorithm using a programming language like Java:

<pre><code>public class EightQueens {
    private static final int BOARD_SIZE = 8;
    private static int[][] board = new int[BOARD_SIZE][BOARD_SIZE];

    public static void main(String[] args) {

    private static void solve(int row) {
        if (row == BOARD_SIZE) {

        for (int col = 0; col < BOARD_SIZE; col++) {
            if (isSafe(row, col)) {
                board[row][col] = 1;
                solve(row + 1);
                board[row][col] = 0;

    private static boolean isSafe(int row, int col) {
        // Check if there is a queen in the same column
        for (int i = 0; i < row; i++) {
            if (board[i][col] == 1) {
                return false;

        // Check if there is a queen in the upper-left diagonal
        for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
            if (board[i][j] == 1) {
                return false;

        // Check if there is a queen in the upper-right diagonal
        for (int i = row, j = col; i >= 0 && j < BOARD_SIZE; i--, j++) {
            if (board[i][j] == 1) {
                return false;

        return true;

    private static void printSolution() {
        for (int[] row : board) {
            for (int cell : row) {
                System.out.print(cell == 1 ? "Q " : "- ");

By running this code, you will get all possible solutions to the 8 Queen Problem. Each solution is represented by placing a 'Q' to indicate the position of a queen on the chessboard.


The 8 Queen Problem is an interesting puzzle that challenges our ability to think logically and efficiently. By using backtracking and carefully considering the constraints, we can find all valid solutions to this problem. Understanding and solving such problems not only helps in improving our problem-solving skills but also deepens our understanding of data structures and algorithms.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy