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.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ]
My Code
class Solution {
public:
bool inline check(vector<string>& str, int i, int j, int n)
{
for (int ii = 0; ii < i; ii++)
if (str[ii][j] == 'Q')
return true;
for (int ii = i - 1, jj = j - 1; ii >= 0 && jj >= 0; ii--, jj--)
if (str[ii][jj] == 'Q')
return true;
for (int ii = i - 1, jj = j + 1; ii >= 0 && jj < n; ii--, jj++)
if (str[ii][jj] == 'Q')
return true;
return false;
}
void doSolve(vector<vector<string> >& strs, vector<string>& str, int i, int j, int n)
{
if (i == n || j == n)
return;
// false -> no clash
bool flag = check(str, i, j, n);
// Clash
if (flag == true)
doSolve(strs, str, i, j + 1, n);
else
{
str[i][j] = 'Q';
if (i == n - 1)
strs.push_back(str);
else
doSolve(strs, str, i + 1, 0, n);
str[i][j] = '.';
doSolve(strs, str, i, j + 1, n);
}
}
vector<vector<string> > solveNQueens(int n) {
vector<string> str(n, string(n, '.'));
vector<vector<string> > strs;
doSolve(strs, str, 0, 0, n);
return strs;
}
};
Runtime: 8 ms