BFS Template

A generic template.

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
struct Coord {
int x;
int y;
};

std::queue<Coord> q; // q contains locations we will visit.

q.push({i,j}); // Add a starting point to the queue.
visited[i][j] = 1; // Mark as visited.

std::vector<int> ds{0, 1, 0, -1, 0}; // In a 2D matrix, this is helpful for iterating four adjacent directions.

while (!q.empty()) {
int size = q.size();
while (size--) { // Iterating all the data in current level.
Coord coords = q.front();
int x = coords.x;
int y = coords.y;
q.pop(); // Remove current node.

for (int i = 0; i < 4; i++) { // Start looking for neighbors
int nx = x + ds[i];
int ny = y + ds[i+1];

// Skip out-of-bound data and visited data.
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (visited[nx][ny] == 1) continue;

// Add new data to the queue and mark as visited
q.push({nx,ny});
visited[nx][ny] = 1;
}
}
}
Author

Joe Chu

Posted on

2024-05-24

Updated on

2024-05-24

Licensed under

Comments