[go: up one dir, main page]

0% found this document useful (0 votes)
17 views2 pages

8 Queens Problem Java Solution

The document contains a Java implementation of the Eight Queens problem, which aims to place eight queens on an 8x8 chessboard such that no two queens threaten each other. It includes methods for printing the board, checking if a queen can be safely placed, and a backtracking algorithm to find a solution. The main method initializes the board and attempts to solve the problem, printing the solution if found or indicating that no solution exists.

Uploaded by

rachi.website
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

8 Queens Problem Java Solution

The document contains a Java implementation of the Eight Queens problem, which aims to place eight queens on an 8x8 chessboard such that no two queens threaten each other. It includes methods for printing the board, checking if a queen can be safely placed, and a backtracking algorithm to find a solution. The main method initializes the board and attempts to solve the problem, printing the solution if found or indicating that no solution exists.

Uploaded by

rachi.website
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class EightQueens {

// Method to print the board with the solution


static void printBoard(int board[][], int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1) {
System.out.print("Q "); // Q represents a queen
} else {
System.out.print(". "); // . represents an empty space
}
}
System.out.println();
}
}

// Method to check if a queen can be placed at board[row][col]


static boolean isSafe(int board[][], int row, int col, int N) {
// Check the column for previous rows
for (int i = 0; i < row; i++) {
if (board[i][col] == 1) {
return false;
}
}

// Check the upper-left diagonal


for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) {
return false;
}
}

// Check the upper-right diagonal


for (int i = row, j = col; i >= 0 && j < N; i--, j++) {
if (board[i][j] == 1) {
return false;
}
}

return true;
}

// Backtracking function to solve the problem


static boolean solveEightQueens(int board[][], int row, int N) {
if (row == N) {
// All queens are placed
return true;
}

// Try placing a queen in each column one by one


for (int col = 0; col < N; col++) {
if (isSafe(board, row, col, N)) {
board[row][col] = 1; // Place queen

// Recur to place the next queen


if (solveEightQueens(board, row + 1, N)) {
return true;
}
// If placing queen in board[row][col] doesn't work, backtrack
board[row][col] = 0;
}
}

return false; // If no place is found for a queen in this row


}

public static void main(String[] args) {


int N = 8; // Size of the board (8x8 for the 8-Queens problem)
int[][] board = new int[N][N];

if (!solveEightQueens(board, 0, N)) {
System.out.println("Solution does not exist");
} else {
printBoard(board, N);
}
}
}

You might also like