|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 | 6 | /**
|
| 7 | + * 51. N-Queens |
| 8 | + * |
7 | 9 | * The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
|
8 | 10 |
|
9 |
| -
|
10 |
| -
|
11 | 11 | Given an integer n, return all distinct solutions to the n-queens puzzle.
|
12 |
| -
|
13 | 12 | Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
|
14 | 13 |
|
15 | 14 | For example,
|
|
29 | 28 | */
|
30 | 29 | public class _51 {
|
31 | 30 |
|
| 31 | + public static class Solution1 { |
| 32 | + |
32 | 33 | public List<List<String>> solveNQueens(int n) {
|
33 |
| - List<List<String>> result = new ArrayList<>(); |
34 |
| - if (n <= 0) { |
35 |
| - return result; |
36 |
| - } |
37 |
| - search(n, new ArrayList<>(), result); |
| 34 | + List<List<String>> result = new ArrayList<>(); |
| 35 | + if (n <= 0) { |
38 | 36 | return result;
|
| 37 | + } |
| 38 | + search(n, new ArrayList<>(), result); |
| 39 | + return result; |
39 | 40 | }
|
40 | 41 |
|
41 | 42 | private void search(int n, ArrayList<Integer> col, List<List<String>> result) {
|
42 |
| - if (col.size() == n) { |
43 |
| - result.add(drawChessBoard(col)); |
44 |
| - return; |
45 |
| - } |
46 |
| - |
47 |
| - for (int i = 0; i < n; i++) { |
48 |
| - if (!isValid(col, i)) { |
49 |
| - continue; |
50 |
| - } |
51 |
| - col.add(i); |
52 |
| - search(n, col, result); |
53 |
| - col.remove(col.size() - 1<
8000
/span>); |
| 43 | + if (col.size() == n) { |
| 44 | + result.add(drawChessBoard(col)); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + for (int i = 0; i < n; i++) { |
| 49 | + if (!isValid(col, i)) { |
| 50 | + continue; |
54 | 51 | }
|
| 52 | + col.add(i); |
| 53 | + search(n, col, result); |
| 54 | + col.remove(col.size() - 1); |
| 55 | + } |
55 | 56 | }
|
56 | 57 |
|
57 | 58 | private boolean isValid(ArrayList<Integer> col, int next) {
|
58 |
| - int row = col.size(); |
59 |
| - for (int i = 0; i < row; i++) { |
60 |
| - if (next == col.get(i)) { |
61 |
| - return false; |
62 |
| - } |
63 |
| - |
64 |
| - if (i - row == col.get(i) - next) { |
65 |
| - return false; |
66 |
| - } |
67 |
| - |
68 |
| - if (i - row == next - col.get(i)) { |
69 |
| - return false; |
70 |
| - } |
| 59 | + int row = col.size(); |
| 60 | + for (int i = 0; i < row; i++) { |
| 61 | + if (next == col.get(i)) { |
| 62 | + return false; |
71 | 63 | }
|
72 |
| - return true; |
73 |
| - } |
74 | 64 |
|
75 |
| - private ArrayList<String> drawChessBoard(ArrayList<Integer> col) { |
76 |
| - ArrayList<String> chessBoard = new ArrayList<>(); |
| 65 | + if (i - row == col.get(i) - next) { |
| 66 | + return false; |
| 67 | + } |
77 | 68 |
|
78 |
| - for (int i = 0; i < col.size(); i++) { |
79 |
| - String row = ""; |
80 |
| - for (int j = 0; j < col.size(); j++) { |
81 |
| - if (col.get(j) == i) { |
82 |
| - row += "Q"; |
83 |
| - } else { |
84 |
| - row += "."; |
85 |
| - } |
86 |
| - } |
87 |
| - chessBoard.add(row); |
| 69 | + if (i - row == next - col.get(i)) { |
| 70 | + return false; |
88 | 71 | }
|
89 |
| - return chessBoard; |
| 72 | + } |
| 73 | + return true; |
90 | 74 | }
|
91 | 75 |
|
92 |
| - public static void main(String...args) { |
93 |
| - _51 test = new _51(); |
94 |
| - |
95 |
| - ArrayList<Integer> col = new ArrayList<>(); |
96 |
| - col.add(0);//false, false, true, true, |
97 |
| -// col.add(1);//false, false, false, true, |
98 |
| -// col.add(2);//true, false, false, false, |
99 |
| -// col.add(3);//true, true, false, false, |
100 |
| - for (int x = 0; x < 4; x++) { |
101 |
| - System.out.print(test.isValid(col, x) + ", "); |
| 76 | + private ArrayList<String> drawChessBoard(ArrayList<Integer> col) { |
| 77 | + ArrayList<String> chessBoard = new ArrayList<>(); |
| 78 | + |
| 79 | + for (int i = 0; i < col.size(); i++) { |
| 80 | + String row = ""; |
| 81 | + for (int j = 0; j < col.size(); j++) { |
| 82 | + if (col.get(j) == i) { |
| 83 | + row += "Q"; |
| 84 | + } else { |
| 85 | + row += "."; |
| 86 | + } |
102 | 87 | }
|
| 88 | + chessBoard.add(row); |
| 89 | + } |
| 90 | + return chessBoard; |
103 | 91 | }
|
| 92 | + } |
104 | 93 | }
|
0 commit comments