<p>Given a 2D array of characters <code>grid</code> of size <code>m x n</code>, you need to find if there exists any cycle consisting of the <strong>same value</strong> in <code>grid</code>.</p>

<p>A cycle is a path of <strong>length 4 or more</strong> in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the <strong>same value</strong> of the current cell.</p>

<p>Also, you cannot move to the cell that you visited in your last move. For example, the cycle <code>(1, 1) -&gt; (1, 2) -&gt; (1, 1)</code> is invalid because from <code>(1, 2)</code> we visited <code>(1, 1)</code> which was the last visited cell.</p>

<p>Return <code>true</code> if any cycle of the same value exists in <code>grid</code>, otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/1.png" style="width: 231px; height: 152px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;]]
<strong>Output:</strong> true
<strong>Explanation: </strong>There are two valid cycles shown in different colors in the image below:
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/11.png" style="width: 225px; height: 163px;" />
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/22.png" style="width: 236px; height: 154px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;c&quot;,&quot;c&quot;,&quot;c&quot;,&quot;a&quot;],[&quot;c&quot;,&quot;d&quot;,&quot;c&quot;,&quot;c&quot;],[&quot;c&quot;,&quot;c&quot;,&quot;e&quot;,&quot;c&quot;],[&quot;f&quot;,&quot;c&quot;,&quot;c&quot;,&quot;c&quot;]]
<strong>Output:</strong> true
<strong>Explanation: </strong>There is only one valid cycle highlighted in the image below:
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/2.png" style="width: 229px; height: 157px;" />
</pre>

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

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/3.png" style="width: 183px; height: 120px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;z&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;b&quot;,&quot;a&quot;]]
<strong>Output:</strong> false
</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;= 500</code></li>
	<li><code>grid</code> consists only of lowercase English letters.</li>
</ul>

struct hash_pair {
  size_t operator()(const std::pair<int, int>& p) const
    {
        // Hash the first element
        size_t hash_f = std::hash<int>{}(p.first);
        // Hash the second element
        size_t hash_s = std::hash<int>{}(p.second);
        // Combine the two hash values
        return hash_f ^ (hash_s + 0x9e3779b9 + (hash_f << 6) + (hash_f >> 2));
    }
};

using grid = std::vector<std::vector<char>>;
using dir = std::vector<std::pair<int, int>>;
using cell = std::pair<int, int>;
using vi = std::vector<std::vector<bool>>;


class Solution {
private:
    int M;
    int N;
    vi visited;
    dir dirs = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
    bool checkBoundary(int r, int c)
    { return r >= 0 && r < M && c >= 0 && c < N; }
    bool isParent(int r, int c, std::optional<cell> p)
    {
        if(!p.has_value())
          return false;
        return p.value().first == r && p.value().second == c;  
    }
    bool dfs(const grid& g, cell x, std::optional<cell> p, std::unordered_map<cell, int, hash_pair>& acc)
    {
        if(auto it = acc.find(x); it != acc.end())
          if((*it).second >= 2)
              return true;

        bool res = false;
        for(const auto& dr : dirs)
        {
            int r = x.first + dr.first;
            int c = x.second + dr.second;
            if(!checkBoundary(r, c) || 
               (g[r][c] != g[x.first][x.second]) ||
               isParent(r, c, p))
              continue;

            acc[{r, c}] += 1;
            res |= dfs(g, {r, c}, std::make_optional<cell>(x), acc);
        }
        return res;
    }
public:
    bool containsCycle(vector<vector<char>>& g) 
    {
        M = g.size();
        N = g[0].size();
        visited = vi(M, std::vector<bool>(N, false));
        bool ans = false;
        for(int r = 0; r < M; r++)
          for(int c = 0; c < N; c++)
            if(!visited[r][c] && !ans)
            {
               std::unordered_map<cell, int, hash_pair> acc;
               visited[r][c] = true;
               acc[{r, c}] = 1;
               ans |= dfs(g, {r, c}, std::nullopt, acc);
               for(auto a : acc)
                 visited[a.first.first][a.first.second] = true;
            }
        return ans;
    }
};
157214