|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * 51. N-Queens |
8 |
| - * |
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. |
10 |
| -
|
11 |
| - Given an integer n, return all distinct solutions to the n-queens puzzle. |
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. |
13 |
| -
|
14 |
| - For example, |
15 |
| - There exist two distinct solutions to the 4-queens puzzle: |
16 |
| -
|
17 |
| - [ |
18 |
| - [".Q..", // Solution 1 |
19 |
| - "...Q", |
20 |
| - "Q...", |
21 |
| - "..Q."], |
22 |
| -
|
23 |
| - ["..Q.", // Solution 2 |
24 |
| - "Q...", |
25 |
| - "...Q", |
26 |
| - ".Q.."] |
27 |
| - ] |
28 |
| - */ |
29 | 6 | public class _51 {
|
30 | 7 |
|
31 |
| - public static class Solution1 { |
| 8 | + public static class Solution1 { |
32 | 9 |
|
33 |
| - public List<List<String>> solveNQueens(int n) { |
34 |
| - List<List<String>> result = new ArrayList<>(); |
35 |
| - if (n <= 0) { |
36 |
| - return result; |
37 |
| - } |
38 |
| - search(n, new ArrayList<>(), result); |
39 |
| - return result; |
40 |
| - } |
| 10 | + public List<List<String>> solveNQueens(int n) { |
| 11 | + List<List<String>> result = new ArrayList<>(); |
| 12 | + if (n <= 0) { |
| 13 | + return result; |
| 14 | + } |
| 15 | + search(n, new ArrayList<>(), result); |
| 16 | + return result; |
| 17 | + } |
41 | 18 |
|
42 |
| - private void search(int n, ArrayList<Integer> col, List<List<String>> result) { |
43 |
| - if (col.size() == n) { |
44 |
| - result.add(drawChessBoard(col)); |
45 |
| - return; |
46 |
| - } |
| 19 | + private void search(int n, ArrayList<Integer> col, List<List<String>> result) { |
| 20 | + if (col.size() == n) { |
| 21 | + result.add(drawChessBoard(col)); |
| 22 | + return; |
| 23 | + } |
47 | 24 |
|
48 |
| - for (int i = 0; i < n; i++) { |
49 |
| - if (!isValid(col, i)) { |
50 |
| - continue; |
| 25 | + for (int i = 0; i < n; i++) { |
| 26 | + if (!isValid(col, i)) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + col.add(i); |
| 30 | + search(n, col, result); |
| 31 | + col.remove(col.size() - 1); |
| 32 | + } |
51 | 33 | }
|
52 |
| - col.add(i); |
53 |
| - search(n, col, result); |
54 |
| - col.remove(col.size() - 1); |
55 |
| - } |
56 |
| - } |
57 | 34 |
|
58 |
| - private boolean isValid(ArrayList<Integer> col, int next) { |
59 |
| - int row = col.size(); |
60 |
| - for (int i = 0; i < row; i++) { |
61 |
| - if (next == col.get(i)) { |
62 |
| - return false; |
63 |
| - } |
| 35 | + private boolean isValid(ArrayList<Integer> col, int next) { |
| 36 | + int row = col.size(); |
| 37 | + for (int i = 0; i < row; i++) { |
| 38 | + if (next == col.get(i)) { |
| 39 | + return false; |
| 40 | + } |
64 | 41 |
|
65 |
| - if (i - row == col.get(i) - next) { |
66 |
| - return false; |
67 |
| - } |
| 42 | + if (i - row == col.get(i) - next) { |
| 43 | + return false; |
| 44 | + } |
68 | 45 |
|
69 |
| - if (i - row == next - col.get(i)) { |
70 |
| - return false; |
| 46 | + if (i - row == next - col.get(i)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + } |
| 50 | + return true; |
71 | 51 | }
|
72 |
| - } |
73 |
| - return true; |
74 |
| - } |
75 | 52 |
|
76 |
| - private ArrayList<String> drawChessBoard(ArrayList<Integer> col) { |
77 |
| - ArrayList<String> chessBoard = new ArrayList<>(); |
| 53 | + private ArrayList<String> drawChessBoard(ArrayList<Integer> col) { |
| 54 | + ArrayList<String> chessBoard = new ArrayList<>(); |
78 | 55 |
|
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 |
| - } |
| 56 | + for (int i = 0; i < col.size(); i++) { |
| 57 | + String row = ""; |
| 58 | + for (int j = 0; j < col.size(); j++) { |
| 59 | + if (col.get(j) == i) { |
| 60 | + row += "Q"; |
| 61 | + } else { |
| 62 | + row += "."; |
| 63 | + } |
| 64 | + } |
| 65 | + chessBoard.add(row); |
| 66 | + } |
| 67 | + return chessBoard; |
87 | 68 | }
|
88 |
| - chessBoard.add(row); |
89 |
| - } |
90 |
| - return chessBoard; |
91 | 69 | }
|
92 |
| - } |
93 | 70 | }
|
0 commit comments