{"id":"34eea277-3274-4d1e-97df-23dfb2a4b992","name":"N Queens","description":"1. You are given a number n, the size of a chess board.\r\n2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another.\r\nNote - Queens kill at distance in all 8 directions\r\n3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens. Use sample input and output to get more idea.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"A number n","outputFormat":"Safe configurations of queens as suggested in sample output","constraints":"1 &lt;= n &lt;= 10","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid printNQueens(vector<vector<int>> chess,string qsf,int row){\n //write your code here\n \n}\n\nint main(){\n int n;\n cin >> n;\n vector<vector<int>> chess(n , vector<int> (n));\n \n printNQueens(chess,\"\",0);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n \r\n }\r\n\r\n public static void printNQueens(int[][] chess, String qsf, int row) {\r\n \r\n }\r\n}"},"python":{"code":"def printNQueens(chess,qsf,row):\n #write your code here\n \n \nchess=[];\nn = int(input());\nfor i in range(n): \n a =[]\n for j in range(n): \n a.append(0);\n chess.append(a)\nprintNQueens(chess,\"\",0);"}},"points":10,"difficulty":"easy","sampleInput":"4","sampleOutput":"0-1, 1-3, 2-0, 3-2, .\r\n0-2, 1-0, 2-3, 3-1, .","questionVideo":"https://www.youtube.com/embed/prZJ0hA43NU","hints":[],"associated":[{"id":"125b24bb-62da-4663-b034-edc03c207b79","name":"(N Queens) If n=1, an imaginary solution for the problem exists?","slug":"n-queens-if-n-1-an-imaginary-solution-for-the-problem-exists","type":4},{"id":"23e468f3-e065-4b9a-ada1-0ed5a8009997","name":"(N Queens) In how many possible directions can a queen attack?","slug":"n-queens-in-how-many-possible-directions-can-a-queen-attack","type":4},{"id":"7b1f0c53-b174-4052-90e8-d7db9769674c","name":"(N Queens) Backtracking in general is used to solve?","slug":"n-queens-backtracking-in-general-is-used-to-solve","type":4},{"id":"95d6b92d-8eff-4e98-bcca-150cf644f593","name":"(N Queens) Which algorithm can be used to solve N queen problem?","slug":"n-queens-which-algorithm-can-be-used-to-solve-n-queen-problem","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"726d3e2f-21fc-4b68-a2eb-45d67cfadd0d","name":"N Queens","slug":"n-queens","type":1}],"next":{"id":"2497f77a-65e3-472a-a59e-2481ef554ea4","name":"N Queens","type":3,"slug":"n-queens"},"prev":{"id":"268b526d-f047-4f45-b4cd-303ff09103bf","name":"Target Sum Subsets","type":3,"slug":"target-sum-subsets"}}}

N Queens

1. You are given a number n, the size of a chess board. 2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another. Note - Queens kill at distance in all 8 directions 3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens. Use sample input and output to get more idea. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

{"id":"34eea277-3274-4d1e-97df-23dfb2a4b992","name":"N Queens","description":"1. You are given a number n, the size of a chess board.\r\n2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another.\r\nNote - Queens kill at distance in all 8 directions\r\n3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens. Use sample input and output to get more idea.\r\n\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"A number n","outputFormat":"Safe configurations of queens as suggested in sample output","constraints":"1 &lt;= n &lt;= 10","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid printNQueens(vector<vector<int>> chess,string qsf,int row){\n //write your code here\n \n}\n\nint main(){\n int n;\n cin >> n;\n vector<vector<int>> chess(n , vector<int> (n));\n \n printNQueens(chess,\"\",0);\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n \r\n }\r\n\r\n public static void printNQueens(int[][] chess, String qsf, int row) {\r\n \r\n }\r\n}"},"python":{"code":"def printNQueens(chess,qsf,row):\n #write your code here\n \n \nchess=[];\nn = int(input());\nfor i in range(n): \n a =[]\n for j in range(n): \n a.append(0);\n chess.append(a)\nprintNQueens(chess,\"\",0);"}},"points":10,"difficulty":"easy","sampleInput":"4","sampleOutput":"0-1, 1-3, 2-0, 3-2, .\r\n0-2, 1-0, 2-3, 3-1, .","questionVideo":"https://www.youtube.com/embed/prZJ0hA43NU","hints":[],"associated":[{"id":"125b24bb-62da-4663-b034-edc03c207b79","name":"(N Queens) If n=1, an imaginary solution for the problem exists?","slug":"n-queens-if-n-1-an-imaginary-solution-for-the-problem-exists","type":4},{"id":"23e468f3-e065-4b9a-ada1-0ed5a8009997","name":"(N Queens) In how many possible directions can a queen attack?","slug":"n-queens-in-how-many-possible-directions-can-a-queen-attack","type":4},{"id":"7b1f0c53-b174-4052-90e8-d7db9769674c","name":"(N Queens) Backtracking in general is used to solve?","slug":"n-queens-backtracking-in-general-is-used-to-solve","type":4},{"id":"95d6b92d-8eff-4e98-bcca-150cf644f593","name":"(N Queens) Which algorithm can be used to solve N queen problem?","slug":"n-queens-which-algorithm-can-be-used-to-solve-n-queen-problem","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"726d3e2f-21fc-4b68-a2eb-45d67cfadd0d","name":"N Queens","slug":"n-queens","type":1}],"next":{"id":"2497f77a-65e3-472a-a59e-2481ef554ea4","name":"N Queens","type":3,"slug":"n-queens"},"prev":{"id":"268b526d-f047-4f45-b4cd-303ff09103bf","name":"Target Sum Subsets","type":3,"slug":"target-sum-subsets"}}}
plane

Editor


Loading...

N Queens

easy

1. You are given a number n, the size of a chess board. 2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another. Note - Queens kill at distance in all 8 directions 3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens. Use sample input and output to get more idea. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

Constraints

1 <= n <= 10

Format

Input

A number n

Output

Safe configurations of queens as suggested in sample output

Example

Sample Input

4

Sample Output

0-1, 1-3, 2-0, 3-2, . 0-2, 1-0, 2-3, 3-1, .

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode