`{"id":"63f2a47c-b736-4b05-8308-57addb423b60","name":"Knights Tour","description":"1. You are given a number n, the size of a chess board.\r\n2. You are given a row and a column, as a starting point for a knight piece.\r\n3. You are required to generate the all moves of a knight starting in (row, col) such that knight visits \r\n all cells of the board exactly once.\r\n4. Complete the body of printKnightsTour function - without changing signature - to calculate and \r\n print all configurations of the chess board representing the route\r\n of knight through the chess board. Use sample input and output to get more idea.\r\n\r\nNote -> When moving from (r, c) to the possible 8 options give first precedence to (r - 2, c + 1) and \r\n move in clockwise manner to\r\n explore other options.\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of \r\n question is. Write recursive and not iterative logic. The purpose of the question is to aid \r\n learning recursion and not test you.","inputFormat":"A number n\r\nA number row\r\nA number col","outputFormat":"All configurations of the chess board representing route of knights through the chess board starting in (row, col)\r\nUse displayBoard function to print one configuration of the board.","constraints":"n = 5\r\n0 &lt;= row &lt; n\r\n0 &lt;= col &lt; n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n//function to display the 2-d array\nvoid display(vector<vector<int>> &chess){\n for(int i=0;i<chess.size();i++){\n for(int j=0;j<chess.size();j++){\n cout << chess[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n\nvoid printKnightsTour(vector<vector<int>> &chess,int n,int r,int c,int upcomingMove){\n //write your code here\n\n\n }\n \nint main(){\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 printKnightsTour(int[][] chess, int r, int c, int upcomingMove) {\r\n \r\n }\r\n\r\n public static void displayBoard(int[][] chess){\r\n for(int i = 0; i < chess.length; i++){\r\n for(int j = 0; j < chess[0].length; j++){\r\n System.out.print(chess[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n System.out.println();\r\n }\r\n}"},"python":{"code":"def displayBoard(chess):\n for i in range(len(chess)):\n for j in range(len(chess)):\n print(chess[i][j],\"\",end='');\n print()\n print()\n \n \ndef printKnightsTour(chess,n,r,c,upcomingMove):\n # write your code here\n\n\n\n\n\n\ndef main():\n n=int(input());\n chess=[];\n for i in range(n):\n a=[];\n for j in range(n):\n a.append(0);\n chess.append(a);\n row=int(input()); \n col=int(input()); \n printKnightsTour(chess,n,row,col,1);\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n2\r\n0","sampleOutput":"25 2 13 8 23 \r\n12 7 24 3 14 \r\n1 18 15 22 9 \r\n6 11 20 17 4 \r\n19 16 5 10 21 \r\n\r\n19 2 13 8 21 \r\n12 7 20 3 14 \r\n1 18 15 22 9 \r\n6 11 24 17 4 \r\n25 16 5 10 23 \r\n\r\n25 2 13 8 19 \r\n12 7 18 3 14 \r\n1 24 15 20 9 \r\n6 11 22 17 4 \r\n23 16 5 10 21 \r\n\r\n19 2 13 8 25 \r\n12 7 18 3 14 \r\n1 20 15 24 9 \r\n6 11 22 17 4 \r\n21 16 5 10 23 \r\n\r\n21 2 17 8 19 \r\n12 7 20 3 16 \r\n1 22 13 18 9 \r\n6 11 24 15 4 \r\n23 14 5 10 25 \r\n\r\n23 2 17 8 25 \r\n12 7 24 3 16 \r\n1 22 13 18 9 \r\n6 11 20 15 4 \r\n21 14 5 10 19 \r\n\r\n25 2 17 8 23 \r\n12 7 24 3 16 \r\n1 18 13 22 9 \r\n6 11 20 15 4 \r\n19 14 5 10 21 \r\n\r\n19 2 17 8 21 \r\n12 7 20 3 16 \r\n1 18 13 22 9 \r\n6 11 24 15 4 \r\n25 14 5 10 23 \r\n\r\n25 2 15 8 19 \r\n16 7 18 3 14 \r\n1 24 11 20 9 \r\n6 17 22 13 4 \r\n23 12 5 10 21 \r\n\r\n19 2 15 8 25 \r\n16 7 18 3 14 \r\n1 20 11 24 9 \r\n6 17 22 13 4 \r\n21 12 5 10 23 \r\n\r\n21 2 15 8 19 \r\n16 7 20 3 14 \r\n1 22 11 18 9 \r\n6 17 24 13 4 \r\n23 12 5 10 25 \r\n\r\n23 2 15 8 25 \r\n16 7 24 3 14 \r\n1 22 11 18 9 \r\n6 17 20 13 4 \r\n21 12 5 10 19 \r\n\r\n23 2 13 8 21 \r\n14 7 22 3 12 \r\n1 24 9 20 17 \r\n6 15 18 11 4 \r\n25 10 5 16 19 \r\n\r\n21 2 13 8 23 \r\n14 7 22 3 12 \r\n1 20 9 24 17 \r\n6 15 18 11 4 \r\n19 10 5 16 25 \r\n\r\n25 2 13 8 19 \r\n14 7 18 3 12 \r\n1 24 9 20 17 \r\n6 15 22 11 4 \r\n23 10 5 16 21 \r\n\r\n19 2 13 8 25 \r\n14 7 18 3 12 \r\n1 20 9 24 17 \r\n6 15 22 11 4 \r\n21 10 5 16 23 \r\n\r\n21 2 11 16 19 \r\n12 17 20 3 10 \r\n1 22 7 18 15 \r\n6 13 24 9 4 \r\n23 8 5 14 25 \r\n\r\n23 2 11 16 25 \r\n12 17 24 3 10 \r\n1 22 7 18 15 \r\n6 13 20 9 4 \r\n21 8 5 14 19 \r\n\r\n23 2 11 16 21 \r\n12 17 22 3 10 \r\n1 24 7 20 15 \r\n6 13 18 9 4 \r\n25 8 5 14 19 \r\n\r\n21 2 11 16 23 \r\n12 17 22 3 10 \r\n1 20 7 24 15 \r\n6 13 18 9 4 \r\n19 8 5 14 25 \r\n\r\n21 2 9 14 19 \r\n10 15 20 3 8 \r\n1 22 5 18 13 \r\n16 11 24 7 4 \r\n23 6 17 12 25 \r\n\r\n23 2 9 14 25 \r\n10 15 24 3 8 \r\n1 22 5 18 13 \r\n16 11 20 7 4 \r\n21 6 17 12 19 \r\n\r\n25 2 9 14 23 \r\n10 15 24 3 8 \r\n1 18 5 22 13 \r\n16 11 20 7 4 \r\n19 6 17 12 21 \r\n\r\n19 2 9 14 21 \r\n10 15 20 3 8 \r\n1 18 5 22 13 \r\n16 11 24 7 4 \r\n25 6 17 12 23 \r\n\r\n23 2 7 12 21 \r\n8 13 22 17 6 \r\n1 24 3 20 11 \r\n14 9 18 5 16 \r\n25 4 15 10 19 \r\n\r\n21 2 7 12 23 \r\n8 13 22 17 6 \r\n1 20 3 24 11 \r\n14 9 18 5 16 \r\n19 4 15 10 25 \r\n\r\n25 2 7 12 23 \r\n8 13 24 17 6 \r\n1 18 3 22 11 \r\n14 9 20 5 16 \r\n19 4 15 10 21 \r\n\r\n19 2 7 12 21 \r\n8 13 20 17 6 \r\n1 18 3 22 11 \r\n14 9 24 5 16 \r\n25 4 15 10 23 \r\n\r\n25 4 15 10 23 \r\n14 9 24 5 16 \r\n1 18 3 22 11 \r\n8 13 20 17 6 \r\n19 2 7 12 21 \r\n\r\n19 4 15 10 21 \r\n14 9 20 5 16 \r\n1 18 3 22 11 \r\n8 13 24 17 6 \r\n25 2 7 12 23 \r\n\r\n25 4 15 10 19 \r\n14 9 18 5 16 \r\n1 24 3 20 11 \r\n8 13 22 17 6 \r\n23 2 7 12 21 \r\n\r\n19 4 15 10 25 \r\n14 9 18 5 16 \r\n1 20 3 24 11 \r\n8 13 22 17 6 \r\n21 2 7 12 23 \r\n\r\n21 6 17 12 19 \r\n16 11 20 7 4 \r\n1 22 5 18 13 \r\n10 15 24 3 8 \r\n23 2 9 14 25 \r\n\r\n23 6 17 12 25 \r\n16 11 24 7 4 \r\n1 22 5 18 13 \r\n10 15 20 3 8 \r\n21 2 9 14 19 \r\n\r\n25 6 17 12 23 \r\n16 11 24 7 4 \r\n1 18 5 22 13 \r\n10 15 20 3 8 \r\n19 2 9 14 21 \r\n\r\n19 6 17 12 21 \r\n16 11 20 7 4 \r\n1 18 5 22 13 \r\n10 15 24 3 8 \r\n25 2 9 14 23 \r\n\r\n25 8 5 14 19 \r\n6 13 18 9 4 \r\n1 24 7 20 15 \r\n12 17 22 3 10 \r\n23 2 11 16 21 \r\n\r\n19 8 5 14 25 \r\n6 13 18 9 4 \r\n1 20 7 24 15 \r\n12 17 22 3 10 \r\n21 2 11 16 23 \r\n\r\n21 8 5 14 19 \r\n6 13 20 9 4 \r\n1 22 7 18 15 \r\n12 17 24 3 10 \r\n23 2 11 16 25 \r\n\r\n23 8 5 14 25 \r\n6 13 24 9 4 \r\n1 22 7 18 15 \r\n12 17 20 3 10 \r\n21 2 11 16 19 \r\n\r\n21 12 5 10 19 \r\n6 17 20 13 4 \r\n1 22 11 18 9 \r\n16 7 24 3 14 \r\n23 2 15 8 25 \r\n\r\n23 12 5 10 25 \r\n6 17 24 13 4 \r\n1 22 11 18 9 \r\n16 7 20 3 14 \r\n21 2 15 8 19 \r\n\r\n23 12 5 10 21 \r\n6 17 22 13 4 \r\n1 24 11 20 9 \r\n16 7 18 3 14 \r\n25 2 15 8 19 \r\n\r\n21 12 5 10 23 \r\n6 17 22 13 4 \r\n1 20 11 24 9 \r\n16 7 18 3 14 \r\n19 2 15 8 25 \r\n\r\n21 14 5 10 19 \r\n6 11 20 15 4 \r\n1 22 13 18 9 \r\n12 7 24 3 16 \r\n23 2 17 8 25 \r\n\r\n23 14 5 10 25 \r\n6 11 24 15 4 \r\n1 22 13 18 9 \r\n12 7 20 3 16 \r\n21 2 17 8 19 \r\n\r\n25 14 5 10 23 \r\n6 11 24 15 4 \r\n1 18 13 22 9 \r\n12 7 20 3 16 \r\n19 2 17 8 21 \r\n\r\n19 14 5 10 21 \r\n6 11 20 15 4 \r\n1 18 13 22 9 \r\n12 7 24 3 16 \r\n25 2 17 8 23 \r\n\r\n23 16 5 10 21 \r\n6 11 22 17 4 \r\n1 24 15 20 9 \r\n12 7 18 3 14 \r\n25 2 13 8 19 \r\n\r\n21 16 5 10 23 \r\n6 11 22 17 4 \r\n1 20 15 24 9 \r\n12 7 18 3 14 \r\n19 2 13 8 25 \r\n\r\n25 16 5 10 23 \r\n6 11 24 17 4 \r\n1 18 15 22 9 \r\n12 7 20 3 14 \r\n19 2 13 8 21 \r\n\r\n19 16 5 10 21 \r\n6 11 20 17 4 \r\n1 18 15 22 9 \r\n12 7 24 3 14 \r\n25 2 13 8 23 \r\n\r\n23 10 5 16 21 \r\n6 15 22 11 4 \r\n1 24 9 20 17 \r\n14 7 18 3 12 \r\n25 2 13 8 19 \r\n\r\n21 10 5 16 23 \r\n6 15 22 11 4 \r\n1 20 9 24 17 \r\n14 7 18 3 12 \r\n19 2 13 8 25 \r\n\r\n25 10 5 16 19 \r\n6 15 18 11 4 \r\n1 24 9 20 17 \r\n14 7 22 3 12 \r\n23 2 13 8 21 \r\n\r\n19 10 5 16 25 \r\n6 15 18 11 4 \r\n1 20 9 24 17 \r\n14 7 22 3 12 \r\n21 2 13 8 23","questionVideo":"https://www.youtube.com/embed/EgoKDqfpbMg","hints":[],"associated":[{"id":"1a94fa6a-a891-4922-bab9-c6394268aaf9","name":"Let the knight is present at some point, say (r, c) on the chess board. Is (r-2, c-1) a valid point to move? (Knights Tour)","slug":"let-the-knight-is-present-at-some-point-say-r-c-on-the-chess-board-is-r-2-c-1-a-valid-point-to-move-knights-tour","type":4},{"id":"2a696e42-b051-4c4c-b154-7efac6693c1d","name":"From a point on chess board, a knight can go in how many directions? (Knights Tour)","slug":"from-a-point-on-chess-board-a-knight-can-go-in-how-many-directions-knights-tour","type":4},{"id":"6a63a3fd-d161-4993-9548-f10dbc1e74d5","name":"Which of the following approaches can be used to solve the Knights Tour problem?","slug":"which-of-the-following-approaches-can-be-used-to-solve-the-knights-tour-problem","type":4},{"id":"ca864e18-f5ec-4ba6-b73c-7459596e0982","name":"If you have a chess board of size 5*5, how many moves the knightmare can take? (Knights Tour)","slug":"if-you-have-a-chess-board-of-size-5-5-how-many-moves-the-knightmare-can-take-knights-tour","type":4},{"id":"e47188ee-1f8a-45cb-98fd-5e3eaa5fdcf8","name":"Which of the following is the base condition for our \"Knights Tour\" problem, if 'r' is the row, 'c' is the column of the cell and a board of size n*n is given?","slug":"which-of-the-following-is-the-base-condition-for-our-knights-tour-problem-if-r-is-the-row-c-is-the-column-of-the-cell-and-a-board-of-size-n-n-is-given","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":"02ab8fe3-cddd-43ad-b241-b8b1a2fcd52e","name":"Graphs For Beginners","slug":"graphs-for-beginners","type":0},{"id":"723c19f6-1ab7-4a87-badf-1c36d5ab5831","name":"Knights Tour","slug":"knights-tour","type":1}],"next":{"id":"d0bfafb3-548c-430a-8034-fa4daae93781","name":"Knights Tour","type":3,"slug":"knights-tour"},"prev":{"id":"d33b0418-358c-4a2b-94a3-08189f022b40","name":"Hamiltonian Path and Cycle","type":3,"slug":"hamiltonian-path-and-cycle"}}}`

