Total Accepted: 35425Total Submissions: 134108My Submissions
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:
public static List<List<String>> solveNQueens(int n) {
List<List<String>> ret = new ArrayList<List<String>>();
int[] queenList = new int[n];
placeQueen(queenList, 0, n, ret);
return ret;
}
// 递归回溯8皇后,关键记录下到达了哪一行了
public static void placeQueen(int[] queenList, int row, int n, List<List<String>> ret){
// Base Case, 已经完成任务了
if(row == n){
StringBuilder[] sol = new StringBuilder[n];