<p>You are given an <code>m x n</code> <code>grid</code> where each cell can have one of three values:</p>

<ul>
	<li><code>0</code> representing an empty cell,</li>
	<li><code>1</code> representing a fresh orange, or</li>
	<li><code>2</code> representing a rotten orange.</li>
</ul>

<p>Every minute, any fresh orange that is <strong>4-directionally adjacent</strong> to a rotten orange becomes rotten.</p>

<p>Return <em>the minimum number of minutes that must elapse until no cell has a fresh orange</em>. If <em>this is impossible, return</em> <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/16/oranges.png" style="width: 650px; height: 137px;" />
<pre>
<strong>Input:</strong> grid = [[2,1,1],[1,1,0],[0,1,1]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[2,1,1],[0,1,1],[1,0,1]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,2]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since there are already no fresh oranges at minute 0, the answer is just 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10</code></li>
	<li><code>grid[i][j]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</li>
</ul>

#include <vector>
#include <queue>
using namespace std;

class Solution {
public:
    int orangesRotting(vector<vector<int>>& grid) {
        int rows = grid.size();  // Number of rows in the grid
        int cols = grid[0].size();  // Number of columns in the grid
        int freshOranges = 0;  // Count of fresh oranges
        int minutes = 0;  // Time taken to rot all oranges

        // Step 1: Count all fresh oranges and check for rotten oranges
        // in a less efficient way by not using a queue yet.
        while (true) {
            vector<vector<int>> nextGrid = grid; // Create a copy of the grid for next state
            bool anyOrangeRotted = false; // Keep track if any orange rotted this round

            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    // If the orange is fresh, we increase the fresh orange count
                    if (grid[row][col] == 1) {
                        freshOranges++;
                    }

                    // If the orange is rotten, we check its neighbors (up, down, left, right)
                    if (grid[row][col] == 2) {
                        // Check up
                        if (row > 0 && grid[row - 1][col] == 1) {
                            nextGrid[row - 1][col] = 2;  // Rot the fresh orange
                            anyOrangeRotted = true;  // Track that we rotted an orange
                        }
                        // Check down
                        if (row < rows - 1 && grid[row + 1][col] == 1) {
                            nextGrid[row + 1][col] = 2;
                            anyOrangeRotted = true;
                        }
                        // Check left
                        if (col > 0 && grid[row][col - 1] == 1) {
                            nextGrid[row][col - 1] = 2;
                            anyOrangeRotted = true;
                        }
                        // Check right
                        if (col < cols - 1 && grid[row][col + 1] == 1) {
                            nextGrid[row][col + 1] = 2;
                            anyOrangeRotted = true;
                        }
                    }
                }
            }

            // If no oranges rotted in this round, we're done
            if (!anyOrangeRotted) {
                break;
            }

            // Update the grid to the new state after rotting
            grid = nextGrid;

            // Increment minutes because we completed one round of rotting
            minutes++;
        }

        // Step 2: After rotting, check if there are still fresh oranges left
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (grid[row][col] == 1) {
                    return -1;  // If any fresh orange is left, return -1
                }
            }
        }

        // If all fresh oranges have rotted, return the number of minutes taken
        return minutes;
    }
};

16500