# Knights Tour

1. You are given a number n, the size of a chess board. 2. You are given a row and a column, as a starting point for a knight piece. 3. You are required to generate the all moves of a knight starting in (row, col) such that knight visits all cells of the board exactly once. 4. Complete the body of printKnightsTour function - without changing signature - to calculate and print all configurations of the chess board representing the route of knight through the chess board. Use sample input and output to get more idea. Note -> When moving from (r, c) to the possible 8 options give first precedence to (r - 2, c + 1) and move in clockwise manner to explore other options. 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":"63f2a47c-b736-4b05-8308-57addb423b60","name":"Knights Tour","description":"1. You are given a number n, the size of a chess board.\r\n2. You are given a row and a column, as a starting point for a knight piece.\r\n3. You are required to generate the all moves of a knight starting in (row, col) such that knight visits \r\n all cells of the board exactly once.\r\n4. Complete the body of printKnightsTour function - without changing signature - to calculate and \r\n print all configurations of the chess board representing the route\r\n of knight through the chess board. Use sample input and output to get more idea.\r\n\r\nNote -> When moving from (r, c) to the possible 8 options give first precedence to (r - 2, c + 1) and \r\n move in clockwise manner to\r\n explore other options.\r\nNote -> The online judge can't force you to write the function recursively but that is what the spirit of \r\n question is. Write recursive and not iterative logic. The purpose of the question is to aid \r\n learning recursion and not test you.","inputFormat":"A number n\r\nA number row\r\nA number col","outputFormat":"All configurations of the chess board representing route of knights through the chess board starting in (row, col)\r\nUse displayBoard function to print one configuration of the board.","constraints":"n = 5\r\n0 &lt;= row &lt; n\r\n0 &lt;= col &lt; n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n//function to display the 2-d array\nvoid display(vector<vector<int>> &chess){\n for(int i=0;i<chess.size();i++){\n for(int j=0;j<chess.size();j++){\n cout << chess[i][j] << \" \";\n }\n cout << endl;\n }\n cout << endl;\n}\n\nvoid printKnightsTour(vector<vector<int>> &chess,int n,int r,int c,int upcomingMove){\n //write your code here\n\n\n }\n \nint main(){\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 printKnightsTour(int[][] chess, int r, int c, int upcomingMove) {\r\n \r\n }\r\n\r\n public static void displayBoard(int[][] chess){\r\n for(int i = 0; i < chess.length; i++){\r\n for(int j = 0; j < chess[0].length; j++){\r\n System.out.print(chess[i][j] + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n System.out.println();\r\n }\r\n}"},"python":{"code":"def displayBoard(chess):\n for i in range(len(chess)):\n for j in range(len(chess)):\n print(chess[i][j],\"\",end='');\n print()\n print()\n \n \ndef printKnightsTour(chess,n,r,c,upcomingMove):\n # write your code here\n\n\n\n\n\n\ndef main():\n n=int(input());\n chess=[];\n for i in range(n):\n a=[];\n for j in range(n):\n a.append(0);\n chess.append(a);\n row=int(input()); \n col=int(input()); \n printKnightsTour(chess,n,row,col,1);\nmain()"}},"points":10,"difficulty":"easy","sampleInput":"5\r\n2\r\n0","sampleOutput":"25 2 13 8 23 \r\n12 7 24 3 14 \r\n1 18 15 22 9 \r\n6 11 20 17 4 \r\n19 16 5 10 21 \r\n\r\n19 2 13 8 21 \r\n12 7 20 3 14 \r\n1 18 15 22 9 \r\n6 11 24 17 4 \r\n25 16 5 10 23 \r\n\r\n25 2 13 8 19 \r\n12 7 18 3 14 \r\n1 24 15 20 9 \r\n6 11 22 17 4 \r\n23 16 5 10 21 \r\n\r\n19 2 13 8 25 \r\n12 7 18 3 14 \r\n1 20 15 24 9 \r\n6 11 22 17 4 \r\n21 16 5 10 23 \r\n\r\n21 2 17 8 19 \r\n12 7 20 3 16 \r\n1 22 13 18 9 \r\n6 11 24 15 4 \r\n23 14 5 10 25 \r\n\r\n23 2 17 8 25 \r\n12 7 24 3 16 \r\n1 22 13 18 9 \r\n6 11 20 15 4 \r\n21 14 5 10 19 \r\n\r\n25 2 17 8 23 \r\n12 7 24 3 16 \r\n1 18 13 22 9 \r\n6 11 20 15 4 \r\n19 14 5 10 21 \r\n\r\n19 2 17 8 21 \r\n12 7 20 3 16 \r\n1 18 13 22 9 \r\n6 11 24 15 4 \r\n25 14 5 10 23 \r\n\r\n25 2 15 8 19 \r\n16 7 18 3 14 \r\n1 24 11 20 9 \r\n6 17 22 13 4 \r\n23 12 5 10 21 \r\n\r\n19 2 15 8 25 \r\n16 7 18 3 14 \r\n1 20 11 24 9 \r\n6 17 22 13 4 \r\n21 12 5 10 23 \r\n\r\n21 2 15 8 19 \r\n16 7 20 3 14 \r\n1 22 11 18 9 \r\n6 17 24 13 4 \r\n23 12 5 10 25 \r\n\r\n23 2 15 8 25 \r\n16 7 24 3 14 \r\n1 22 11 18 9 \r\n6 17 20 13 4 \r\n21 12 5 10 19 \r\n\r\n23 2 13 8 21 \r\n14 7 22 3 12 \r\n1 24 9 20 17 \r\n6 15 18 11 4 \r\n25 10 5 16 19 \r\n\r\n21 2 13 8 23 \r\n14 7 22 3 12 \r\n1 20 9 24 17 \r\n6 15 18 11 4 \r\n19 10 5 16 25 \r\n\r\n25 2 13 8 19 \r\n14 7 18 3 12 \r\n1 24 9 20 17 \r\n6 15 22 11 4 \r\n23 10 5 16 21 \r\n\r\n19 2 13 8 25 \r\n14 7 18 3 12 \r\n1 20 9 24 17 \r\n6 15 22 11 4 \r\n21 10 5 16 23 \r\n\r\n21 2 11 16 19 \r\n12 17 20 3 10 \r\n1 22 7 18 15 \r\n6 13 24 9 4 \r\n23 8 5 14 25 \r\n\r\n23 2 11 16 25 \r\n12 17 24 3 10 \r\n1 22 7 18 15 \r\n6 13 20 9 4 \r\n21 8 5 14 19 \r\n\r\n23 2 11 16 21 \r\n12 17 22 3 10 \r\n1 24 7 20 15 \r\n6 13 18 9 4 \r\n25 8 5 14 19 \r\n\r\n21 2 11 16 23 \r\n12 17 22 3 10 \r\n1 20 7 24 15 \r\n6 13 18 9 4 \r\n19 8 5 14 25 \r\n\r\n21 2 9 14 19 \r\n10 15 20 3 8 \r\n1 22 5 18 13 \r\n16 11 24 7 4 \r\n23 6 17 12 25 \r\n\r\n23 2 9 14 25 \r\n10 15 24 3 8 \r\n1 22 5 18 13 \r\n16 11 20 7 4 \r\n21 6 17 12 19 \r\n\r\n25 2 9 14 23 \r\n10 15 24 3 8 \r\n1 18 5 22 13 \r\n16 11 20 7 4 \r\n19 6 17 12 21 \r\n\r\n19 2 9 14 21 \r\n10 15 20 3 8 \r\n1 18 5 22 13 \r\n16 11 24 7 4 \r\n25 6 17 12 23 \r\n\r\n23 2 7 12 21 \r\n8 13 22 17 6 \r\n1 24 3 20 11 \r\n14 9 18 5 16 \r\n25 4 15 10 19 \r\n\r\n21 2 7 12 23 \r\n8 13 22 17 6 \r\n1 20 3 24 11 \r\n14 9 18 5 16 \r\n19 4 15 10 25 \r\n\r\n25 2 7 12 23 \r\n8 13 24 17 6 \r\n1 18 3 22 11 \r\n14 9 20 5 16 \r\n19 4 15 10 21 \r\n\r\n19 2 7 12 21 \r\n8 13 20 17 6 \r\n1 18 3 22 11 \r\n14 9 24 5 16 \r\n25 4 15 10 23 \r\n\r\n25 4 15 10 23 \r\n14 9 24 5 16 \r\n1 18 3 22 11 \r\n8 13 20 17 6 \r\n19 2 7 12 21 \r\n\r\n19 4 15 10 21 \r\n14 9 20 5 16 \r\n1 18 3 22 11 \r\n8 13 24 17 6 \r\n25 2 7 12 23 \r\n\r\n25 4 15 10 19 \r\n14 9 18 5 16 \r\n1 24 3 20 11 \r\n8 13 22 17 6 \r\n23 2 7 12 21 \r\n\r\n19 4 15 10 25 \r\n14 9 18 5 16 \r\n1 20 3 24 11 \r\n8 13 22 17 6 \r\n21 2 7 12 23 \r\n\r\n21 6 17 12 19 \r\n16 11 20 7 4 \r\n1 22 5 18 13 \r\n10 15 24 3 8 \r\n23 2 9 14 25 \r\n\r\n23 6 17 12 25 \r\n16 11 24 7 4 \r\n1 22 5 18 13 \r\n10 15 20 3 8 \r\n21 2 9 14 19 \r\n\r\n25 6 17 12 23 \r\n16 11 24 7 4 \r\n1 18 5 22 13 \r\n10 15 20 3 8 \r\n19 2 9 14 21 \r\n\r\n19 6 17 12 21 \r\n16 11 20 7 4 \r\n1 18 5 22 13 \r\n10 15 24 3 8 \r\n25 2 9 14 23 \r\n\r\n25 8 5 14 19 \r\n6 13 18 9 4 \r\n1 24 7 20 15 \r\n12 17 22 3 10 \r\n23 2 11 16 21 \r\n\r\n19 8 5 14 25 \r\n6 13 18 9 4 \r\n1 20 7 24 15 \r\n12 17 22 3 10 \r\n21 2 11 16 23 \r\n\r\n21 8 5 14 19 \r\n6 13 20 9 4 \r\n1 22 7 18 15 \r\n12 17 24 3 10 \r\n23 2 11 16 25 \r\n\r\n23 8 5 14 25 \r\n6 13 24 9 4 \r\n1 22 7 18 15 \r\n12 17 20 3 10 \r\n21 2 11 16 19 \r\n\r\n21 12 5 10 19 \r\n6 17 20 13 4 \r\n1 22 11 18 9 \r\n16 7 24 3 14 \r\n23 2 15 8 25 \r\n\r\n23 12 5 10 25 \r\n6 17 24 13 4 \r\n1 22 11 18 9 \r\n16 7 20 3 14 \r\n21 2 15 8 19 \r\n\r\n23 12 5 10 21 \r\n6 17 22 13 4 \r\n1 24 11 20 9 \r\n16 7 18 3 14 \r\n25 2 15 8 19 \r\n\r\n21 12 5 10 23 \r\n6 17 22 13 4 \r\n1 20 11 24 9 \r\n16 7 18 3 14 \r\n19 2 15 8 25 \r\n\r\n21 14 5 10 19 \r\n6 11 20 15 4 \r\n1 22 13 18 9 \r\n12 7 24 3 16 \r\n23 2 17 8 25 \r\n\r\n23 14 5 10 25 \r\n6 11 24 15 4 \r\n1 22 13 18 9 \r\n12 7 20 3 16 \r\n21 2 17 8 19 \r\n\r\n25 14 5 10 23 \r\n6 11 24 15 4 \r\n1 18 13 22 9 \r\n12 7 20 3 16 \r\n19 2 17 8 21 \r\n\r\n19 14 5 10 21 \r\n6 11 20 15 4 \r\n1 18 13 22 9 \r\n12 7 24 3 16 \r\n25 2 17 8 23 \r\n\r\n23 16 5 10 21 \r\n6 11 22 17 4 \r\n1 24 15 20 9 \r\n12 7 18 3 14 \r\n25 2 13 8 19 \r\n\r\n21 16 5 10 23 \r\n6 11 22 17 4 \r\n1 20 15 24 9 \r\n12 7 18 3 14 \r\n19 2 13 8 25 \r\n\r\n25 16 5 10 23 \r\n6 11 24 17 4 \r\n1 18 15 22 9 \r\n12 7 20 3 14 \r\n19 2 13 8 21 \r\n\r\n19 16 5 10 21 \r\n6 11 20 17 4 \r\n1 18 15 22 9 \r\n12 7 24 3 14 \r\n25 2 13 8 23 \r\n\r\n23 10 5 16 21 \r\n6 15 22 11 4 \r\n1 24 9 20 17 \r\n14 7 18 3 12 \r\n25 2 13 8 19 \r\n\r\n21 10 5 16 23 \r\n6 15 22 11 4 \r\n1 20 9 24 17 \r\n14 7 18 3 12 \r\n19 2 13 8 25 \r\n\r\n25 10 5 16 19 \r\n6 15 18 11 4 \r\n1 24 9 20 17 \r\n14 7 22 3 12 \r\n23 2 13 8 21 \r\n\r\n19 10 5 16 25 \r\n6 15 18 11 4 \r\n1 20 9 24 17 \r\n14 7 22 3 12 \r\n21 2 13 8 23","questionVideo":"https://www.youtube.com/embed/EgoKDqfpbMg","hints":[],"associated":[{"id":"1a94fa6a-a891-4922-bab9-c6394268aaf9","name":"Let the knight is present at some point, say (r, c) on the chess board. Is (r-2, c-1) a valid point to move? (Knights Tour)","slug":"let-the-knight-is-present-at-some-point-say-r-c-on-the-chess-board-is-r-2-c-1-a-valid-point-to-move-knights-tour","type":4},{"id":"2a696e42-b051-4c4c-b154-7efac6693c1d","name":"From a point on chess board, a knight can go in how many directions? (Knights Tour)","slug":"from-a-point-on-chess-board-a-knight-can-go-in-how-many-directions-knights-tour","type":4},{"id":"6a63a3fd-d161-4993-9548-f10dbc1e74d5","name":"Which of the following approaches can be used to solve the Knights Tour problem?","slug":"which-of-the-following-approaches-can-be-used-to-solve-the-knights-tour-problem","type":4},{"id":"ca864e18-f5ec-4ba6-b73c-7459596e0982","name":"If you have a chess board of size 5*5, how many moves the knightmare can take? (Knights Tour)","slug":"if-you-have-a-chess-board-of-size-5-5-how-many-moves-the-knightmare-can-take-knights-tour","type":4},{"id":"e47188ee-1f8a-45cb-98fd-5e3eaa5fdcf8","name":"Which of the following is the base condition for our \"Knights Tour\" problem, if 'r' is the row, 'c' is the column of the cell and a board of size n*n is given?","slug":"which-of-the-following-is-the-base-condition-for-our-knights-tour-problem-if-r-is-the-row-c-is-the-column-of-the-cell-and-a-board-of-size-n-n-is-given","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":"02ab8fe3-cddd-43ad-b241-b8b1a2fcd52e","name":"Graphs For Beginners","slug":"graphs-for-beginners","type":0},{"id":"723c19f6-1ab7-4a87-badf-1c36d5ab5831","name":"Knights Tour","slug":"knights-tour","type":1}],"next":{"id":"d0bfafb3-548c-430a-8034-fa4daae93781","name":"Knights Tour","type":3,"slug":"knights-tour"},"prev":{"id":"d33b0418-358c-4a2b-94a3-08189f022b40","name":"Hamiltonian Path and Cycle","type":3,"slug":"hamiltonian-path-and-cycle"}}}`

