-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNQueenBranchAndBound.java
40 lines (28 loc) · 1.08 KB
/
NQueenBranchAndBound.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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
boolean[][] board = new boolean[n][n];
boolean cols[] = new boolean[n];
boolean nd[] = new boolean[2*n-1];
boolean rd[] = new boolean[2*n-1];
nqueens(board, 0, cols, nd, rd, "");
}
static void nqueens(boolean board[][], int row, boolean cols[], boolean nd[], boolean rd[], String asf) {
if(row == board.length) {
System.out.println(asf + ".");
return;
}
for(int i=0; i<board[0].length; i++) {
if(!cols[i] && !nd[row+i] && !rd[row-i+board.length-1]) {
cols[i] = nd[row+i] = rd[row-i+board.length-1] = true;
board[row][i] = true;
nqueens(board, row+1, cols, nd, rd, asf + row + "-" + i + ", ");
board[row][i] = false;
cols[i] = nd[row+i] = rd[row-i+board.length-1] = false;
}
}
}
}