-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathQueenCombinations2DBoard.java
More file actions
44 lines (37 loc) · 960 Bytes
/
Copy pathQueenCombinations2DBoard.java
File metadata and controls
44 lines (37 loc) · 960 Bytes
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
package Backtracking;
public class QueenCombinations2DBoard {
public static void main(String[] args) {
// TODO Auto-generated method stub
QueenCombinations2D(new boolean[2][2],2,0,0,0,"");
}
public static void QueenCombinations2D(boolean[][] board,int tq,int qpsf,int col,int row,String ans)
{
//Positive Base Case
if(qpsf==tq)
{
System.out.println(ans);
return;
}
//Either use line 21-24 OR line 27-31
//Manually changing row
// if(col==board[0].length)
// {
// row++;
// col=0;
// }
//Or Giving a recursive call to avoid changing row manually
if(col==board[0].length)
{
QueenCombinations2D(board,tq,qpsf,0,row+1,ans);
return;
}
//Negative Base Case
if(row==board.length)
return;
board[row][col]=true;
QueenCombinations2D(board,tq,qpsf+1,col+1,row,ans+"{"+row+","+col+"}");
board[row][col]=false;
//Not Place
QueenCombinations2D(board,tq,qpsf,col+1,row,ans);
}
}