Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<stdio.h>
- #include<stdlib.h>
- //#include<limits.h>
- using namespace std;
- struct Queue
- {
- int font,rear,siz;
- unsigned capacity;
- int* arr;
- };
- struct Queue* create(unsigned capacity)
- {
- struct Queue* que = (struct Queue*) malloc(sizeof(struct Queue));
- que->font=que->siz=0;
- que->rear=capacity-1;
- que->capacity=capacity;
- que->arr = (int*) malloc(que->capacity * sizeof(int));
- return que;
- };
- int isFull(struct Queue* que)
- {
- if(que->siz==que->capacity) return 1;
- else return 0;
- }
- int isEmpty(struct Queue* que)
- {
- if(que->siz==0) return 1;
- else return 0;
- }
- void push(struct Queue* que, int item)
- {
- if(isFull(que)) return;
- que->rear = (que->rear+1)%que->capacity;
- que->arr[que->rear]=item;
- que->siz= que->siz + 1;
- }
- void pop(struct Queue* que)
- {
- if(isEmpty(que)) return;
- que->font= (que->font + 1)%que->capacity;
- que->siz = que->siz -1;
- }
- int font (struct Queue* que)
- {
- if (isEmpty(que)) return INT_MIN;
- return que->arr[que->font];
- }
- int rear (struct Queue* que)
- {
- if (isEmpty(que)) return INT_MIN;
- return que->arr[que->rear];
- }
- int visited[100];
- int mat[100][100];
- int main()
- {
- for(int i=0; i<100; i++) visited[i]=-1;
- int n,e;
- cin>>n>>e;
- for(int i=1; i<=e; i++)
- {
- int a,b;
- cin>>a>>b;
- mat[a][b]=1;
- mat[b][a]=1;
- }
- int src,des;
- cin>>src>>des;
- struct Queue* que = create(100);
- push(que,src);
- visited[src]=0;
- while(!isEmpty(que))
- {
- int p=font(que);
- pop(que);
- for(int i=1; i<=n; i++ )
- {
- int t=i;
- if(visited[t]==-1 && mat[p][t]==1 and t!=p)
- {
- visited[t]=visited[p]+1;
- push(que,t);
- if(t==des)
- {
- cout<<"reachable" << " :"<<visited[t];
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement