<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:
    struct pair_hash{
        template<class T1, class T2>
        size_t operator () (const pair<T1, T2> &p) const{
            auto p1 = hash<T1> {} (p.first);
            auto p2 = hash<T2> {} (p.second);
            return p1 ^ p2;
        }
    };

    int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
        // Use pairs for everything.
        // Use deque pair, unordered set pair and vector pair
        // Since these are long, make variables for the grid lengths and also the sum of the individual neighbours.
        //Make sure to adjust for cases that does not start from 0,0 and those where there is no clear solution. 
        int ROWS = grid.size();
        int COLS = grid[0].size();
        unordered_set<pair<int, int>, pair_hash> visit;
        deque<pair<int,int>> queue;
        if(grid[0][0] != 0){
            return -1;
        }else{
            visit.insert({0,0});
            queue.push_back({0,0});
        }
        int length = 1;
        while(queue.size()){
            int qlen = queue.size();
            for(int i = 0; i < qlen; i++){
                int r = queue.front().first;
                int c = queue.front().second;
                queue.pop_front();
                if(r == ROWS -1 && c == COLS -1){
                    return length;
                }
                vector<pair<int,int>> neighbours = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
                for(int j = 0; j < neighbours.size();j++ ){
                    int dr = r + neighbours[j].first;
                    int dc = c + neighbours[j].second;
                    if(dr < 0 || dc < 0 || dr == ROWS || dc == COLS || grid[dr][dc] == 1 || visit.find({dr,dc}) != visit.end()){
                        continue;
                    }
                    visit.insert({dr,dc});
                    queue.push_back({dr,dc});
                }
            }
            length++;
        }
        return -1;
    }
};
57653