-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSstack.cpp
More file actions
55 lines (51 loc) · 1.01 KB
/
DFSstack.cpp
File metadata and controls
55 lines (51 loc) · 1.01 KB
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
53
54
55
#include <bits/stdc++.h>
#include <stack>
#define MAX 100
using namespace std;
class Dothi{
int n;
int a[MAX][MAX]; // matranke
public :
bool chuaxet[MAX];
void readdata();
void init();
void DFS(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 :: DFS(int u){
stack <int> st;
cout <<u<<" ";
chuaxet[u]=false;
st.push(u);
while(!st.empty()){
int s= st.top();
st.pop();
for(int v=1; v<=n; v++){
if(a[s][v]==1 && chuaxet[v]==true){
cout <<v<<" ";
chuaxet[v]=false;
st.push(s);
st.push(v);
break;
}
}
}
}
int main(){
Dothi G;
G.init();
G.readdata();
G.DFS(1);
}