Also known as “Min hours to send file to all available servers”.

Description:

Input:
[[0, 1, 1, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 1],
[0, 1, 0, 0, 0]]
Output: 2Explanation:
At the end of the 1st hour, the status of the grid:
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 0, 1, 1],
[1, 1, 1, 0, 1]]
At the end of the 2nd hour, the status of the grid:
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]

Solution:

struct node_t { int row = 0, col = 0; };int min_hours(vector<vector<int>> &matrix) {
int hours = -1; // return -1 in case of error
int directions[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; size_t rows = matrix.size(); // to prevent access error when rows == 0
size_t cols = rows ? matrix[0].size() : 0;
// collect all positions of the zombies into a queue
queue<node_t> q;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 1) { q.push({i, j}); }
}
}
// Do BFS until the end of the humans
while (!q.empty()) {
int q_size = q.size();
// Process all positions which are already
// added into the queue.
for (int i = 0; i < q_size; i++) {
auto node = q.front();
q.pop();
// Find all humans in adjacent cells
// and add them into the queue
for (auto dir: directions) {
int nr = node.row + dir[0];
int nc = node.col + dir[1];
if (nr >= 0 && nr < rows && nc >= 0 &&
nc < cols && matrix[nr][nc] == 0) {
q.push({nr, nc});
matrix[nr][nc] = 1;
}
}
}
hours++;
}
return hours;
}

Looking for an interesting project to join. https://www.linkedin.com/in/molchevsky/