[LeetCode]51. N-Queens&52. N-Queens II

来源:互联网 发布:c语言编写银行家算法 编辑:程序博客网 时间:2024/05/23 01:22

51 . N-Queens
Hard

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.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[
[“.Q..”, // Solution 1
“…Q”,
“Q…”,
“..Q.”],

[“..Q.”, // Solution 2
“Q…”,
“…Q”,
“.Q..”]
]

9ms:

 public List<List<String>> solveNQueens(int n) {              List<List<String>> res = new ArrayList<List<String>>();              int[] queenList = new int[n]; //第i个位置存放的数表示row行时,Q的列              placeQueen(queenList, 0, n, res);//在第0行放Q              return res;          }          private void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {              //如果已经填满,就生成结果              if (row == n) {                  ArrayList<String> list = new ArrayList<String>();                  for (int i = 0; i < n; i++) {                      String str = "";                      for (int col = 0; col < n; col++){                          if(queenList[i] == col) {                              str += "Q";                          } else {                              str += ".";                          }                      }                      list.add(str);                  }                  res.add(list);              }              for (int col = 0; col < n; col++) {//循环每一列                  if (isValid(queenList, row, col)) { //如果在该列放入Q不冲突的话                      queenList[row] = col;                      placeQueen(queenList, row + 1, n, res);                  }              }          }          private boolean isValid(int[] queenList, int row, int col) {              for (int i = 0; i < row; i++) {                  int pos = queenList[i];                  if (pos == col) { //和新加入的Q处于同一列                      return false;                  }                  if (pos + row - i == col) { //在新加入的Q的右对角线上                      return false;                  }                  if (pos - row + i == col) { //在新加入的Q的左对角线上                      return false;                  }              }              return true;          } 

52 . N-Queens II
Hard

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

public class Solution {    int count = 0;    public int totalNQueens(int n) {        boolean[] cols = new boolean[n];     // columns   |        boolean[] d1 = new boolean[2 * n];   // diagonals \        boolean[] d2 = new boolean[2 * n];   // diagonals /        backtracking(0, cols, d1, d2, n);        return count;    }    public void backtracking(int row, boolean[] cols, boolean[] d1, boolean []d2, int n) {        if(row == n) count++;        for(int col = 0; col < n; col++) {            int id1 = col - row + n;            int id2 = col + row;            if(cols[col] || d1[id1] || d2[id2]) continue;            cols[col] = true; d1[id1] = true; d2[id2] = true;            backtracking(row + 1, cols, d1, d2, n);            cols[col] = false; d1[id1] = false; d2[id2] = false;        }    }}
0 0