LeetCode-51.N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

Example:

Input: 4
Output: [
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

使用深度优先遍历,并剪枝
注意斜线上的规律,左斜线上的点 横纵坐标和相同,右斜线上的点 横纵坐标差相同
 1 class Solution {//mytip
 2     List<List<String>> re = new ArrayList<>();
 3     
 4     public List<List<String>> solveNQueens(int n) {
 5         List<Integer> cols= new ArrayList<>();
 6         dfs(n,0,cols);
 7         return re;
 8         
 9     }
10     
11     private void dfs(int n,int level,List<Integer> cols){
12         if(level==n){
13             List<String> list = new ArrayList<>();
14             for (int i = 0; i < n ; i++) {
15                 StringBuffer s = new StringBuffer();
16                 int col= cols.get(i);
17                 for (int j = 0; j < n; j++) {
18                     if(j==col){
19                         s.append("Q");
20                     }
21                     else{
22                         s.append(".");
23                     }
24                 }
25                 list.add(s.toString());
26             }
27             re.add(list);
28             return;
29         }
30         for (int i = 0; i < n ; i++) {
31             int flag = 0;
32             for (int j = 0; j < cols.size(); j++) {
33                 int col = cols.get(j);
34                 if(i==col|| i+level==j+col || i-level==col-j){
35                     flag=1;
36                     break;
37                 }
38             }
39             if(0==flag){
40                 cols.add(i);
41                 dfs(n,level+1,cols);
42                 cols.remove(cols.size()-1);
43             }
44         }
45     }
46 }

更简洁写法
 1 class Solution {//mytip
 2     List<List<String>> re = new ArrayList<>();
 3     
 4     public List<List<String>> solveNQueens(int n) {
 5         List<Integer> cols= new ArrayList<>();
 6         dfs(n,0,cols,new ArrayList<Integer>(),new ArrayList<Integer>());
 7         return re;
 8         
 9     }
10     
11     private void dfs(int n,int level,List<Integer> cols,List<Integer> sum,List<Integer> dif){
12         if(level==n){
13             List<String> list = new ArrayList<>();
14             for (int i = 0; i < n ; i++) {
15                 char[] s= new char[n];
16                 Arrays.fill(s,'.');
17                 s[cols.get(i)]='Q';
18                 list.add(new String(s));
19             }
20             re.add(list);
21             return;
22         }
23         for (int i = 0; i < n ; i++) {
24             if(cols.contains(i)||sum.contains(i+level)||dif.contains(i-level))
25                 continue;
26             cols.add(i);
27             sum.add(i+level);
28             dif.add(i-level);
29             dfs(n,level+1,cols,sum,dif);
30             cols.remove(cols.size()-1);
31             sum.remove(sum.size()-1);
32             dif.remove(dif.size()-1);
33         }
34     }
35 }

 

相关题

N皇后 LeetCode52 https://www.cnblogs.com/zhacai/p/10621381.html



posted @ 2019-03-29 14:59  月半榨菜  阅读(120)  评论(0编辑  收藏  举报