Editor

# Knights Tour

easy

1. You are given a number n, the size of a chess board. 2. You are given a row and a column, as a starting point for a knight piece. 3. You are required to generate the all moves of a knight starting in (row, col) such that knight visits all cells of the board exactly once. 4. Complete the body of printKnightsTour function - without changing signature - to calculate and print all configurations of the chess board representing the route of knight through the chess board. Use sample input and output to get more idea. Note -> When moving from (r, c) to the possible 8 options give first precedence to (r - 2, c + 1) and move in clockwise manner to explore other options. 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

n = 5 0 <= row < n 0 <= col < n

## Format

### Input

A number n A number row A number col

### Output

All configurations of the chess board representing route of knights through the chess board starting in (row, col) Use displayBoard function to print one configuration of the board.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 2 0```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}25 2 13 8 23 12 7 24 3 14 1 18 15 22 9 6 11 20 17 4 19 16 5 10 21 19 2 13 8 21 12 7 20 3 14 1 18 15 22 9 6 11 24 17 4 25 16 5 10 23 25 2 13 8 19 12 7 18 3 14 1 24 15 20 9 6 11 22 17 4 23 16 5 10 21 19 2 13 8 25 12 7 18 3 14 1 20 15 24 9 6 11 22 17 4 21 16 5 10 23 21 2 17 8 19 12 7 20 3 16 1 22 13 18 9 6 11 24 15 4 23 14 5 10 25 23 2 17 8 25 12 7 24 3 16 1 22 13 18 9 6 11 20 15 4 21 14 5 10 19 25 2 17 8 23 12 7 24 3 16 1 18 13 22 9 6 11 20 15 4 19 14 5 10 21 19 2 17 8 21 12 7 20 3 16 1 18 13 22 9 6 11 24 15 4 25 14 5 10 23 25 2 15 8 19 16 7 18 3 14 1 24 11 20 9 6 17 22 13 4 23 12 5 10 21 19 2 15 8 25 16 7 18 3 14 1 20 11 24 9 6 17 22 13 4 21 12 5 10 23 21 2 15 8 19 16 7 20 3 14 1 22 11 18 9 6 17 24 13 4 23 12 5 10 25 23 2 15 8 25 16 7 24 3 14 1 22 11 18 9 6 17 20 13 4 21 12 5 10 19 23 2 13 8 21 14 7 22 3 12 1 24 9 20 17 6 15 18 11 4 25 10 5 16 19 21 2 13 8 23 14 7 22 3 12 1 20 9 24 17 6 15 18 11 4 19 10 5 16 25 25 2 13 8 19 14 7 18 3 12 1 24 9 20 17 6 15 22 11 4 23 10 5 16 21 19 2 13 8 25 14 7 18 3 12 1 20 9 24 17 6 15 22 11 4 21 10 5 16 23 21 2 11 16 19 12 17 20 3 10 1 22 7 18 15 6 13 24 9 4 23 8 5 14 25 23 2 11 16 25 12 17 24 3 10 1 22 7 18 15 6 13 20 9 4 21 8 5 14 19 23 2 11 16 21 12 17 22 3 10 1 24 7 20 15 6 13 18 9 4 25 8 5 14 19 21 2 11 16 23 12 17 22 3 10 1 20 7 24 15 6 13 18 9 4 19 8 5 14 25 21 2 9 14 19 10 15 20 3 8 1 22 5 18 13 16 11 24 7 4 23 6 17 12 25 23 2 9 14 25 10 15 24 3 8 1 22 5 18 13 16 11 20 7 4 21 6 17 12 19 25 2 9 14 23 10 15 24 3 8 1 18 5 22 13 16 11 20 7 4 19 6 17 12 21 19 2 9 14 21 10 15 20 3 8 1 18 5 22 13 16 11 24 7 4 25 6 17 12 23 23 2 7 12 21 8 13 22 17 6 1 24 3 20 11 14 9 18 5 16 25 4 15 10 19 21 2 7 12 23 8 13 22 17 6 1 20 3 24 11 14 9 18 5 16 19 4 15 10 25 25 2 7 12 23 8 13 24 17 6 1 18 3 22 11 14 9 20 5 16 19 4 15 10 21 19 2 7 12 21 8 13 20 17 6 1 18 3 22 11 14 9 24 5 16 25 4 15 10 23 25 4 15 10 23 14 9 24 5 16 1 18 3 22 11 8 13 20 17 6 19 2 7 12 21 19 4 15 10 21 14 9 20 5 16 1 18 3 22 11 8 13 24 17 6 25 2 7 12 23 25 4 15 10 19 14 9 18 5 16 1 24 3 20 11 8 13 22 17 6 23 2 7 12 21 19 4 15 10 25 14 9 18 5 16 1 20 3 24 11 8 13 22 17 6 21 2 7 12 23 21 6 17 12 19 16 11 20 7 4 1 22 5 18 13 10 15 24 3 8 23 2 9 14 25 23 6 17 12 25 16 11 24 7 4 1 22 5 18 13 10 15 20 3 8 21 2 9 14 19 25 6 17 12 23 16 11 24 7 4 1 18 5 22 13 10 15 20 3 8 19 2 9 14 21 19 6 17 12 21 16 11 20 7 4 1 18 5 22 13 10 15 24 3 8 25 2 9 14 23 25 8 5 14 19 6 13 18 9 4 1 24 7 20 15 12 17 22 3 10 23 2 11 16 21 19 8 5 14 25 6 13 18 9 4 1 20 7 24 15 12 17 22 3 10 21 2 11 16 23 21 8 5 14 19 6 13 20 9 4 1 22 7 18 15 12 17 24 3 10 23 2 11 16 25 23 8 5 14 25 6 13 24 9 4 1 22 7 18 15 12 17 20 3 10 21 2 11 16 19 21 12 5 10 19 6 17 20 13 4 1 22 11 18 9 16 7 24 3 14 23 2 15 8 25 23 12 5 10 25 6 17 24 13 4 1 22 11 18 9 16 7 20 3 14 21 2 15 8 19 23 12 5 10 21 6 17 22 13 4 1 24 11 20 9 16 7 18 3 14 25 2 15 8 19 21 12 5 10 23 6 17 22 13 4 1 20 11 24 9 16 7 18 3 14 19 2 15 8 25 21 14 5 10 19 6 11 20 15 4 1 22 13 18 9 12 7 24 3 16 23 2 17 8 25 23 14 5 10 25 6 11 24 15 4 1 22 13 18 9 12 7 20 3 16 21 2 17 8 19 25 14 5 10 23 6 11 24 15 4 1 18 13 22 9 12 7 20 3 16 19 2 17 8 21 19 14 5 10 21 6 11 20 15 4 1 18 13 22 9 12 7 24 3 16 25 2 17 8 23 23 16 5 10 21 6 11 22 17 4 1 24 15 20 9 12 7 18 3 14 25 2 13 8 19 21 16 5 10 23 6 11 22 17 4 1 20 15 24 9 12 7 18 3 14 19 2 13 8 25 25 16 5 10 23 6 11 24 17 4 1 18 15 22 9 12 7 20 3 14 19 2 13 8 21 19 16 5 10 21 6 11 20 17 4 1 18 15 22 9 12 7 24 3 14 25 2 13 8 23 23 10 5 16 21 6 15 22 11 4 1 24 9 20 17 14 7 18 3 12 25 2 13 8 19 21 10 5 16 23 6 15 22 11 4 1 20 9 24 17 14 7 18 3 12 19 2 13 8 25 25 10 5 16 19 6 15 18 11 4 1 24 9 20 17 14 7 22 3 12 23 2 13 8 21 19 10 5 16 25 6 15 18 11 4 1 20 9 24 17 14 7 22 3 12 21 2 13 8 23```

Question Video

Discussions

Show Discussion

Related Resources