-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdls.cpp
96 lines (74 loc) · 1.58 KB
/
dls.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*bismillahir~rahmanir~rahim*/
#include <bits/stdc++.h>
#pragma GCC target("popcnt")
using namespace std;
#define ll long long int
#define yes printf("YES\n")
#define no printf("NO\n")
bool DLS (vector<vector<int>>& g,ll start, ll goal , ll limit ){
//if we reached the goal
if(start == goal){
return true;
}
//if we crossed limit
if(limit == 0){
return false;
}
//traversing child nodes
for(auto i : g[start]){
if(DLS(g,i,goal,limit-1)){
return true;
}
}
return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
// taking input the numbers of nodes and edges
cout<<"Enter the number of nodes and edges:\n";
ll n, m;
cin >> n >> m;
// adjacency list
vector<vector<int>> graph(n+1);
// taking input the edges
cout<<"Enter the edges:\n";
for(int i=0;i<m;i++){
ll u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[u].push_back(v);
}
// taking input the start and goal node
cout<<"Enter the start and goal node and limit:\n";
ll goal , limit , start;
cin >> start >> goal >> limit;
if(DLS(graph,start,goal,limit)) cout << "Yes, it is possible to reach the goal \n";
else cout << "No, it isn't possible to reach the goal \n";
}
/*
input 1 :
9 8
1 2
1 3
2 4
2 5
5 8
3 6
6 7
7 9
1 3 2
output 1 : Yes, it is possible to reach the goal
input 2 :
9 8
1 2
1 3
2 4
2 5
5 8
3 6
6 7
7 9
1 9 2
output 1 : No, it isn't possible to reach the goal
*/