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...
2
{ "code": "class Solution {\npublic:\n bool isSafe(vector<vector<int>>&board,int row,int col,int n)\n {\n int x=row,y=col;\n while(x>=0 && y>=0)\n {\n if(board[x][y]==1) return false;\n x--;\n y--;\n }\n\n x=row,y=col;\n while(y>=0)\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...
2
{ "code": "class Solution {\r\npublic:\r\n vector<vector<string>> final_result;\r\n int N;\r\n unordered_set<int> cols;\r\n unordered_set<int> diag;//right\r\n unordered_set<int> antidiag;//left \r\nvoid NQueen_fn(vector<string> &board ,int row ){//yha hum row pr daalenge queen ko and check krenge ki v...
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...
2
{ "code": "class Solution {\npublic:\n void solution_appender(vector<vector<char>> &chessboard , int board_size,vector<vector<string>> &final_ans)\n {\n //after i get a chessboard config which works i will traverse it row wise and attach each row string to \n //an empty array. After all rows trave...
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 unordered_set<int> col;\n unordered_set<int> posDiag;\n unordered_set<int> negDiag;\n\n vector<vector<char>> grid(n, vector<char>(n, '.'));\n vector<vector<string>> res;\n\n dfs(0, col, p...
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 // Sets to track used columns and diagonals\n set<int> col, posDiag, negDiag;\n vector<vector<string>> res;\n vector<string> board(n, string(n, '.'));\n \n // Backtracking function\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 void f(int i,int n,vector<vector<string>> &v,set<int> &c,set<int> &diff,set<int> &sum,vector<string> &curr) {\n // cout<<i<<endl;\n if(i==n) {\n v.push_back(curr); \n return ;\n }\n for(int j=0;j<n;j++) {\n if(c.fin...
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": "#include <iostream>\n#include <vector>\n#include <string>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<vector<string>> solveNQueens(int n) {\n char board[10][10]; // Static array to represent the board with a maximum size of 10x10\n\n // Initialize the board with '.'\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 map<int,int> straightline;\n map<int,int> diagonallyleft;\n map<int,int> diagonallyright;\n vector<vector<string>> solveNQueens(int n) {\n vector<vector<string>> output;\n vector<int> path;\n solve(n,0,output,path);\n return output; ...
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 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 ...
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": "typedef struct {\n uint64_t a;\n uint64_t b;\n} extint;\n\ntypedef struct {\n extint sol;\n extint guide;\n int queens;\n} Board;\n\nint getBit(extint x, int bit) {\n if (bit >= 64) {\n return (x.a & ((uint64_t)1 << ((uint64_t)bit-64))) > 0;\n } else {\n return (x.b & ((uint...
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": "typedef struct {\n uint64_t a;\n uint64_t b;\n} extint;\n\ntypedef struct {\n extint sol;\n extint guide;\n int guidebits;\n int queens;\n} Board;\n\nint getBit(extint x, int bit) {\n if (bit >= 64) {\n return (x.a & ((uint64_t)1 << ((uint64_t)bit-64))) > 0;\n } else {\n r...
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<string>> result;\n vector<bool> mask(3*n, true);\n for (auto tab: search(n, 0, mask)) {\n vector<string> board;\n for (auto col: tab) {\n string s = convert(...
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<string> board(n, string(n, '.'));\n vector<vector<string>> ans;\n solve(0, board, ans, n); // row board ans\n return ans;\n }\n\n void solve(int rowIdx, vector<string> board, vector<vector...
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>> finalAns;\n\n bool canBePlaced(vector<string>&ans,int srow,int scol,int erow)\n {\n for(int i=0;i<srow;i++)\n {\n if(ans[i][scol]=='Q') return false;\n }\n for(int i=srow-1,j=scol-1;i>=0&&j>=0;i--,j--)\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:\nusing vs = vector<string>; // Alias for vector<string>\n\nvector<vector<string>> boards;\nbool isIdxAvail(const vs &board, int row, int col)\n{\n return row >= 0 && col >= 0 && row < board.size() && col < board.front().size();\n}\nbool isQueen(const char &ch)\n{\n return 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 check_ans(vector<string> temp2, int n)\n {\n int count_q = 0;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(temp2[i][j]=='Q')\n count_q++;\n }\n }\n\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 {\n vector<vector<string>> res;\n\n bool isSafe(const vector<string>& board, int row, int col) {\n for (int i = 0; i < row; i++) \n if (board[i][col] == 'Q')\n return false;\n\n for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)\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 {\n public:\n bool validIndex(int i, int j, int n) {\n return i >= 0 and i < n and j >= 0 and j < n;\n }\n bool safe(int i, int j, int cx, int cy, vector<vector<bool>>& vis) {\n int n = vis.size();\n if (i < 0 or i >= n or j < 0 or j >= n or vis[i][j])\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 \n{\npublic:\n vector<vector<string>> solveNQueens(const int n)\n {\n vector<string> board(n, string(n, '.'));\n vector<vector<int>> blocked(n, vector<int>(n));\n set<vector<string>> result;\n set<vector<string>> memo;\n\n for (int i = 0; i < n; ++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:\nbool check(int n,vector<string>&board,int i,int j)\n{\n //upper left diag no queen should be present\n int row =i,col=j;\n while(row>-1&& col>-1)\n {\n if(board[row][col]=='Q')\n return 0;\n row--,col--;\n\n }\n\n \n // upper righ diag no queen should be prsen...
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 vector<vector<string>>res;\n\n// bool isvalid(vector<string>board,int row,int col)\n// {//upward\n// for(int i=row;i>=0;i--)\n// {\n// if(board[i][col]=='Q')\n// {\n// return false ; \n// }\n// }\n// //diagonally upward left \n// for(int i=ro...
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<string>> ans;\n vector<int> loc(n,-1);//loc[i] represents the number of column that the queen at ith row situates\n solve(n,loc,0,ans);\n return ans;\n }\nprivate:\n void solve(int ...
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_set<int> c, ld, rd;\n vector<vector<string>> r;\n void h(vector<string> &b, int n, int row) {\n if (row==n) {\n r.push_back(b);\n return;\n }\n for (int j=0; j<n; j++) {\n auto x=c.insert(j);\n a...
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 recur(int ind , int n , vector<string> board,\n set<int> &cols,set<int> &posd,set<int> &negd,\n vector<vector<string>> &res){\n if( ind ==n){\n res.push_back(board);\n return;\n }\n\n for(int j=0;j<n;j++){\n if(...
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 vector<vector<string>> res;\n void helper(int row, int n, vector<pair<int,int>> &positions, vector<string> &ans){\n if(row==n) {\n res.push_back(ans);\n return;\n }\n string temp(n,'.');\n unordered_set<int> st;\n for(auto x:...
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 isValid(vector<pair<int, int>> placedQueen, int m, int n)\n {\n for(auto [i, j]: placedQueen)\n {\n if(m == i || n == j || abs(m - i) == abs(n - j))\n return false;\n }\n return true;\n }\n void placeQueen(ve...
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_set<int> validQueenPoss(int n, int row, const vector<int>& queenPoss) {\n unordered_set<int> validPoss;\n for(int i = 0; i < n; i++) {\n validPoss.insert(i);\n }\n\n for(int i = 0; i < row; i++) {\n int queenPos = qu...
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 func(int j , int n , vector<int> row , vector<int> left ,\n vector<int> right , vector<string> mat , vector<vector<string>> &res )\n {\n if(j == n)\n {\n res.push_back(mat) ;\n return ;\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:\nvoid solve(int n,vector<string> temp,vector<vector<string>> &ans,int col,vector<int> upperdiagonal,vector<int> lowerdiagonal,vector<int> leftrow){\n if(col==n){\n ans.push_back(temp);\n return;\n }\n for(int row=0;row<n;row++){\n if(leftrow[row]==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>> res;\n void solve(int col,vector<int> leftRow,vector<int> lowerDiag,vector<int> upperDiag,vector<string> queen,int n)\n {\n if(col==n)\n {\n res.push_back(queen);\n return;\n }\n\n for(int i=0;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\nvector<vector<string>> ans;\n void p_t_q(vector<vector<char>>b,int i,int j,int noqn)\n {\n noqn++;\n for(int m=0;m<b.size();m++)\n {\n for(int n=0;n<b.size();n++)\n {\n if(m-n ==i-j)\n b[m][n] = '.';\n else if(m+n == i+j)\...
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 check(vector<pair<int, int>> queens, int i, int j) {\n for(int k = 0; k < queens.size(); ++k) {\n if(queens[k].first == i) {\n return false;\n }\n if(abs(queens[k].first - i) == abs(queens[k].second - j)) {\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 check(vector<pair<int, int>> queens, int i, int j) {\n for(int k = 0; k < queens.size(); ++k) {\n if(queens[k].first == i) {\n return false;\n }\n if(abs(queens[k].first - i) == abs(queens[k].second - j)) {\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<string>> ans;\n if (n < 0 || n > 9) return ans;\n \n set<int> cols;\n set<int> posDiag;\n set<int> negDiag;\n vector<string> board;\n for (int k=0;k<n;k++) {\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 {\n unordered_map<int,int>rows,cols,upperDaig,lowerDaig;\npublic:\n vector<vector<string>> solveNQueens(int n) {\n string str;\n int col=0;\n for(int i=0;i<n;i++)\n {\n str.push_back('.');\n }\n vector<string>output;\n vector<...
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:\nvoid check_uncheck(vector<vector<int>> &vis , int val , int i , int j , int n){\n for(int k = 0 ; k < n ; k++){\n vis[i][k] += val ;\n vis[k][j] += val ;\n if(i-k >= 0 && j-k >= 0 ){\n vis[i-k][j-k] += val ;\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 string pattern(int c, int n) {\n string s(n, '.');\n s[c] = 'Q';\n return s;\n }\n\n void helper(int n, int r, vector<int> col_track, vector<int> main_dia,\n vector<int> anti_dia, vector<string> temp,\n vector<vector<st...
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 placeQueen(vector<vector<string>>& res, vector<string>& cur,\n vector<vector<bool>>& validPos, int i, int n)\n {\n if(i == n)\n {\n res.push_back(cur);\n return;\n }\n\n for(int j = 0; j < n; ++j)\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 void changePlacementMatrix(vector<vector<bool>> &placementMatrix, int x, int y,int n){\n for(int i=0;i<n;i++){\n placementMatrix[x][i] = true;\n placementMatrix[i][y] = true; \n if((x+i<n and x+i>=0) && (y+i<n and y+i>=0)){\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 vector<vector<string>> ans;\n\n void mark(int i, int j, vector<vector<int>>& vis, int n)\n {\n int rows, cols;\n //mark rows\n for(rows = 0; rows < n; rows++)\n {\n vis[rows][j] = 1;\n }\n\n //mark columns\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<string>> o;\n vector<string> c(n);\n vector<vector<bool>> v(n, vector<bool>(n, false));\n vector<bool> h(n, false);\n dfs(n, 0, -1000, v, c, o, h);\n return o;\n }\n v...
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 safe(vector<vector<int>>& board, int col, int row, int n) {\n int x = row;\n int y = col - 1;\n while (y >= 0) {\n if (board[x][y] == 1) {\n return false;\n }\n y--;\n }\n x = row - 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 string str1, str2; \n vector<vector<string>> solveNQueens(int n) {\n vector<vector<string>> res; \n vector<int> prevIndices; \n if(n==2 || n==3) return res;\n\n vector<string> r; \n for(int i=0; i<n; i++) str1+='.'; \n str2=str1; \...
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<pair<int, int>>> solutions;\n vector<pair<int, int>> currentSolution;\n vector<vector<bool>> boardOfPossibilities(n, vector<bool>(n, true));\n solutions = solve(boardOfPossibilities, curr...
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 placeQueen(vector<vector<int>>& table, int n, int r, int c) {\n for (int i=0; i<n; i++) {\n table[r][i] = 1;\n table[i][c] = 1;\n if (r+i<n && c+i<n)\n table[r+i][c+i] = 1;\n if (r+i<n && c-i>=0)\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 bool QdiagAvailable(vector<string> currConfig, int r,int c, int n)\n {\n\n if(currConfig[r][c] == 'Q')\n {\n return true;\n }\n\n int tempr = r, tempc =c;\n while(tempr >= 0 && tempc >=0)\n {\n if(currConfig...
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 placeQ(int i, int j, int n, vector<vector<bool>>& attacked) {\n int k;\n for(k=0; k<n; k++) attacked[k][j] = true;\n for(k=0; k<n; k++) attacked[i][k] = true;\n \n k = -1*min(i,j);\n for(; max(i,j)+k<n; k++) attacked[i+k][j+k] = ...
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 string DOTS;\n vector<int> NUMS;\n int n;\npublic:\n vector<vector<string>> solveNQueens(int n) {\n this -> n = n;\n DOTS = string(n, '.');\n NUMS.resize(n);\n iota(NUMS.begin(), NUMS.end(), 0);\n vector<vector<string>> res {};\n help...
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 bool inDiag(vector<string> curr, int x, int y, int n){\n // printf(\"Checking diag for (%d, %d)\\n\", x, y);\n for(int i=x-min(x,y), j=y-min(x,y); i<n&&j<n; i++, j++){\n if(curr[i][j] == 'Q')\n return true;\n }\n\n int ma...
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<string> markRowsColumnsAndDiaginal(vector<string> v, int i, int j) {\n vector<string> board = v;\n int n = v.size();\n for(int k = 0; k < n; k++) {\n board[k][j] = '.';\n board[i][k] = '.';\n }\n int k = i, l = j...
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<string> markRowsColumnsAndDiaginal(vector<string> v, int i, int j) {\n vector<string> board = v;\n int n = v.size();\n for(int k = 0; k < n; k++) {\n board[k][j] = '.';\n board[i][k] = '.';\n }\n int k = i, l = j...
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 safePlace(vector<vector<char>> &board, int row, int col){\n int n = board.size();\n // -1, -1, -1, 1\n for(int i = 0; i<row; i++){\n if(board[i][col] == 'Q') return false;\n }\n\n int x = row-1, y = col-1;\n while(x >=...
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_set<int> cols; \n unordered_set<int> rows; \n unordered_set<int> diag1; \n unordered_set<int> diag2;\n\n bool check(int x, int y){\n if (rows.count(x) || cols.count(y) || diag1.count(x - y) || diag2.count(x + y)) return false;\n\n return tr...
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 using Board = vector<string>;\npublic:\n vector<vector<string>> solveNQueens(int n) {\n Board board(n, string(n, '.'));\n vector<Board> Solutions;\n findSolutions(board, Solutions,0);\n return Solutions;\n }\n\n void findSolutions(Board &board,\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 //diagonal\n //antidiagonal\n //column\n vector<vector<string>> solveNQueens(int n) {\n queue<set<pair<int,int>>> q;\n q.push({});\n vector<vector<string>> ans;\n while (q.size()) {\n set<pair<int,int>> cur = q.front(); q.pop();...
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 if (n == 1)\n return {{\"Q\"}};\n if (n <= 3)\n return {};\n vector<vector<string>> ans;\n vector<string> graph(n, string(n, '.'));\n vector<vector<bool>> trace(n, vector<b...
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<int>> validPositions(n, vector<int>(n));\n vector<string> currBoard;\n string boardString = \"\";\n for (int i = 0; i < n; i++) {\n boardString += \".\";\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 vector<vector<string>> ans;\n\n void helper(int i,int n,vector<string> curr,vector<vector<int>> valid){\n if(i == n){\n ans.push_back(curr);\n return;\n }\n\n for(int j = 0;j<n;j++){\n if(valid[i][j] == 0){\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<string>> res;\n if (n == 1) {\n res.push_back({\"Q\"});\n return res;\n }\n if (n == 2 || n == 3) {\n return res;\n }\n vector<int> position...
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>> sol;\n vector<string> createBoard(vector<vector<char>>& state, int n) {\n vector<string> board;\n for(int row = 0; row < n; row++) {\n string currRow(state[row].begin(), state[row].end());\n board.push_back(currRow...
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 solveNQueensWrapper(int n, vector<string> visited, vector<string> curr, vector<vector<string>>& ans, int currow) {\n if (currow == n) {\n ans.push_back(curr);\n }\n else {\n for (int i = 0; i < n; i++) {\n if (vis...
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 is_valid(vector<string> temp, int n, int x_real, int y_real){\n int x = x_real-1;\n int y = y_real-1;\n\n while (x >= 0 && y >= 0){\n \n if (temp[x][y] == 'Q'){\n return false;\n }\n x--;\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 void helper(int n, vector<string> &curr, vector<vector<string>> &ans, int row, set<int> col, set<int> diag, set<int> adiag){\n cout<<n<<endl;\n for(int i=0;i<curr.size();i++){\n for(int j=0;j<curr.size();j++){\n cout<<curr[i][j]<<\" \";...
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 solve(int row,int n,set<int>br,set<int>bc,set<int>bd1,set<int>bd2,vector<string>&board,set<vector<string>>&ans)\n {\n if(row == n){\n\n if(ans.find(board) == ans.end()){\n ans.insert(board);\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": "void Aum(vector<vector<string>> &ans, vector<string> &cur, int iter, int n, set<int> col, set<int> sum, set<int> diff){\n if(iter == n){\n vector<string> tempo(cur);\n ans.push_back(tempo);\n return;\n }\n string S;\n for(int aa = 0; aa < n; aa++){S.push_back('.');}\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 vector<vector<string>> v;\n void solve(vector<string> a, int n,set<int> s, int r, int e, set<pair<int,int>> p){\n if(s.size()==n){\n v.push_back(a);\n return;\n }\n if(r>n){\n return;\n }\n int f=0;\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 isValid(vector<int> s, vector<int> bs, vector<int> r, vector<int> c, int a, int b)\n {\n bool result = true;\n for (int i = 0; i < r.size(); i++)\n {\n result = (result)&&(a!=r[i])&&(b!=c[i])&&((a-b)!=s[i])&&((a+b)!=bs[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 void solve(int n,vector<int> col,set<int> s1,set<int> s2,vector<string> temp,int i,vector<vector<string>>& ans){\n if(i==n) ans.push_back(temp);\n string s=\"\";\n for(int j=0;j<n;j++) s+='.';\n for(int j=0;j<n;j++){\n if(col[j] || s1.fi...
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 mark(int row,int col,vector<vector<bool>> &pos){\n int nx,ny;\n int n = pos.size();\n for(int i=0;i<n;i++){\n pos[row][i] = true;\n pos[i][col] = true;\n nx = row+i;\n ny = col+i;\n if(nx>=0 && 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 isSafe(int line, int col, int n, vector<string> &temp,\n vector<bool> hor, vector<bool> ver, vector<bool> diag1, vector<bool> diag2) {\n\n if(hor[line] || ver[col] || diag1[n-(col-line)] || diag2[line+col])\n return false;\n return true;\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 {\n void SOL ( std::vector < std::vector < char > > & Grid , std::set < std::pair < int , int > > Empty , std::vector < std::vector < std::string > > & Ans , int & n , int Op ) {\n if ( Empty.empty() ) {\n std::vector < std::string > Vec ; int Count = 0 ;\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 bool isValid(vector<int> s, vector<int> bs, vector<int> r, vector<int> c, int a, int b)\n {\n bool result = true;\n for (int i = 0; i < r.size(); i++)\n {\n result = (result)&&(a!=r[i])&&(b!=c[i])&&((a-b)!=s[i])&&((a+b)!=bs[i]);\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 isValid(vector<int> s, vector<int> bs, vector<int> r, vector<int> c, int a, int b)\n {\n bool result = true;\n for (int i = 0; i < r.size(); i++)\n {\n result = (result)&&(a!=r[i])&&(b!=c[i])&&((a-b)!=s[i])&&((a+b)!=bs[i]);\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>> solveNQueens(int n) {\n vector<vector<string>> res;\n vector<string> sub(n, string(n, '.'));\n helper(n, 0, sub, {}, {}, {}, res);\n return res;\n }\n\nprivate:\n void helper(int n, int curr, vector<string> sub, unorder...
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 void SOL ( std::vector < std::vector < char > > & Grid , std::set < std::pair < int , int > > Empty , std::set < std::vector < std::string > > & Ans , int & n ,\n std::vector < std::vector < std::set < std::pair < int , int > > > > & Curr_Node_Visited_From , int Op ) {\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>> ans;\n void solve(int i,vector<string> tmp,unordered_set<int> col,unordered_set<int> pos,unordered_set<int> neg){\n int n = tmp.size();\n if(i==n){\n ans.push_back(tmp);\n return;\n }\n\n for(int j=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 void solve(int i,vector<string> tmp,unordered_set<int> col,unordered_set<int> pos,unordered_set<int> neg){\n int n = tmp.size();\n if(i==n){\n ans.push_back(tmp);\n return;\n }\n\n for(int j=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 std::vector<std::vector<std::string>> solveNQueens(int n) {\n std::vector<std::vector<std::string>> answer;\n std::vector<std::vector<int>> temp(n, std::vector<int>(n, 0));\n find(temp, 0, answer, n);\n return answer;\n }\n void find(std::vector<std::vector<int>> ...
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": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\nprivate:\n\n void solve1(vector<vector<int>> &grid, int k, int l){\n int n=grid.size();\n\n for(int i=-10;i<=10;++i){\n if(k+i>=0 && k+i<n)\n if(grid[k+i][l]<2) grid[k+i][l] = 1;\n if(l+...
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\n vector<vector<int>> ans;\n vector<vector<string>> Ans;\n vector<int>state(n,-1);\n solve(ans,state,0);\n string s,s1;\n while(n--){\n s+=\".\";\n }\n vector<str...
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<int>> board;\n int N;\n vector<vector<string>> res;\n void converter(vector<vector<int>> board){\n vector<string> temp;\n for(auto x : board){\n string str = \"\";\n for(int z : x){\n if(z == 2) str += ...
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 check(int col, vector<vector<string>>& ans, vector<string>& board, unordered_map<int,int> left, unordered_map<int,int> left_top, unordered_map<int,int> left_bot){\n if(col==board.size()) {\n ans.push_back(board);\n return;\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> > finalans;\n void findqueen(unordered_map<int, int> m_vertical, unordered_map<int, int> m_minus, unordered_map<int, int> m_plus, vector<string> ans, int n, int row){\n\n if(row == n){\n finalans.push_back(ans);\n return...
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 check(vector<pair<int, int>> queens, int i, int j) {\n for(int k = 0; k < queens.size(); ++k) {\n if(queens[k].first == i) {\n return false;\n }\n if(abs(queens[k].first - i) == abs(queens[k].second - j)) {\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<char>> update_vis(vector<vector<char>> vis, int row, int col ,int n)\n {\n \n vector<vector<char>>new_vis=vis;\n for(int i=0;i<n;i++)\n { \n if(i!=col)\n new_vis[row][i]='.';\n if(i!=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 vector<vector<string>> solveNQueens(int n) {\n vector<vector<string>> ans;\n string dots(n, '.');\n vector<string> graph(n, dots);\n vector<vector<bool>> trace(n, vector<bool>(n, true));\n backTrack(ans, graph, trace, n, 0);\n return ...
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<string>> ans;\n string dots(n, '.');\n vector<string> graph(n, dots);\n vector<vector<bool>> trace(n, vector<bool>(n, true));\n backTrack(ans, graph, trace, n, 0);\n return ...
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<string>> ans;\n\n helper(ans, {}, {}, {}, {}, n); \n \n return ans; \n }\nprivate:\n void helper(vector<vector<string>>& ans, vector<string> vec, set<int> cols, set<int> pDia...
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": "/*\n\".Q..\"\n\"...Q\"\n\"Q...\"\n\"..Q.\"\n\n\n[\"..Q.\",\"Q...\",\"...Q\",\".Q..\"]]\n*/\n\nclass Solution {\npublic:\n vector<string> convertToBoard(vector<vector<char>> &state) {\n int n = state.size();\n \n vector<string> board;\n for(int row=0; row<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 vector<vector<string>> solveNQueens(int n) {\n vector <vector <string>> ans;\n\n string s = \"\";\n\n for (int i = 0;i < n;i++) s += '.';\n\n vector <string> board(n, s);\n\n vector <vector <int>> myQ;\n\n dfs(0, board, myQ, ans);\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>> solveNQueens(int n) {\n vector <vector <string>> ans;\n\n string s = \"\";\n\n for (int i = 0;i < n;i++) s += '.';\n\n vector <string> board(n, s);\n\n vector <vector <int>> myQ;\n\n dfs(0, board, myQ, ans);\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 {\n\n bool isSafe(vector<string> positions, int row, int col) {\n \n for (int i = row-1; i >= 0; i--) {\n // same col\n if (positions[i][col] == 'Q') return false;\n\n // right diagonal\n if (col+(row-i) < positions[0].length() ...
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": "// Solution class to solve the N-Queens problem\nclass Solution{\n private:\n int size; // Size of the baord (n x n)\n vector<vector<string>> solutions; // Vector to store all possible solutions\n\n\n // Making use of a helper function to ...
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 int size;\n vector<vector<string>> solutions;\n // Making use of a helper function to get the\n // solutions in the correct output format\n vector<string> createBoard(vector<vector<char>> state) {\n vector<string> board;\n for (int row = 0; row < si...
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 string temp;\n\n void dfs(vector<string> now, vector<vector<bool>> vis, int z, int n){\n if(z==n){ans.push_back(now);return;}\n for(int i=0;i<n;i++){\n if(vis[z][i])continue;\n string s = temp;\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 void solve(vector<vector<string>>&ans,vector<string>t,int n,int row,int col,unordered_map<int,bool>m,unordered_map<int,bool>m1,unordered_map<int,bool>m2){\n if(row>=n){\n ans.push_back(t);\n return;\n }\n for(int col = 0;col<n;col++)...
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<bool>> block(vector<vector<bool>> done, int r, int c, int n) {\n // Block all vertical places\n for (int i = 0; i < n; i++)\n done[i][c] = true;\n\n // Block all horizontal places\n for (int j = 0; j < n; j++)\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<bool>> block(vector<vector<bool>> done, int r, int c, int n) {\n // Block all vertical places\n for (int i = 0; i < n; i++)\n done[i][c] = true;\n\n // Block all horizontal places\n for (int j = 0; j < n; j++)\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<bool>> block(vector<vector<bool>> done, int r, int c, int n) {\n // Block all vertical places\n for (int i = 0; i < n; i++)\n done[i][c] = true;\n\n // Block all horizontal places\n for (int j = 0; j < n; j++)\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>> 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 // cout<<\"i=\"<<row<<\" c=\"<<i<<endl;\n }\n int c=colum...