You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) ) . No recursion, no container needed for alredy seen cells.
int connectedCell(std::vector<std::vector<int>> & _m){ using namespace std; assert(_m.size() > 0 && _m.front().size() > 0); static const auto connected = vector<pair<int, int>>( {{0, 0},{-1, 0},{-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}); auto const rows = static_cast<int>(_m.size()); auto const cols = static_cast<int>(_m.front().size()); auto inRegion = vector<pair<int, int>>(); inRegion.reserve(82); auto maxRegion = 0; auto rowNum = 0; for_each(begin(_m), end(_m), [&](auto & _row){ auto _colNum = 0; for_each(begin(_row), end(_row), [&](auto & _cell){ if(_cell != 1){ ++_colNum; return; } auto currentRegion = 0; inRegion.emplace_back(rowNum, _colNum); for(size_t current = 0; current != inRegion.size(); ++current){ for_each(begin(connected), end(connected), [&](auto & _loc){ auto rowNew = inRegion.at(current).first + _loc.first, colNew = inRegion.at(current).second + _loc.second; if( !(0 <= rowNew && rowNew < rows && 0 <= colNew && colNew < cols && _m.at(rowNew).at(colNew) == 1) ){ return; } ++_m.at(rowNew).at(colNew); ++currentRegion; inRegion.emplace_back(rowNew, colNew); }); } maxRegion = max(currentRegion, maxRegion); inRegion.clear(); ++_colNum; }); ++rowNum; }); return maxRegion; }
Seems like cookies are disabled on this browser, please enable them to open this website
Connected Cells in a Grid
You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) ) . No recursion, no container needed for alredy seen cells.