fork(3) download
  1. #include<stdio.h>
  2. #define N 8
  3.  
  4. int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[],
  5. int yMove[]);
  6.  
  7. /* A utility function to check if i,j are valid indexes for N*N chessboard */
  8. int isSafe(int x, int y, int sol[N][N])
  9. {
  10. if ( x >= 0 && x < N && y >= 0 && y < N && sol[x][y] == -1)
  11. return 1;
  12. return 0;
  13. }
  14.  
  15. /* A utility function to print solution matrix sol[N][N] */
  16. void printSolution(int sol[N][N])
  17. {
  18. for (int x = 0; x < N; x++)
  19. {
  20. for (int y = 0; y < N; y++)
  21. printf(" %2d ", sol[x][y]);
  22. printf("\n");
  23. }
  24. }
  25.  
  26. /* This function solves the Knight Tour problem using Backtracking. This
  27. function mainly uses solveKTUtil() to solve the problem. It returns false if
  28. no complete tour is possible, otherwise return true and prints the tour.
  29. Please note that there may be more than one solutions, this function
  30. prints one of the feasible solutions. */
  31. bool solveKT()
  32. {
  33. int sol[N][N];
  34.  
  35. /* Initialization of solution matrix */
  36. for (int x = 0; x < N; x++)
  37. for (int y = 0; y < N; y++)
  38. sol[x][y] = -1;
  39.  
  40. /* xMove[] and yMove[] define next move of Knight.
  41.   xMove[] is for next value of x coordinate
  42.   yMove[] is for next value of y coordinate */
  43. int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  44. int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  45.  
  46. // Since the Knight is initially at the first block
  47. sol[0][0] = 0;
  48.  
  49. /* Start from 0,0 and explore all tours using solveKTUtil() */
  50. if(solveKTUtil(0, 0, 1, sol, xMove, yMove) == false)
  51. {
  52. printf("Solution does not exist");
  53. return false;
  54. }
  55. else
  56. printSolution(sol);
  57.  
  58. return true;
  59. }
  60.  
  61. /* A recursive utility function to solve Knight Tour problem */
  62. int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[N],
  63. int yMove[N])
  64. {
  65. int k, next_x, next_y;
  66. if (movei == N*N)
  67. return true;
  68.  
  69. /* Try all next moves from the current coordinate x, y */
  70. for (k = 0; k < 8; k++)
  71. {
  72. next_x = x + xMove[k];
  73. next_y = y + yMove[k];
  74. if (isSafe(next_x, next_y, sol))
  75. {
  76. sol[next_x][next_y] = movei;
  77. if (solveKTUtil(next_x, next_y, movei+1, sol, xMove, yMove) == true)
  78. return true;
  79. else
  80. sol[next_x][next_y] = -1;// backtracking
  81. }
  82. }
  83.  
  84. return false;
  85. }
  86.  
  87. /* Driver program to test above functions */
  88. int main()
  89. {
  90. solveKT();
  91. getchar();
  92. return 0;
  93. }
Success #stdin #stdout 0.49s 2684KB
stdin
Standard input is empty
stdout
  0  59  38  33  30  17   8  63 
 37  34  31  60   9  62  29  16 
 58   1  36  39  32  27  18   7 
 35  48  41  26  61  10  15  28 
 42  57   2  49  40  23   6  19 
 47  50  45  54  25  20  11  14 
 56  43  52   3  22  13  24   5 
 51  46  55  44  53   4  21  12