-
Notifications
You must be signed in to change notification settings - Fork 0
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
b61c271
commit 0ff2c08
Showing
2 changed files
with
117 additions
and
0 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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |