-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSqueue.cpp
More file actions
52 lines (48 loc) · 947 Bytes
/
BFSqueue.cpp
File metadata and controls
52 lines (48 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
class Dothi{
int n;
int a[MAX][MAX];
public:
bool chuaxet[MAX];
void readdata();
void init();
void BFS(int u);
};
void Dothi :: init(){
cin >>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin >>a[i][j];
}
}
}
void Dothi :: readdata(){
for(int i=1; i<=n; i++){
chuaxet[i]= true;
}
}
void Dothi :: BFS(int u){
queue <int> Qu;
cout <<u<<" ";
chuaxet[u]= false;
Qu.push(u);
while(!Qu.empty()){
int s= Qu.front();
Qu.pop();
for(int v=1; v<=n; v++){
if(a[s][v]==1 && chuaxet[v]==true){
cout <<v<<" ";
chuaxet[v]=false;
Qu.push(v);
}
}
}
}
int main(){
Dothi D;
D.init();
D.readdata();
D.BFS(2);
}