Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- using namespace std;
- int E[10][10];
- int visited[10];
- int parent[10];
- struct queue{
- vector <int> arr;
- int front;
- int size;
- void enqueue(queue &a, int b){
- a.arr.push_back(b);
- a.size++;
- }
- int dequeue(queue &a){
- int x = a.arr[a.front];
- a.front++;
- a.size--;
- return x;
- }
- };
- void bfs(int i){
- for(int j = 1; j < 10; j++){
- visited[j] = 0;
- }
- queue Q;
- Q.front = 0;
- Q.size = 0;
- visited[i] = 1;
- Q.enqueue(Q, i);
- while(Q.size != 0){
- int j = Q.dequeue(Q);
- for(int k = 1; k < 10; k++) {
- if(E[j][k] == 1){
- if(visited[k] == 0){
- visited[k] == 1;
- Q.enqueue(Q, k);
- }
- }
- }
- }
- }
- int main() {
- E[1][2] = 1;
- E[2][1] = 1;
- E[3][1] = 1;
- E[1][3] = 1;
- E[2][4] = 1;
- E[4][2] = 1;
- E[9][8] = 1;
- E[8][9] = 1;
- E[7][6] = 1;
- E[6][7] = 1;
- E[6][3] = 1;
- E[3][6] = 1;
- E[4][6] = 1;
- E[6][4] = 1;
- E[4][5] = 1;
- E[5][4] = 1;
- E[4][8] = 1;
- E[8][4] = 1;
- bfs(1);
- cout << "Reachable points from 1" << endl;
- for(int i = 1; i < 10; i++){
- cout << visited[i] << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment