leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

bfs_floodfill_recursive.cpp (568B)


      1 // Matrix BFS/Flood fill Recursive
      2 
      3 typedef vector<vector<int>> Matrix;
      4 typedef vector<vector<bool>> Marked;
      5 const vector<pair<int, int>> offsets = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
      6 
      7 int n, m;
      8 int valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
      9 
     10 bool dfs(const Matrix &mat, Marked &mark, int a, int b) {
     11     mark[a][b] = true;
     12     for (auto [oa, ob] : offsets) {
     13         int x = a + oa, y = b + ob;
     14         if (!valid(x, y) || mark[x][y]) continue;
     15         if (dfs(mat, mark, x, y)) return true;
     16     }
     17     mark[a][b] = false;
     18 
     19     return false;
     20 }