Skip to content

Commit

Permalink
Java dsa-Backtracking
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaspundpal committed Jan 8, 2023
1 parent b61c271 commit 0ff2c08
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Backtracking/NQueen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package Java_DSA.Backtracking;
import java.util.*;

public class NQueen
{
public static void saveBoard(char[][] board,List<List<String>> allBoards){
String row = "";
List<String> newBoard = new ArrayList<>();
for (int i = 0;i < board.length;i++){
row = "";
for(int j = 0;j < board.length;j++){
if(board[i][j] == 'Q')
row = row + 'Q';
else
row = row + '.';
}
newBoard.add(row);
}
allBoards.add(newBoard);
}
public static boolean isSafe(int row,int col,char[][] board){
//checking horizontally
for(int j = 0;j < board.length;j++){
if(board[row][j] == 'Q'){
return false;
}
}

//checking verically
for(int i = 0;i < board.length;i++){
if(board[i][col] == 'Q'){
return false;
}
}

//checking upper left
int r = row;
for(int c = col;c >= 0 && r >= 0 ;c--,r--){
if(board[r][c] == 'Q'){
return false;
}
}

//checking upper right
r = row;
for(int c = col;c < board.length && r >= 0 ;c++,r--){
if(board[r][c] == 'Q'){
return false;
}
}

//checking lower left
r = row;
for(int c = col;c >= 0 && r < board.length ;c--,r++){
if(board[r][c] == 'Q'){
return false;
}
}

//checking lower right
r = row;
for(int c = col;c < board.length && r < board.length ;c++,r++){
if(board[r][c] == 'Q'){
return false;
}
}

return true;
}
public static void helper(char[][] board,List<List<String>> allBoards,int col){
if(col == board.length){
saveBoard(board,allBoards);
return;
}
for(int row = 0;row < board.length;row++){
if(isSafe(row,col,board)){
board[row][col] = 'Q';
helper(board,allBoards,col+1);
board[row][col] = '.';
}
}
}
public static List<List<String>>calcNQueen(int n){
List<List<String>>allBoards = new ArrayList<>();
char[][] board = new char[n][n];
helper(board,allBoards,0);

return allBoards;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<List<String>> result = new ArrayList<>();
System.out.println("Enter the degree of the chess : ");
int n = sc.nextInt();
result = calcNQueen(n);
System.out.println(result);
}
}
19 changes: 19 additions & 0 deletions Backtracking/StringPermutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Java_DSA.Backtracking;

public class StringPermutation {
public static void calcPermutation(String str,String perm,int index){
if(str.length() == 0){
System.out.println(perm);
return;
}
for(int i = 0;i < str.length();i++){
char currChar = str.charAt(i);
String newStr = str.substring(0,i) + str.substring(i+1);
calcPermutation(newStr,perm + currChar,index+1);
}
}
public static void main(String[] args) {
String str = "ABC";
calcPermutation(str,"",0);
}
}

0 comments on commit 0ff2c08

Please sign in to comment.