/* Author: King, nkuwjg@gmail.com Date: Jul 25, 2013 Problem: N-Queens Difficulty: Medium Source: https://oj.leetcode.com/problems/n-queens/ Notes: 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.."] ] Solution: Recursion (DFS). Use bit-manipulation solution (See N-QueensII for more details). */ public class Solution { public List solveNQueens(int n) { List res = new ArrayList(); List sol = new ArrayList(); solveNQueensRe(n, 0, 0, 0, sol, res); return res; } public void solveNQueensRe(int n, int row, int ld, int rd, List sol, List res) { if (row == (1<= 0; --i) { int pos = 1 << i; if ((int)(avail & pos) != 0) { char[] str = new char[n]; Arrays.fill(str, '.'); str[i] = 'Q'; sol.add(str); solveNQueensRe(n, row | pos, (ld|pos)<<1, (rd|pos)>>1, sol, res); sol.remove(sol.size()-1); } } } }