leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
bfs_floodfill.cpp (648B)
0 // Matrix BFS/Flood fill
2 typedef vector<vector<int>> Matrix;
3 typedef queue<pair<int, int>> Queue;
4 const vector<pair<int, int>> offsets = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
6 int n, m;
7 int valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
9 void dfs(Matrix &mat, int x, int y) {
10 Queue q;
12 q.push({x, y}), mat[x][y] = 2;
13 while (!q.empty()) {
14 auto [a, b] = q.front();
15 q.pop();
16 for (auto [oa, ob] : offsets) {
17 int x = a + oa, y = b + ob;
18 if (!valid(x, y) || mat[x][y] == 0 || mat[x][y] != 1) continue;
19 mat[x][y] = 2;
20 q.push({x, y});
21 }
22 }
23 }