forked from robinrst/Dynamic-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackTracking.java
More file actions
46 lines (34 loc) · 926 Bytes
/
Copy pathBackTracking.java
File metadata and controls
46 lines (34 loc) · 926 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
45
46
package Lec12;
public class BackTracking {
public static void main(String[] args) {
// TODO Auto-generated method stub
// queenPrmt(new boolean[4], 0, 2, "");
queenComb(new boolean[4], 0, 2, "", 0);
}
public static void queenPrmt(boolean[] board, int qpsf, int tq, String ans) {
if (qpsf == tq) {
System.out.println(ans);
return;
}
for (int i = 0; i < board.length; i++) {
if (board[i] == false) {
board[i] = true;
queenPrmt(board, qpsf + 1, tq, ans + "q" + qpsf + "b" + i + " ");
board[i] = false;
}
}
}
public static void queenComb(boolean[] board, int qpsf, int tq, String ans, int Lastplaced) {
if (qpsf == tq) {
System.out.println(ans);
return;
}
for (int i = Lastplaced; i < board.length; i++) {
if (board[i] == false) {
board[i] = true;
queenComb(board, qpsf + 1, tq, ans + "q" + qpsf + "b" + i + " ", i);
board[i] = false;
}
}
}
}