id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> ans;\n void markBoard(vector<vector<int>>& board,int row,int column){\n int n= board.size();\n for(int i=column;i<n;i++){\n board[row][i]=0;\n }\n int c=column;\n for(int i=row;i<n;i++){\n if(c... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> ans;\n void markBoard(vector<vector<int>>& board,int row,int column){\n int n= board.size();\n for(int i=column;i<n;i++){\n board[row][i]=0;\n }\n int c=column;\n for(int i=row;i<n;i++){\n if(c... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n\n bool isvalid(vector<string> path, int queenrow, int queencol, int dim){\n for(int i = queenrow - 1; i>= 0;i--){\n if( path[i][queencol] == 'Q') return false;\n }\n for(int i=queenrow-1,j=queencol-1; i>=0 && j>=0; i--,j--){\n if( pa... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n\n bool isvalid(vector<string> path, int queenrow, int queencol, int dim){\n for(int i = queenrow - 1; i>= 0;i--){\n if( path[i][queencol] == 'Q') return false;\n }\n for(int i=queenrow-1,j=queencol-1; i>=0 && j>=0; i--,j--){\n if( pa... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n void markBoard(vector<vector<int>>& board,int row,int column){\n int n= board.size();\n for(int i=column;i<n;i++){\n board[row][i]=0;\n }\n int c=column;\n for(int i=row;i<n;i++){\n if(c<n) {\n board[i][c... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "void markAttackArea(vector<vector<bool>> &visited, int row, int col, int n) {\n\n // mark row\n for(int j=0; j<n; j++) {\n visited[row][j] = true;\n }\n\n // mark column\n for(int i=0; i<n; i++) {\n visited[i][col] = true;\n }\n\n // mark top right tilted diagonal\n fo... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n \n bool isSafe(int row, int col, unordered_map<int,int> rc, int n){\n int r = row, c = col;\n //towards top left\n while(r>=0 && c>=0){\n if(rc.find(r)!=rc.end() && rc[r]==c) return false;\n r--;\n c--;\n }\n ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> isSolution(vector<vector<int>> &board, vector<vector<string>> &ans, int n) {\n vector<string> temp;\n for(int i = 0; i < n; i++) {\n string s;\n for(int j = 0; j < n; j++) {\n if(board[i][j] == 1) {\n ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> solveNQueens(int n) {\n vector<vector<vector<int>>> solutions;\n set<int> used_cols;\n set<int> used_diagonals;\n set<int> used_diagonals_2;\n vector<vector<int>> starter(n, vector<int>(n));\n helper(starter, 0,... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> ans;\n map<string, int> _map;\n\n vector<string> posToStr(vector<vector<int>> &pos, int n) {\n int i, j;\n vector<string> ans;\n vector<vector<char>> matrix(n, vector<char>(n, '.'));\n for (i = 0; i < pos.size(); i++) {... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> ans;\n map<string, int> _map;\n\n vector<string> posToStr(vector<vector<int>> &pos, int n) {\n int i, j;\n vector<string> ans;\n vector<vector<char>> matrix(n, vector<char>(n, '.'));\n for (i = 0; i < pos.size(); i++) {... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<string>> ans;\n map<string, int> _map;\n\n vector<string> posToStr(vector<vector<int>> &pos, int n) {\n int i, j;\n vector<string> ans;\n vector<vector<char>> matrix(n, vector<char>(n, '.'));\n for (i = 0; i < pos.size(); i++) {... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,bool> rows;\n unordered_map<int,bool> lowerdiagonal;\n unordered_map<int,bool> upperdiagonal;\n bool isSafe(vector<vector<char>> & board,int row ,int col,int n)\n {\n if(rows[row]==true || lowerdiagonal[row+col]==true || upperdiagonal[row-... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n void fill(int n, int r, int c, vector<vector<bool>>& availability){\n int i=r;int j=c;\n availability[r][c] = false;\n while(j<n){\n availability[i][j] = false;\n j++;\n }\n j=c;\n while(j>=0){\n avail... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\n bool possible(vector<string> cur, int i, int n){\n int straight = i;\n int left = i-1;\n int right = i+1;\n for(int j=cur.size()-1; j>=0; j--){\n if(cur[j][straight] == 'Q') return 0;\n if(left>=0){\n if(cur[j][left] == ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "struct boardDesc {\npublic:\n set<pair<int,int>> queens;\n map<int, set<int>> validSlots;\n};\n\nclass Solution {\npublic:\n boardDesc emptyBoard(int n) {\n boardDesc board;\n\n set<int> emptyRow;\n for (int j = 0; j < n; ++j) {\n emptyRow.insert(j);\n }\n\n ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n bool isPossible(vector<string>&board,int col,int row,vector<int>leftrow,vector<int>upperd,vector<int>lowerd,int n){\n if(leftrow[row] == 1) return false;\n if(lowerd[row+col]==1) return false;\n if(upperd[n-1+col-row]==1) return false;\n return tru... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n int n;\n bool isValid(vector<string>& curr, vector<int> col, vector<int> diag, vector<int> anti_diag, int r, int c){\n if(col[c])\n return false;\n if(diag[c-r + n-1])\n return false;\n if(anti_diag[r+c])\n return false... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\nprivate:\n void solve(int r, map<int,bool> col, map<int,bool> posdiag, map<int,bool> negdiag, vector<vector<string>>& ans,vector<string> board, int n){\n if(r == n){\n ans.push_back(board);\n return;\n }\n for(int i = 0; i < n; i++){\n ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n\n void addQueen(vector<string> board, int seq, int n, vector<vector<string>> &ans, int space)\n {\n if (seq == n)\n {\n ans.push_back(board);\n return;\n }\n \n if (space == n * n)\n {\n return;\n ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n\n\n\n bool isSafe1(int row, int col, vector < string > board, int n) {\n // check upper element\n int duprow = row;\n int dupcol = col;\n\n while (row >= 0 && col >= 0) {\n if (board[row][col] == 'Q')\n return false;\n row--;\n ... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n bool isSafe1(int row, int col, vector<string> board, int n) {\n int duprow = row;\n int dupcol = col;\n\n while (row >= 0 && col >= 0) {\n if (board[row][col] == 'Q') return false;\n row--;\n col--;\n }\n\n c... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\n bool isSafe(int row,int col,vector<string>board,int n){\n int duprow=row;\n int dupcol=col;\n\n while(row>=0 && col>=0){\n if(board[row][col]=='Q') return false;\n\n row--;\n col--;\n\n }\n col=dupcol;\n row=du... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\nbool issafe(int row,int col,vector<string> board,int n){\n int duprow = row;\n int dupcol = col;\n\n while(row>=0 && col>=0){\n if(board[row][col]=='Q'){\n return false;\n }\n row--;\n col--;\n }\n\n row = duprow;\n col = d... |
51 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st... | 3 | {
"code": "class Solution {\npublic:\n bool isSafe(int row, int col, vector<string> board, int n) {\n int dupRow = row;\n int dupCol = col;\n\n while (row >= 0 && col >= 0) {\n if (board[row][col] == 'Q') {\n return false;\n }\n row--;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 0 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n if(n==1)return 1;\n if(n<=3)return 0;\n if(n==4)return 2;\n if(n==5)return 10;\n if(n==6)return 4;\n if(n==7)return 40;\n if(n==8)return 92;\n if(n==9)return 352;\n return 0;\n }\n};... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 0 | {
"code": "class Solution {\n bool isValid(const vector<int>& pos, int l) {\n for (int i = 0; i < l; ++i) {\n if (pos[i] == pos[l] ||\n l-i == std::abs(pos[l]-pos[i]))\n return false;\n }\n return true;\n }\npublic:\n int totalNQueens(int n) {\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 1 | {
"code": "class Solution {\npublic:\nvector<int> row,col,leftdia,rightdia;\n int totalNQueens(int n) {\n vector<vector<int>> grid(n,vector<int>(n,0));\n row.resize(n,0);\n col.resize(n,0);\n leftdia.resize(2*n,0);\n rightdia.resize(2*n,0);\n return solve(0,n,grid);\n }... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 1 | {
"code": "class Solution {\npublic:\n void solve(int col, vector<int>& board, vector<vector<int>>& res, vector<int>& leftRow, vector<int>& lowerDiagonal, vector<int>& upperDiagonal,int n){\n if(col==n){\n res.push_back(board);\n return;\n }\n for(int row=0;row<n;row++){\... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 1 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n vector<vector<string>> ans;\n vector<string> board(n);\n string s(n,'.');\n for(int i=0;i<n;i++)\n board[i] = s;\n solve(0,ans,board,n);\n return ans.size();\n }\n void solve(int col, vecto... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 1 | {
"code": "class Solution {\n int N;\n vector<vector<string>> ans;\n vector<string> grid;\n\n void makeGrid(int n)\n {\n this->N = n;\n grid.clear();\n string t = \"\";\n for (int i = 0; i < n; i++)\n t += \".\";\n for (int i = 0; i < n; i++)\n g... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 2 | {
"code": "class Solution {\npublic:\n short co=0;\n vector<bool> c, ld, rd;\n void h(int n, short i) {\n if (i==n) {\n ++co;\n return;\n }\n short j=0;\n for (; j<n; j++) {\n if (!c[j] && !ld[i-j+(n-1)] && !rd[i+j]) {\n c[j]=true;\n... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 2 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n // Each solution can be represented as a vector as follows:\n // sol[i] = j -> there is a queen on row i, col j\n vector<int> sol(n, 0);\n vector<vector<int>> allSolutions;\n vector<bool> visited(n, false);\n\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void solve(int ind,int n,vector<string>&temp,vector<vector<string>>&ans){\n if(ind==n){\n ans.push_back(temp);\n return;\n }\n\n for(int i=0;i<n;i++){\n //decide to put at ans[i][ind]\n //check row\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n vector<bool> col(n,false);\n vector<bool> diag1(2*n, false);\n vector<bool> diag2(2 * n, false);\n\n function<int(int)> dfsQueens = [&](int row_no){\n if (row_no >= n){\n return 0;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n\n void addSolution(vector<vector<char>>& board, vector<vector<string>>& ans, int n){\n vector<string> temp_ans;\n for(int i=0; i<n; i++){\n string temp=\"\";\n for(int j=0; j<n; j++){\n temp+=board[i][j];\n }\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n bool can_place(int row, int col, const vector<vector<bool>> &board) {\n // check row\n for (size_t col = 0; col < board.size(); col++) {\n if (board[row][col]) {\n return false;\n }\n }\n // check column\n for (size_t row = 0; row < board.size();... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n\nprivate:\n\n void solve(int col,vector<string> &board,vector<int> &leftRow,vector<int> &upperDiagonal,vector<int> lowerDiagonal,int n,int &cnt){\n\n if(col >= n){\n cnt++;\n return;\n }\n\n for(int row = 0 ;row < n;row++){\n\n if(... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n \n\n bool isSafe(int row, int col, vector<vector<char>> &board, int n) {\n // Check if it's safe to place a queen at board[row][col]\n int i = row;\n int j = col;\n\n // Check row on the left\n while (j >= 0) {\n if (board[i][j... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<char>>grid; \n vector<vector<string>> result;\n bool canPlaceQueen(int row,int col,int n){\n for (int i = row-1; i>=0; i--)\n {\n if (grid[i][col]=='Q')\n {\n return false;\n }\n //column check\n }\n for (int i = row-1,j=col-1; i>=0 and j>=0; ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<char>> grid;\n vector<vector<string>> result;\n\n bool canPlaceQueen(int r, int c, int n) {\n // row check\n for (int i=0; i<r;i++) {\n if (grid[i][c]=='Q') return false;\n }\n // left diagonal check\n for (int... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\nprivate:\n // set to record each queen location fwd_diag(r+c), bwk_diag(r-c)\n std::set<int> col{}, fwd_diag{}, bwk_diag{};\n\n int count = 0;\n\n void findPositions(int row, int n)\n {\n if(row == n) {\n count++;\n }\n else {\n fo... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\nprivate:\n // set to record each queen location fwd_diag(r+c), bwk_diag(r-c)\n std::set<int> col{}, fwd_diag{}, bwk_diag{};\n\n int count = 0;\n\n void findPositions(int row, int n)\n {\n if(row == n) {\n count++;\n }\n else {\n fo... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n unordered_set<int> top;\n unordered_set<int> topRight;\n unordered_set<int> topLeft;\n void makeBoard(int row, vector<string> &board, int n, vector<vector<string>> &result) {\n if(row == n) {\n result.push_back(board);\n return;\n }\n\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void rec(int n, vector<vector<int>>& ans, unordered_set<int>& cols, unordered_set<int>& diag1, unordered_set<int>& diag2, vector<int>& output, int index) {\n if (index == n) {\n if (output.size() == n) {\n ans.push_back(output);\n }... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void helper(int col ,int n, int &res , vector<bool> topDiagonal , \n vector<bool> bottomDiagonal , vector<bool> mid) {\n if(col == n) {\n // res.push_back(board);\n res++;\n return;\n }\n\n for(int row ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int publicN=0; \n int ans=0;\n set<int> colSet;\n set<int> PlusSet;\n set<int> MinusSet;\n bool isValid(int i,int j)\n {\n if (colSet.find(j)!=colSet.end() || PlusSet.find(i+j)!=PlusSet.end() ||MinusSet.find(i-j)!=MinusSet.end())\n {\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) \n {\n int sol = 0;\n unordered_set<int> rows;\n unordered_set<int> cols;\n unordered_set<int> diagonals;\n unordered_set<int> anti_diagonals;\n backtrack(sol, 0, n, rows, cols, diagonals, anti_diagonals);\n... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) \n {\n int sol = 0;\n unordered_set<int> rows;\n unordered_set<int> cols;\n unordered_set<int> diagonals;\n unordered_set<int> anti_diagonals;\n backtrack(sol, 0, n, rows, cols, diagonals, anti_diagonals);\n... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void solve(int col, int n, vector<string>& board,\n vector<vector<string>>& ans, vector<int> leftRow,\n vector<int> leftDiagonal, vector<int> lowerDiagonal) {\n if (col == n) {\n ans.push_back(board);\n return;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "int sol_from(vector<int> orient,int ind,int row, int n, vector<bool> row_done, vector<bool> diagu_done, vector<bool> diagd_done)\n{\n orient[ind]=row;\n row_done[row]=true;\n diagu_done[ind-row+n]=true;\n diagd_done[row+ind]=true;\n int ans=0;\n if(ind==n-1)\n {\n return 1;}\n\n... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n set<int> atkCols;\n set<int> pdiags;\n set<int> ndiags;\n vector<vector<char>> setup(n, vector<char>(n, '.'));\n vector<vector<string>> setups;\n generate(setups, setup, 0, atkCols, pdiags, ndiags, n);\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n bool isValid(vector<int> tmp, int i)\n {\n if(tmp.size()==0)\n return true;\n\n int y = tmp.size();\n int x = i;\n\n for(int j=0;j<tmp.size();j++)\n {\n if(tmp[j]==i)\n return false;\n if((y-x) == (j-tmp[j])... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int cnt = 0;\n bool is_valid(int r, int c, vector<string> & board) {\n // up, up-left and up-right\n vector<pair<int, int>> dirs = {{-1, 0}, {-1, -1}, {-1, 1}};\n for (auto & dir : dirs) {\n int curr = r, curc = c;\n while (0 <= c... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void diagonal(vector<string> &dp, int i, int j, int n){\n for(int x=0;x<n;x++){\n if(x!=j){\n dp[i][x] = '.';\n }\n if(x!=i){\n dp[x][j] = '.';\n }\n if(x!=0){\n if(i-x>... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n bool canPlace (int x, int col, vector<string>& s)\n {\n int row =x;\n int column = col;\n while ( x >= 0 && col >= 0 )\n { \n if(s[x][col]=='Q') return false;\n x--;\n col--; \n }\n x= r... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n bool canPlace (int x, int col, vector<string>& s)\n {\n int row =x;\n int column = col;\n while ( x >= 0 && col >= 0 )\n { \n if(s[x][col]=='Q') return false;\n x--;\n col--; \n }\n x= r... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n/*\nTime complexity:O(N^2)\n\nSpace complexity:O(N)\n*/\n\nbool isSafe(int row,int col,vector<int>board,int n){\n for(int i=0;i<row;i++){\n if(board[i]==col) return false;\n if(abs(row-i)==abs(col-board[i])) return false;\n }\n return tr... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n\nvector<int> curr;\nvector<bool> col, a, b;\nint ans = 0;\nint tot;\n\nvoid sol_map(int row) {\n if (row == -1)\n return;\n//cout << row << endl;\n if (row == tot) {\n ans++;\n sol_map(row-1);\n return;\n }\n\n if (curr[row] != -1) {\n col[c... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n vector<vector<int>> ans;\n vector<int> loc(n);\n solve(n,loc,0,ans);\n return ans.size();\n }\nprivate:\n void solve(int n, vector<int>& loc, int row, vector<vector<int>>& ans){\n if(row==n){\n an... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\nbool check(int n,int row,int col,vector<string>& board)\n{\n for (int i=0;i<row;i++)\n {\n if (board[i][col] == 'Q') {\n return false;\n }\n }\n int i=row;\n int j=col;\n while(i>=0&&j>=0)\n {\n if(board[i][j]=='Q')\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int co=0;\n unordered_set<int> c, ld, rd;\n void h(int n, int i) {\n if (i==n) {\n ++co;\n return;\n }\n int j=0;\n for (; j<n; j++) {\n auto x=c.insert(j);\n auto y=ld.insert(j-i);\n aut... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n bool check(int level,int col,int total,vector<int> v){\n int temp=col;\n for(int i=level-1;i>=0;i--){\n if(col==0) break;\n if(v[i]==col-1){\n return false;\n }\n col--;\n }\n col=temp;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n bool isqueensettle(int i,int a,vector<string>& ss,int n){\n for(int x=0;x<ss.size();x++){\n if(ss[x][a]=='Q'){\n return false;\n }\n int row=0;\n int col=0;\n for(int y=0;y<n;y++){\n i... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n\nvoid solve(vector<vector<string>>& R, vector<string> C, int i,int n, vector<pair<int,int>> M){\n if(i>=n){\n R.push_back(C);\n return;\n }\n for(int j=0;j<n;j++){\n bool go=true;\n for(int x=0;x<M.size();x++){\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void backtrack(int n,int row, vector<string> output,vector<bool> upperCol,vector<bool> rightDiag,vector<bool> leftDiag,int &cnt){\n\n if(row==n){\n cnt++;\n return;\n }\n \n for(int col=0;col<n;col++){\n if(upperCol... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void solve(vector<vector<string>>& result, vector<string>board, vector<bool> diag1, vector<bool> diag2, vector<bool> cols, int row, int n) {\n if (row == n) {\n result.push_back(board);\n return;\n }\n for(int i=0;i<n;i++) {\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n bool is_good(const vector<string>& board, int num) {\n vector<bool> rows(num + 1);\n vector<bool> cols(board.front().size());\n\n for (int i = 0; i < board.size(); i++) {\n for (int j = 0; j < board[i].size(); j++) {\n if (board[... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int number_of_solutions = 0;\n\n int totalNQueens(int n) {\n std::set<std::pair<int, int>> available_positions;\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n available_positions.insert(std::make_pair(i, j));\n }\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n void solve(int col, int& ans, vector<string> board, vector<int> leftRow, vector<int> lowerDiagonal, vector<int> upperDiagonal, int n){\n if(col == n){\n ans++;\n return;\n }\n \n for(int row = 0; row < n; row++){\n if(le... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n void solve(int col, vector<vector<string>>& ans, vector<string> board, vector<int> leftRow, vector<int> lowerDiagonal, vector<int> upperDiagonal, int n){\n if(col == n){\n ans.push_back(board);\n return;\n }\n \n for(int row = 0; r... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n vector<int>movex = {0,0,1,-1,1,-1,1,-1};\n vector<int>movey = {1,-1,0,0,1,1,-1,-1};\n\n bool check(int x, int y, int n){\n if(x>=0 && x<n && y>=0 && y<n) return true;\n return false;\n }\n\n vector<string> color(int n, int x, int y, vector<string>v){... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n\n bool isPlace(vector<int>place,int pos,int QN){\n for(int i=1;i < QN;i++){\n if(place[i] == pos || (abs(pos-place[i]) == abs(i - (QN)))){\n return false;\n }\n }\n return true;\n }\n void Queen(vector<int>&place... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\nprivate:\n void solve(vector<vector<int>> board, int& res, int x, int y, int n)\n {\n int i, j;\n for(i=0; i<n; i++)\n {\n board[i][y] = 1;\n board[x][i] = 1;\n if(x+i < n && y+i < n)\n board[x+i][y+i] = 1;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\nprivate:\n void solve(vector<vector<int>> board, int& res, int x, int y, int n)\n {\n int i, j;\n for(i=0; i<n; i++)\n {\n board[i][y] = 1;\n board[x][i] = 1;\n if(x+i < n && y+i < n)\n board[x+i][y+i] = 1;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "// class Solution {\n// public:\n// int totalNQueens(int n) {\n \n// }\n// };\nclass Solution {\n bool safe(int i,int j,int x,int y){\n if(y==j || (abs(i-x)==abs(j-y)) || i==x) return false;\n return true;\n }\n bool isOk(vector<vector<int>>& placed,int i,int j){\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n bool check(vector<vector<int>> &qpos, int col, int row){\n for(auto queens:qpos){\n if(queens[0]==row){return false;}\n if(abs(queens[0]-row)==abs(queens[1]-col)){return false;}\n }\n return true;\n }\n void helper(int &n, vector<vector... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n vector<vector<int>> locs;\n cout << \" start \" << endl;\n return recurser(n, locs, 0, n);\n }\n\n int recurser(int n, vector<vector<int>>& locs, int row, int len) {\n if (n == 0) {\n return 1;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n void solve(int i,vector<int> col,vector<vector<int>> map,int n,vector<vector<string>> &ans)\n {\n if(i==n)\n {\n vector<string> v;\n for(int i=0;i<n;i++)\n {\n string s;\n for(int j=0;j<n;j++)\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n map<string, int> mp;\n int nn;\n int rec(string &s) {\n if (s.size() == nn) {\n return 1;\n }\n if (mp.count(s) != 0) {\n return mp[s];\n }\n int ans = 0;\n set<int> na;\n for (int i = 0; i < s.size(... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\nint ans=0;\n bool f1(int x,int y,vector<vector<int>>&v){\n for(auto it:v){\n int a=it[0],b=it[1];\n if(a==x || b==y || abs(a-x)==abs(b-y)) return false;\n }\n return true;\n }\n void f(int idx,int n,vector<vector<int>>&v){\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int size;\n int queens(vector<vector<int>> board,int idx,vector<bool> vert,vector<bool> right,vector<bool> left)\n {\n if(idx==-1)\n return 1;\n int sum=0;\n for(int i=0;i<size;i++)\n {\n int ivert=i;\n int ir... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int size;\n int queens(vector<vector<int>> board,int idx,vector<bool> vert,vector<bool> right,vector<bool> left)\n {\n if(idx==-1)\n return 1;\n int sum=0;\n for(int i=0;i<size;i++)\n {\n int ivert=i;\n int ir... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n private:\n vector<vector<string>> ans;\n int no;\n void combinations(int level, vector<pair<int, char>> risks, vector<string> possibility) {\n if(level == no) {\n ans.push_back(possibility);\n return;\n }\n vector<int> safe;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int size;\n int queens(vector<vector<int>> board,int idx,vector<bool> vert,vector<bool> hori,vector<bool> right,vector<bool> left)\n {\n if(idx==-1)\n return 1;\n int sum=0;\n for(int i=0;i<size;i++)\n {\n int ivert=i;\n... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n unordered_set<int> cols_used;\n cols_used.reserve(n);\n unordered_set<int> diagonals_used;\n diagonals_used.reserve(n * 2 - 1);\n return CheckPermutations(n, 0, cols_used, diagonals_used);\n }\n\nprivate:\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n if (n <= 1)\n return n;\n\n vector<vector<bool>> board(n, vector<bool>(n, false));\n return placeQueens(n, 0, 0, board);\n }\n\n int placeQueens(int n, int queens, int row, vector<vector<bool>>& board) {\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\nvector<vector<string>>ans;\n bool check(vector<string>&v, int i, int j){\n //upper diag\n int it, jt;\n it=i-1, jt=j-1;\n while(it>=0&&jt>=0){\n if(v[it][jt]=='Q')return 0;\n it--;jt--;\n }\n it=i-1, jt=j+1;\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n if (n <= 1)\n return n;\n\n vector<vector<bool>> board(n, vector<bool>(n, false));\n return placeQueens(n, 0, 0, board);\n }\n\n int placeQueens(int n, int queens, int row, vector<vector<bool>>& board) {\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n int res = 0;\n unordered_set<int> col, sum, substract;\n totalNQueens(res, n, 0, col, sum, substract);\n return res;\n }\n\nprivate:\n void totalNQueens(int& res, int n, int row, unordered_set<int> col, unordered_... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n unordered_set<int> cols;\n unordered_set<int> posDiag;\n unordered_set<int> negDiag;\n\n return helper(n, 0, cols, posDiag, negDiag);\n }\n\nprivate:\n int helper(const int& n, int row, unordered_set<int> cols,\n ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int bt(int r, int n, unordered_set<int> off, unordered_set<int> on, unordered_set<int> cols){\n if(r == n) return 1;\n int s = 0;\n for(int c = 0; c < n; c++){\n int o = r + c;\n int oo = r - c;\n if(off.find(oo) == off.en... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n int dfs(int n, vector<string>& board, unordered_set<int> columns, unordered_set<int> diagonals, unordered_set<int>antidiagonals, int row){\n if(row>=n){\n // if(columns.size()==n){\n // return 1;\n // }\n // else{\n // ... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\nint ans = 0;\nint N;\n\nvoid bc(int row, set<int> c, set<int> d1, set<int> d2) {\n if (row==N){\n ans++;\n return;\n }\n for (int col = 0; col < N;col++) {\n if (!c.contains(col) && !d1.contains(row-col) && !d2.contains(N-col-row)) {\n c.i... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int queenCount(int n, set<int> cols, set<int> posDiag, set<int> negDiag, int row)\n {\n if(row == n)\n {\n return 1;\n }\n int count = 0;\n for(int i=0; i<n; i++)\n {\n if(cols.find(i) == cols.end() && posDiag... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": " class Solution {\npublic:\n int totalNQueens(int n) {\n int counter=0;\n string s(n,'.');\n vector<string> temp(n,s);\n vector<int> leftRow(n,0), lowerDiagonal(2*n-1,0), upperDiagonal(2*n-1,0);\n\n stack<tuple<int, int, vector<string>, vector<int>, vector<int>, vector... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\n int ans;\npublic:\n void calc(vector<vector<bool>> visited, int itr, int n){\n for(int k=0;k<n;k++){\n if(!visited[itr][k]){\n if(itr==n-1){\n ans++;\n // cout<<\"ans:\"<<itr<<\" \"<<k<<endl;\n }\... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int giveCount(vector<vector<bool>> avail, int n, int r){\n if(r == n - 1){\n int base = 0;\n for(int i=0; i<n; i++){\n if(avail[r][i]){\n base++;\n }\n }\n\n cout<<base<<endl;\... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n int totalNQueens(int n) {\n vector<vector<int> > v;\n vector<int> row(n,0);\n for(int i=0;i<n;i++){\n v.push_back(row);\n }\n int count = 0;\n combinations(v,0,count);\n return count;\n }\n void combinations(ve... |
52 | <p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>
<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the <strong>n-queens puzzle</strong></em>.</p>
<p> </p... | 3 | {
"code": "class Solution {\npublic:\n string f(int k,int n){\n string tempo=\"\";\n for(int i=0;i<n;i++){\n if(i==k)tempo.push_back('Q');\n else tempo.push_back('.');\n }\n return tempo;\n }\n int totalNQueens(int n) {\n vector<string> temp; ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.