← Back to LeetCode Notes

994. Rotting Oranges

medium · array · breadth-first-search · matrix · solved 2026.07.28 · 10 sec read

View on LeetCode ↗
C++
class Solution {public:    // int BFS(vector<vector<int>>& grid,    //         int x, int y) { // i did dfs by accident.    //     if (x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size())    //     return 0; // return if nth if (grid[x][y] == 0) { return 0;}    // if (grid[x][y] == 1) {    //         grid[x][y] = 2;    //         return 1 + max({    //             BFS(grid, x + 1, y),    //             BFS(grid, x - 1, y),    //             BFS(grid, x, y + 1),    //             BFS(grid, x, y - 1)    //         });    //     } else {    //         return max({    //             BFS(grid, x + 1, y),    //             BFS(grid, x - 1, y),    //             BFS(grid, x, y + 1),    //             BFS(grid, x, y - 1)    //         });    //     }    // }    int orangesRotting(vector<vector<int>>& grid) {        // minimum number of minutes to pass must mean shortest path. lets        // do bfs        queue<pair<int, int>> q;        int fresh = 0;        for (int i = 0; i < grid.size(); i++) {            for (int j = 0; j < grid[0].size(); j++) {                if (grid[i][j] == 2) {                    q.push({i, j});                }                if (grid[i][j] == 1) {                    fresh++;                }            }        }  // first step was putting all of my stuff into the queue. I was           // trying        // to do BFS on every rotten orange before hand but that would not        // have given the shortest time possible because one inital path        // would have dominated everything else. instead better method is        // to put everything rotten in first and then do bfs over        // everything. I, for some reason, though of BFS as just one method        // but that is incorrect, it can also be a grown kinda mechanism        int timee = 0;        while (!q.empty() && fresh > 0) {            int levelSize =                q.size();  // other mistake I made was I should have                           // realized the boundary between minutes each                           // minute should at max have stoped per level of                           // expansion, with out level size and the for                           // loop, the while loop finishes the queue                           // incorrectly.            // this problem took more than an hour for sure.            for (int i = 0; i < levelSize; i++) {                auto checking = q.front();                q.pop();                int x = checking.first;                int y = checking.second;                for (pair<int, int> maybe :                     {pair<int, int>{x + 1, y}, pair<int, int>{x - 1, y},                      pair<int, int>{x, y - 1},                      pair<int, int>{                          x, y + 1}}) {  // I was sloppy with maybe where i                                         // was using x and y instead of                                         // properly extracting x and y.                    if (maybe.first < 0 || maybe.second < 0 ||                        maybe.first >= grid.size() ||                        maybe.second >= grid[0].size())                        continue;                    if (grid[maybe.first][maybe.second] == 1) {                        grid[maybe.first][maybe.second] = 2;                        fresh--;                        q.push(maybe);                    }                }            }            timee++;        }        return fresh == 0 ? timee : -1;    }};