<p>Given an <code>n x n</code> binary matrix <code>grid</code>, return <em>the length of the shortest <strong>clear path</strong> in the matrix</em>. If there is no clear path, return <code>-1</code>.</p>

<p>A <strong>clear path</strong> in a binary matrix is a path from the <strong>top-left</strong> cell (i.e., <code>(0, 0)</code>) to the <strong>bottom-right</strong> cell (i.e., <code>(n - 1, n - 1)</code>) such that:</p>

<ul>
	<li>All the visited cells of the path are <code>0</code>.</li>
	<li>All the adjacent cells of the path are <strong>8-directionally</strong> connected (i.e., they are different and they share an edge or a corner).</li>
</ul>

<p>The <strong>length of a clear path</strong> is the number of visited cells of this path.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;" />
<pre>
<strong>Input:</strong> grid = [[0,1],[1,0]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;" />
<pre>
<strong>Input:</strong> grid = [[0,0,0],[1,1,0],[1,1,0]]
<strong>Output:</strong> 4
</pre>

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

<pre>
<strong>Input:</strong> grid = [[1,0,0],[1,1,0],[1,1,0]]
<strong>Output:</strong> -1
</pre>

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

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

class Solution {
public:
    int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
        int n = grid.size();
        // Early exit for small grids
        if (n == 1) 
            return grid[0][0] == 0 ? 1 : -1;

        // Firstly, we need to check that the start and target cells are open.
        if (grid[0][0] != 0 || 
            grid[grid.size() - 1][grid[0].size() - 1] != 0) {
                return -1;
        }

        // Set up the BFS.
        queue<array<int, 3>> q;
        vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size(), false)); // Used as visited set.

        q.push({0, 0, 1}); // Put distance on the queue
        visited[0][0] = true;

        // Carry out the BFS
        while (!q.empty()) {
            auto [row, col, distance] = q.front();
            q.pop();

            // Check if this is the target cell.
            if (row == n - 1 && col == n - 1) {
                return distance;
            }

            for (const auto& neighbour : getNeighbours(row, col, grid)) {
                int neighbourRow = neighbour[0];
                int neighbourCol = neighbour[1];
                if (visited[neighbourRow][neighbourCol]) {
                    continue;
                }
                visited[neighbourRow][neighbourCol] = true;
                q.push({neighbourRow, neighbourCol, distance + 1});
            }
        }

        // The target was unreachable.
        return -1;
    }

private:
    vector<array<int, 2>> getNeighbours(int row, int col, const vector<vector<int>>& grid) {
        vector<array<int, 2>> neighbours;

        for (const auto& direction : directions) {
            int newRow = row + direction[0];
            int newCol = col + direction[1];

            // Edge Case
            if (newRow < 0 || newCol < 0 || 
                newRow >= grid.size() || newCol >= grid[0].size() || 
                grid[newRow][newCol] != 0) {
                    continue;    
            }

            neighbours.push_back({newRow, newCol});
        }

        return neighbours; 
    }

    vector<vector<int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
};
66164