-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8b0e84
commit 3839f5d
Showing
1 changed file
with
51 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,67 @@ | ||
package Recursion.Trying_out_all_Combos_Hard; | ||
|
||
import java.util.*; | ||
|
||
public class NQueens { | ||
/** | ||
* The function checks if it is safe to place a queen at a given position on a chessboard. | ||
* | ||
* @param row The row index of the current position on the chessboard. | ||
* @param col The column index of the current position on the chessboard. | ||
* @param board The board is a 2-dimensional char array representing the chessboard. Each cell of | ||
* the array represents a square on the chessboard. The value 'Q' represents a queen placed on that | ||
* square, and any other value represents an empty square. The size of the board is determined by | ||
* the length of | ||
* @return The method is returning a boolean value. | ||
*/ | ||
public static boolean isSafe(int row, int col, char[][] board) { | ||
|
||
for(int j=0; j<board.length; j++) { | ||
if(board[row][j] == 'Q') { | ||
return false; | ||
} | ||
class Solution { | ||
public static boolean isSafe(char nq[][], int row, int col) { | ||
// Vertical Up direction | ||
for (int i = row - 1; i >= 0; i--) { | ||
if (nq[i][col] == 'Q') return false; | ||
} | ||
|
||
for(int i=0; i<board.length; i++) { | ||
if(board[i][col] == 'Q') { | ||
return false; | ||
} | ||
// Diagonal left direction | ||
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { | ||
if (nq[i][j] == 'Q') return false; | ||
} | ||
|
||
int r = row; | ||
for(int c=col; c>=0 && r>=0; c--, r--) { | ||
if(board[r][c] == 'Q') { | ||
return false; | ||
} | ||
// Diagonal right direction | ||
for (int i = row - 1, j = col + 1; i >= 0 && j < nq.length; i--, j++) { | ||
if (nq[i][j] == 'Q') return false; | ||
} | ||
|
||
r = row; | ||
for(int c=col; c<board.length && r>=0; r--, c++) { | ||
if(board[r][c] == 'Q') { | ||
return false; | ||
} | ||
} | ||
|
||
r = row; | ||
for(int c=col; c>=0 && r<board.length; r++, c--) { | ||
if(board[r][c] == 'Q') { | ||
return false; | ||
} | ||
} | ||
|
||
for(int c=col; c<board.length && r<board.length; c++, r++) { | ||
if(board[r][c] == 'Q') { | ||
return false; | ||
} | ||
} | ||
|
||
|
||
return true; | ||
} | ||
// | ||
/** | ||
* The function "SaveBoard" takes a 2D character array representing a chessboard and a list of | ||
* lists of strings as parameters, and converts the chessboard into a list of strings representing | ||
* the positions of the queens on the board, which is then added to the result list. | ||
* | ||
* @param board A 2D character array representing a chessboard. Each element in the array | ||
* represents a cell on the board, and the character 'Q' represents a queen placed on that cell. | ||
* @param result The "result" parameter is a List of Lists of Strings. It is used to store the | ||
* different configurations of the chessboard with queens placed on it. Each inner List represents | ||
* a single configuration, and each String in the inner List represents a row of the chessboard. | ||
* The character 'Q' represents | ||
*/ | ||
public static void SaveBoard(char board[][],List<List<String>> result){ | ||
String str =""; | ||
List<String> resultList = new ArrayList<>(); | ||
for(int i=0;i<board.length;i++){ | ||
str=""; | ||
for(int j=0;j<board[0].length;j++){ | ||
if(board[i][j]=='Q'){ | ||
str+="Q"; | ||
} | ||
else{ | ||
str+="."; | ||
} | ||
} | ||
resultList.add(str); | ||
} | ||
result.add(resultList); | ||
} | ||
/** | ||
* The function solves the N-Queens problem by placing queens on a chessboard such that no two | ||
* queens threaten each other. | ||
* | ||
* @param board The board parameter is a 2D character array representing the chessboard. Each cell | ||
* can have one of two values: 'Q' if there is a queen placed in that cell, or '.' if the cell is | ||
* empty. | ||
* @param result The "result" parameter is a List of Lists of Strings. It is used to store the | ||
* solutions to the N-Queens problem. Each solution is represented as a List of Strings, where each | ||
* String represents a row of the chessboard. | ||
* @param col The parameter "col" represents the current column that we are trying to place a queen | ||
* in. It starts from 0 and goes up to the length of the board. | ||
*/ | ||
public static void solveNqueen(char board[][],List<List<String>> result,int col){ | ||
if(col==board.length){ | ||
SaveBoard(board,result); | ||
|
||
public static void nQueens(char nq[][], int row, List<List<String>> ans) { | ||
if (row == nq.length) { | ||
ans.add(convertToResult(nq)); | ||
return; | ||
} | ||
for(int row=0;row<board.length;row++){ | ||
if(isSafe(row,col,board)){ | ||
board[row][col] = 'Q'; | ||
solveNqueen(board,result,col+1); | ||
board[row][col] = '.'; | ||
for (int i = 0; i < nq.length; i++) { | ||
if (isSafe(nq, row, i)) { | ||
nq[row][i] = 'Q'; | ||
nQueens(nq, row + 1, ans); | ||
nq[row][i] = '.'; | ||
} | ||
} | ||
} | ||
public static List<List<String>> solveNQueens(int n) { | ||
char board[][]=new char[n][n]; | ||
List<List<String>> result = new ArrayList<>(); | ||
solveNqueen(board,result,0); | ||
|
||
private static List<String> convertToResult(char[][] nq) { | ||
List<String> result = new ArrayList<>(); | ||
for (int i = 0; i < nq.length; i++) { | ||
result.add(new String(nq[i])); | ||
} | ||
return result; | ||
} | ||
public static void main(String[] args) { | ||
System.out.println(solveNQueens(4)); | ||
|
||
public List<List<String>> solveNQueens(int n) { | ||
char nq[][] = new char[n][n]; | ||
List<List<String>> ans = new ArrayList<>(); | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < n; j++) { | ||
nq[i][j] = '.'; | ||
} | ||
} | ||
nQueens(nq, 0, ans); | ||
return ans; | ||
} | ||
} | ||
public class NQueens { | ||
public static void main(String[] args) { | ||
Solution s=new Solution(); | ||
List<List<String>> ans=s.solveNQueens(4); | ||
// print ans | ||
for(List<String> l:ans){ | ||
for(String str:l){ | ||
System.out.println(str); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} |