Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

n queens problem leetcode

/*
	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 :: how to read input in java 
Java :: string remove last character 
Java :: parse string to int java 
Java :: fill an array with random numbers between 1 and 100 java 
Java :: how to convert epoch time to date in java 
Java :: date format android 
Java :: how to convert int into int array of digits in java 
Java :: get distance from latitude and longitude code android 
Java :: java replaceall regex 
Java :: copy array in java 
Java :: view get text android 
Java :: string to bigdecimal 
Java :: bytes to string solidity 
Java :: how to add input in array java 
Java :: printing prime numbers in java 
Java :: Validation failed for query for method public abstract java.util.List 
Java :: odd number in java 
Java :: java replace character in string 
Java :: function in java 
Java :: calendar.year java 
Java :: image cropper implementation 
Java :: Hourglass Java 
Java :: add opacity to activity android 
Java :: java leap year 
Java :: how to check the current user in firebase android 
Java :: Duration class java 
Java :: How to efficiently convert a sorted array into a min height binary search tree, in Java? 
Java :: has a relationship in java 
Java :: get minimum of array java 
Java :: how to get last element in java 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =