forked from Ciphrox/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNQueens.java
56 lines (42 loc) · 1.53 KB
/
NQueens.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
class NQueens {
static String blank=".";
static List<List<String>> ans=new ArrayList<List<String>>();
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<n;i++){blank+=".";}
List<String> grid=new ArrayList<>();
queensUtil(0,n,grid);
for(List<String> l:ans) {
for(String s:l){System.out.println(s+" ");}
System.out.println();
}
}
public static void queensUtil(int i,int n,List<String> grid){
if(i==n){ans.add(new ArrayList<String>( grid));
return ;}
StringBuilder s=new StringBuilder(blank);
for(int j=0;j<n;j++){
if(isPossible(i,j,n,grid)){
s.setCharAt(j,'Q');
grid.add(s.toString());
queensUtil(i+1,n,grid) ;
grid.remove(grid.size()-1);
s.setCharAt(j,'.');
}
}
return ;
}
public static boolean isPossible(int i,int j,int n,List<String> grid){
// if(i<0||j<0||i>=n||j>=n)return false;
int b=1;
for(int a=i-1;a>=0;a--){
if(j-b>=0) {if(grid.get(a).charAt(j-b)=='Q')return false;}
if(j+b<n){if(grid.get(a).charAt(j+b)=='Q')return false;}
if(grid.get(a).charAt(j)=='Q')return false;
b++;
}
return true;
}
}