Advertisement
Guest User

queuekight

a guest
Nov 11th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #define   size   100
  6.  
  7. typedef struct
  8. {
  9.     // (x, y) represents chess board coordinates
  10.     // distance represent its minimum distance from the source
  11.     int x, y, distance;
  12. } itemtype;
  13.  
  14. struct queuetype
  15. { itemtype items [size];
  16.   int front, rear;
  17. };
  18.  
  19. typedef struct queuetype queue;
  20.  
  21.  
  22. #define N 8
  23.  
  24. // Below arrays details all 8 possible movements
  25. // for a knight
  26. int row[] = { 2, 2, -2, -2, 1, 1, -1, -1 };
  27. int col[] = { -1, 1, 1, -1, 2, -2, 2, -2 };
  28. int visited[8][8] = {{0},{0}, {0},{0}, {0},{0}, {0},{0}};
  29.  
  30. // Check if (x, y) is valid chess board coordinates
  31. // Note that a knight cannot go out of the chessboard
  32.  
  33. bool valid(int x, int y)
  34. {
  35.     if (x < 0 || y < 0 || x >= N || y >= N)
  36.         return false;
  37.     return true;
  38. }
  39.  
  40. // queue node used in BFS
  41.  
  42. int empty(queue q)
  43. {
  44.     return q.front == q.rear;
  45. }
  46.  
  47. int full(queue q)
  48. {
  49.     return q.front == (q.rear+1)%size;
  50. }
  51.  
  52. void clear(queue & q)
  53. {
  54.     q.front = q.rear = 0;
  55. }
  56.  
  57. void insert(queue &q , itemtype i)
  58. {   if (!full(q))
  59.     {  q.rear = (q.rear+1)%size;
  60.        q.items[q.rear] = i;
  61.     }
  62.     else cout << "Queue is full";
  63. }
  64.  
  65. itemtype remove (queue &q)
  66. {   if(!empty(q))
  67.      { q.front = (q.front+1)%size;
  68.        return q.items[q.front];
  69.      }
  70.     else
  71.        cout << "Queue is empty";
  72. }
  73.  
  74.  
  75.  
  76. // Find minimum number of steps taken by the knight
  77. // from source to reach destination using BFS
  78. int BFS(itemtype src, itemtype dest)
  79. {
  80.     // set to check if matrix cell is visited before or not
  81.    
  82. //  visited[src.x][src.y] = 1;
  83.     // create a queue and enqueue first node
  84.     queue q;
  85.     clear(q);
  86.     insert(q, src);
  87.  
  88.     // run till queue is not empty
  89.     while (!empty(q))
  90.     {
  91.         // pop front node from queue and process it
  92.         itemtype node = remove(q);
  93.  
  94.         int x = node.x;
  95.         int y = node.y;
  96.         int distance = node.distance;
  97.        
  98.         // if destination is reached, return distance
  99.         if (x == dest.x && y == dest.y)
  100.             return distance;
  101.  
  102.         // Skip if location is visited before
  103.         if (!visited[node.x][node.y])
  104.         {
  105.             // mark current node as visited
  106.             visited[node.x][node.y] = 1;
  107.  
  108.             // check for all 8 possible movements for a knight
  109.             // and enqueue each valid movement into the queue
  110.             for (int i = 0; i < 8; ++i)
  111.             {
  112.                 // Get the new valid position of Knight from current
  113.                 // position on chessboard and enqueue it in the
  114.                 // queue with +1 distance
  115.                 int x1 = x + row[i];
  116.                 int y1 = y + col[i];
  117.                 int d1 = distance + 1;
  118.                 node.x = x1; node.y = y1; node.distance = d1;
  119.                 if (valid(x1, y1) && !visited[x1][y1])
  120.                     insert(q,node);
  121.             }
  122.         }
  123.     }
  124.  
  125.     // return INFINITY if path is not possible
  126.     return INT_MAX;
  127. }
  128.  
  129. // main function
  130. int main()
  131. {
  132.     // source coordinates
  133.     itemtype src = {0, 7, 0};
  134.    
  135.     // destination coordinates
  136.     itemtype dest = {7, 0, 0};
  137.  
  138.     cout << "Minimum number of steps required is " << BFS(src, dest);
  139.    
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement