Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to solve the N queens problem in Java?

/*
	This implementation produces a non-attacking placement
	of chess queens. A non-attacking placement is one where
	no queen can attack another queen in a single turn.
	This is one of the best examples of backtracking. 

	Let N be the number of queens to be placed on 
	a N*N board. 
	Time complexity: O(N!)
	Space complexity: O(N^2)
*/
public class NQueensProblem {
	int[][] board;
	int N; // Number of queens
	public NQueensProblem(int N) {
		this.N = N;
		board = new int[N][N];
	}
	public void printBoard() {
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				System.out.print(board[i][j] + " ");
			}
			System.out.println();
		}
	}
	/*
	 * A utility function to check if a queen can be
	 * placed on board[row][col].
	 * Note that this function is called when "col" queens
	 * are already placed in columns from 0 to col -1.
	 * So we need to check only left side for attacking
	 * queens.
	 */
	public boolean isSafe(int row, int col) {
		int i, j;
		// Check the row on left side
		for (i = 0; i < col; i++) {
			if (board[row][i] == 1) {
				return false;
			}
		}
		// Check upper diagonal on left side
		for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
			if (board[i][j] == 1) {
				return false;
			}
		}
		// Check lower diagonal on left side
		for (i = row, j = col; i < N && j >= 0; i++, j--) {
			if (board[i][j] == 1) {
				return false;
			}
		}
		return true;
	}
	public boolean solveUtil(int col) {
		/*
		 * base case: If all queens are placed then return true
		 */
		if (col >= N) {
			return true;
		}
		/*
		 * Consider this column and try placing this queen
		 * in all rows one by one
		 */
		for (int row = 0; row < N; row++) {
			/*
			 * Check if the queen can be placed on board[row][col]
			 */
			if (isSafe(row, col)) {
				board[row][col] = 1;

				// Recur to place the rest of the queens
				if (solveUtil(col + 1) == true) {
					return true;
				}

				/*
				 * If placing queen in board[row][col] doesn't lead to a * solution then remove
				 * queen from board[i=row][col]
				 */
				board[row][col] = 0; // backtrack
			}
		}
		/*
		 * If the queen can not be placed in any row in this column col, * then return
		 * false
		 */
		return false;
	}
	/*
	 * This function solves the N Queen problem using Backtracking.
	 * It mainly uses solveNQUtil () to solve the problem.
	 * It returns false if queens cannot be
	 * placed, otherwise, return true and prints placement
	 * of queens in the form of
	 * 1s.
	 * Please note that there may be more than one solutions, this
	 * function prints one of the feasible solutions.
	 */
	public void solve() {
		if (solveUtil(0) == false) {
			System.out.println("No solution found.");
			return;
		}
		System.out.println("The following solution was found: ");
		printBoard();
	}
	public static void main(String[] args) {
		NQueensProblem problem = new NQueensProblem(4);
		problem.solve();
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: valueof vs tostring 
Java :: flutter remove last caracter from string 
Java :: clear jtable rows java 
Java :: get type java 
Java :: android studio, not loading url in webview 
Java :: initialize hashset with values java 9 
Java :: change number into array in java 
Java :: input char java 
Java :: android checkbox tint color 
Java :: javafx resizable window 
Java :: split string into int array 
Java :: hide elements android 
Java :: android studio change menu title 
Java :: android studio constraint layout proportional height 
Java :: in java how to throw exception from function 
Java :: android dismiss keyboard 
Java :: prepared statement update 
Java :: add view to relativelayout programmatically 
Java :: how to get last element of array java 
Java :: return the maximum sum of two numbers whose digits add up to an equal sum 
Java :: how to check if string is double or not in java 
Java :: Java Create LinkedList in Java 
Java :: java csv line split 
Java :: java secretkey 
Java :: java get JComboBox value 
Java :: intent pick action video android 
Java :: Array list deep copy 
Java :: How to find the logged-in user in Spring Boot? 
Java :: public static void main vs static public void main 
Java :: java solid principles 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =