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.."]
]
感觉题目说的不清不楚的,至少我看完上面的题目描述,我不知道我应该做什么。
将n个皇后棋子布置在一个n*n的棋盘上,使两个皇后之间无法互相攻击,及每一行,每一列,每一斜线上都只有唯一一个棋子存在(注意有两条斜线)。
以行作为回溯单位
//to do
1 | public List<List<String>> solveNQueens(int n) { |