<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p>

<p>The distance between two adjacent cells is <code>1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[0,0,0]]
<strong>Output:</strong> [[0,0,0],[0,1,0],[0,0,0]]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]]
<strong>Output:</strong> [[0,0,0],[0,1,0],[1,2,1]]
</pre>

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

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>4</sup></code></li>
	<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
	<li>There is at least one <code>0</code> in <code>mat</code>.</li>
</ul>

class Solution {
public:
    /*
        Given:
            mat: Array of binary digits
                1 <= M, N <= 1E4
                1 <= M*N <= 1E4
                Exist i,j s.t. mat[i][j] = 0            

        Return:
            Matrix of same size, res[i][j] = distance to closest 0 from cell

        Obs:
        - In result matrix, every cell is defined
        - 0 <= res[i][j] <= M+N-2

        Bruteforce: 
        Time: O((M*N)^2)
        Space: O(M*N)

        Time: O(M*N)
        Space: O(M*N)
    */
    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
        queue<Point> q {};
        std::set<Point> s {};

        const int M = mat.size();
        const int N = mat[0].size();
        vector<vector<int>> res = get_vec(M, N);

        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                if (mat[i][j] == 0)
                {
                    q.push({i, j});
                    s.insert({i, j});
                }
            }
        }

        while (!q.empty())
        {
            Point p = q.front();
            q.pop();
            const int dist = res[p.x][p.y];

            // Up
            if ((p.x-1 >= 0) && (s.find({p.x-1, p.y}) == s.end()))
            {
                q.push({p.x-1, p.y});
                res[p.x-1][p.y] = dist+1;
                s.insert({p.x-1, p.y});
            }

            // Down
            if ((p.x+1 < M) && (s.find({p.x+1, p.y}) == s.end()))
            {
                q.push({p.x+1, p.y});
                res[p.x+1][p.y] = dist+1;
                s.insert({p.x+1, p.y});
            }

            // Left
            if ((p.y-1 >= 0) && (s.find({p.x, p.y-1}) == s.end()))
            {
                q.push({p.x, p.y-1});
                res[p.x][p.y-1] = dist+1;
                s.insert({p.x, p.y-1});
            }

            // Right
            if ((p.y+1 < N) && (s.find({p.x, p.y+1}) == s.end()))
            {
                q.push({p.x, p.y+1});
                res[p.x][p.y+1] = dist+1;
                s.insert({p.x, p.y+1});
            }

        }

        return res;
    }

private:
    struct Point
    {
        int x;
        int y;

        // Overload < operator for use in std::set
        bool operator<(const Point& other) const {
            return x < other.x || (x == other.x && y < other.y);
        }
    };

    vector<vector<int>> get_vec(int M, int N)
    {
        vector<vector<int>> res {};

        for (size_t i = 0; i < M; i++) {
            vector<int> row {};
            for (size_t j = 0; j < N; j++) {
                row.push_back(0);
            }
            res.push_back(row);
        }

        return res;
    }
};
57644