Advertisement
UMME_RUKAYA13

Knights in FEN

Dec 6th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1.  
  2. Conversation opened. 1 read message.
  3.  
  4. Skip to content
  5. Using Daffodil International University Mail with screen readers
  6. 9 of 1,417
  7. Fwd: Backtracking Codes
  8. Inbox
  9. x
  10. Md. Erfanul Islam Bhuiyan 181-15-10777 <erfanul15-10777@diu.edu.bd>
  11.  
  12. AttachmentsDec 5, 2018, 6:17 PM (1 day ago)
  13.  
  14. to me
  15.  
  16.  
  17. ---------- Forwarded message ---------
  18. From: Nazmus Sakib 181-15-10817 <nazmus15-10817@diu.edu.bd>
  19. Date: Tue, Dec 4, 2018 at 5:22 PM
  20. Subject: Backtracking Codes
  21. To: <alif15-10730@diu.edu.bd>
  22. Cc: <erfanul15-10777@diu.edu.bd>, <imran15-10627@diu.edu.bd>, <tanima15-10733@diu.edu.bd>, <aziz15-8396@diu.edu.bd>
  23.  
  24.  
  25.  
  26.  
  27. 6 Attachments
  28.  
  29.  
  30.  
  31.  
  32. #include <bits/stdc++.h>
  33. using namespace std;
  34. int x[] = {-2,-2,1,-1,-1,1,2,2};
  35. int y[] = {1,-1,-2,-2,2,2,-1,1};
  36.  
  37. int A[12][12] = {
  38. {0,1,0,1,1},
  39. {1,1,0,-1,1},
  40. {0,1,1,1,0},
  41. {0,1,0,1,0},
  42. {0,0,1,0,0},
  43. };
  44. bool isValid(int row,int col){
  45. if(row < 0 or row > 4 or col < 0 or col > 4) return false;
  46. return true;
  47. }
  48. bool solution(){
  49. if(A[0][0] == 1 and
  50. A[0][1] == 1 and
  51. A[0][2] == 1 and
  52. A[0][3] == 1 and
  53. A[0][4] == 1 and
  54. A[1][1] == 1 and
  55. A[1][2] == 1 and
  56. A[1][3] == 1 and
  57. A[1][4] == 1 and
  58. A[2][3] == 1 and
  59. A[2][4] == 1 and
  60. A[3][4] == 1 and
  61. A[2][2] == -1
  62. ) return true;
  63.  
  64.  
  65. return false;
  66. }
  67. bool Solve(int totalMoves,int movesSoFar,int row,int col,int lastRow, int lastCol){
  68. if(movesSoFar > totalMoves)
  69. return false;
  70. if(solution()){
  71. return true;
  72. }
  73. // cout << totalMoves << endl;
  74. for(int i = 0; i < 8; i++){
  75. int dx = row + x[i];
  76. int dy = col + y[i];
  77. if(dx != lastRow and dy != lastCol and isValid(dx,dy)){
  78. swap(A[dx][dy],A[row][col]);
  79. if(Solve(totalMoves,movesSoFar+1,dx,dy,row,col)) return true;
  80. swap(A[dx][dy],A[row][col]);
  81. }
  82. }
  83. return false;
  84. }
  85. int main()
  86. {
  87. int ans = -1;
  88. for(int totalMoves = 0; totalMoves < 12; totalMoves++){
  89. if(Solve(totalMoves,0,1,3,1,3)){
  90. ans = totalMoves;
  91. break;
  92. }
  93. }
  94. cout << ans << endl;
  95. }
  96.  
  97. KnightMove.cpp
  98. Displaying KnightMove.cpp.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement