Advertisement
TeslaCoilGirl

isSort

Dec 9th, 2021
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. // initialize px and py to 0
  2. int isSafe(board[8][8], int x, int y, int px, int py) {
  3.    
  4. if(x<0||y<0||x==8||y==8){
  5. return 0;
  6. }
  7.  
  8. if(board[x][y]==1){
  9. return 1;
  10. }
  11. // Gets called on initial iteration. Finds number of queens in the way.
  12.  
  13. if(px==0&&py==0){
  14.     return isSafe(board[8][8],x-1,y-1,-1,-1) + isSafe(board[8][8],x,y-1,0,-1) + isSafe(board[8][8],x+1,y-1,1,-1) + isSafe(board[8][8],x-1,y,-1,0) + isSafe(board[8][8],x+1,y,1,0) + isSafe(board[8][8],x+1,y+1,1,1) + isSafe(board[8][8],x,y+1,0,1) + isSafe(board[8][8],x-1,y+1,-1,1);
  15. }
  16. //x-1,y-1
  17. if(px=-1&&py=-1){
  18. return isSafe(board[8][8],x-1,y-1,-1,-1);
  19. }
  20. // x,y-1
  21. if(px==0&&py==-1){
  22.     return isSafe(board[8][8],x,y-1,0,-1);
  23. }
  24. // x+1,y-1
  25. if(px==1&&py==-1){
  26.     return isSafe(board[8][8],x+1,y-1,1,-1);
  27. }
  28.  
  29. // x+1,y
  30. if(px==1&&py==0){
  31.     return isSafe(board[8][8],x+1,y,1,0);
  32. }
  33. // x-1,y
  34. if(px==-1&&py==0){
  35.     return isSafe(board[8][8],x-1,y,-1,0);
  36.    
  37. }
  38. // x+1,y+1
  39. if(px==1&&py==1){
  40.     return isSafe(board[8][8],x+1,y+1,1,1);
  41. }
  42. // x, y+1
  43. if(px==0&&py==1){
  44.     return isSafe(board[8][8],x,y+1,0,1);
  45. }
  46. // x-1, y+1
  47. if(px==-1&&py==1){
  48.     return isSafe(board[8][8],x-1,y+1,-1,1);
  49. }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement