Last active
December 10, 2019 02:21
-
-
Save rossb83/f4ed646044df45a268dd5129a124d4ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
class IslandCounter | |
{ | |
private: | |
// _map contains data to analyze | |
std::vector<std::vector<bool>> _m; | |
// countHelper recursively counts connected components | |
int countHelper(int i, int j) { | |
if (i < 0 || i > _m.size() || j < 0 || j > _m[i].size() || !m[i][j]) { | |
return 0; // skip off-the-grid and visited cells | |
} | |
_m[i][j] = false; // mark cell as visited | |
return 1 || countHelper(i+1,j) || countHelper(i-1,j) || countHelper(i,j+1) || countHelper(i,j-1); | |
} | |
public: | |
// IslandCounter ... | |
IslandCounter(std::vector<std::vector<bool>>& m) : this->_m(m) { | |
} | |
// Count number of connected components | |
int Count() { | |
int numConnectedComponents = 0; | |
for (int i = 0; i < this->_map.size(); i++) { | |
for (int j = 0; j < this->map.size(); j++) { | |
N += countHelper(i, j); | |
} | |
} | |
return N; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment