Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. //#include<limits.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Queue
  9. {
  10.     int font,rear,siz;
  11.     unsigned capacity;
  12.     int* arr;
  13. };
  14.  
  15. struct Queue* create(unsigned capacity)
  16. {
  17.     struct Queue* que = (struct Queue*) malloc(sizeof(struct Queue));
  18.     que->font=que->siz=0;
  19.     que->rear=capacity-1;
  20.     que->capacity=capacity;
  21.     que->arr = (int*) malloc(que->capacity * sizeof(int));
  22.     return que;
  23. };
  24.  
  25. int isFull(struct Queue* que)
  26. {
  27.     if(que->siz==que->capacity) return 1;
  28.     else return 0;
  29. }
  30.  
  31. int isEmpty(struct Queue* que)
  32. {
  33.     if(que->siz==0) return 1;
  34.     else return 0;
  35. }
  36.  
  37. void push(struct Queue* que, int item)
  38. {
  39.     if(isFull(que)) return;
  40.  
  41.     que->rear = (que->rear+1)%que->capacity;
  42.  
  43.     que->arr[que->rear]=item;
  44.     que->siz= que->siz + 1;
  45.  
  46. }
  47.  
  48. void pop(struct Queue* que)
  49. {
  50.     if(isEmpty(que)) return;
  51.  
  52.     que->font= (que->font + 1)%que->capacity;
  53.     que->siz = que->siz -1;
  54.  
  55. }
  56.  
  57. int font (struct Queue* que)
  58. {
  59.     if (isEmpty(que)) return INT_MIN;
  60.     return que->arr[que->font];
  61. }
  62.  
  63. int rear (struct Queue* que)
  64. {
  65.     if (isEmpty(que)) return INT_MIN;
  66.     return que->arr[que->rear];
  67. }
  68.  
  69. int visited[100];
  70. int mat[100][100];
  71.  
  72. int main()
  73. {
  74.  
  75.  
  76.  
  77.     for(int i=0; i<100; i++) visited[i]=-1;
  78.  
  79.     int n,e;
  80.     cin>>n>>e;
  81.  
  82.     for(int i=1; i<=e; i++)
  83.     {
  84.         int a,b;
  85.         cin>>a>>b;
  86.         mat[a][b]=1;
  87.         mat[b][a]=1;
  88.     }
  89.  
  90.  
  91.     int src,des;
  92.  
  93.     cin>>src>>des;
  94.  
  95.     struct Queue* que = create(100);
  96.  
  97.     push(que,src);
  98.     visited[src]=0;
  99.  
  100.     while(!isEmpty(que))
  101.     {
  102.         int p=font(que);
  103.         pop(que);
  104.  
  105.         for(int i=1; i<=n; i++ )
  106.         {
  107.             int t=i;
  108.  
  109.             if(visited[t]==-1 && mat[p][t]==1 and t!=p)
  110.             {
  111.                 visited[t]=visited[p]+1;
  112.                 push(que,t);
  113.  
  114.                 if(t==des)
  115.                 {
  116.                     cout<<"reachable" << " :"<<visited[t];
  117.                     break;
  118.                 }
  119.             }
  120.         }
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement