forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs_map.cpp
32 lines (31 loc) · 1.21 KB
/
bfs_map.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
//DFS on MAP in C++
class DFS_Map {
vector<vector<int> > E;
int R, C;
public:
DFS_Map(int r, int c) {
R = r; C = c;
E = vector<vector<int> >(R + 2, vector<int>(C + 2, 0));
}
void setRC(int r, int c, int w) {
E[r][c] = w;
}
int getRC(int r, int c) {
return E[r][c];
}
void dfs(int rr, int cc, vector<vector<bool> > &visited) {
int i, r, c, v;
visited[rr][cc] = true;
for (i = 0; i < 4; i++) {
r = rr + dr[i]; c = cc + dc[i];
if (E[r][c] && !visited[r][c]) {
dfs(r, c, visited);
}
}
}
void dfsAll() {
vector<vector<bool> > visited = vector<vector<bool> >(R + 2, vector<bool>(C + 2, 0));
int r, c;
for (r = 1; r <= R; r++) for (c = 1; c <= C; c++) if (!visited[r][c]) dfs(r, c, visited);
}
};