clueless coding // TODO: be smarter

LeetCode Walls and Gates: CPP Solution



TL;DR Code Solution



void dfs(vector<vector<int>>& rooms, int i, int j, int depth) {
    if (i < 0 || i >= rooms.size() || j < 0 || j >= rooms[0].size() || rooms[i][j] < depth) {
        return;
    }

    rooms[i][j] = depth;

    dfs(rooms, i + 1, j, depth + 1);
    dfs(rooms, i - 1, j, depth + 1);
    dfs(rooms, i, j + 1, depth + 1);
    dfs(rooms, i, j - 1, depth + 1);

}

void wallsAndGates(vector<vector<int>>& rooms) {

    for (int i = 0; i < rooms.size(); i++) {
        for (int j = 0; j < rooms[0].size(); j++) {
            if (rooms[i][j] == 0) {
                dfs(rooms, i, j, 0);
            }    
        }
    }

}