Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // C program for Knight Tour problem
  2. #include<stdio.h>
  3. #include<stdbool.h>
  4. #define N 3
  5. void printSolution(int sol[N][N],int i,int j,int *count);
  6.  
  7. bool isValid(int i, int j);
  8.  
  9. /* A utility function to check if i,j are valid indexes
  10. for N*N chessboard */
  11. int main(){
  12. int sol[N][N]={{1,2,3},{3,4,3},{3,4,3}};
  13. int count =0;
  14. printSolution(sol,0,0,&count);
  15. printf("%d",count);
  16. }
  17.  
  18. bool isValid(int i, int j){
  19.  
  20. return !((i>=N)||(i<0)||(j>=N)||(j<0));
  21.  
  22. }
  23.  
  24.  
  25. /* A utility function to print solution matrix sol[N][N] */
  26. void printSolution(int sol[N][N],int i,int j,int *count)
  27. {
  28.  
  29. if((i==N-1)&&(j==N-1)){
  30. (*count)++;
  31. return;
  32. }
  33. if(isValid(i, j+1))
  34. printSolution(sol, i, j+1, count);
  35.  
  36. if(isValid(i+1, j))
  37. printSolution(sol, i+1, j, count);
  38.  
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement