-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintChessboard.java
More file actions
42 lines (38 loc) · 1.05 KB
/
Copy pathPrintChessboard.java
File metadata and controls
42 lines (38 loc) · 1.05 KB
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
public class PrintChessboard {
private static void printBoard(char chess[][]){
int n = chess.length;
for(int i = 0;i<n ;i++){
for(int j = 0;j<n ;j++){
System.out.print(chess[i][j] + " ");
}
System.out.println();
}
}
public static int count = 0;
public static void placeQueen(char chess[][], int row){
//base case
if(row == chess.length){
printBoard(chess);
count++;
System.out.println();
return;
}
//recursion matlab kaam
for(int j = 0;j<chess.length ;j++){
chess[row][j] = 'Q';
placeQueen(chess, row+1);
chess[row][j] = 'x';
}
}
public static void main(String args[]){
int n = 3;
char chess[][] = new char[n][n];
for(int i = 0;i<n ;i++){
for(int j = 0;j<n ;j++){
chess[i][j] = 'x';
}
}
placeQueen(chess,0);
System.out.println(count);
}
}