<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>

// class Solution {
// public:
//     bool path(vector<vector<char>>& g ,  vector<vector<bool>> B , int i , int j , char cur , int si,int sj){
//          if(i < 0 || j < 0 || i >= g.size() || j >= g[0].size() || g[i][j]!=cur){
//             return false;
//         }
//         if(B[i][j] && i != si && j != sj)
//              return false ;
//         return true ;     
//     }
//     void dfs(vector<vector<char>>& g ,  vector<vector<bool>> B , int i , int j ,bool &cycle, char cur , int si , int sj){
//         if(i < 0 || j < 0 || i >= g.size() || j > g[0].size() || g[i][j]!=cur){
//             return ;
//         }
//         if(i == si && j == sj && B[i][j]){
//             cycle = true ;
//             return ;
//         }
//         B[i][j] = true ;
//         if(path(g,B,i+1,j,cur,si,sj))
//                 dfs(g,B,i+1,j,cycle,cur,si,sj);
//         else if(path(g,B,i,j+1,cur,si,sj))
//                 dfs(g,B,i,j+1,cycle,cur,si,sj);        
//         else if(path(g,B,i-1,j,cur,si,sj))
//                 dfs(g,B,i-1,j,cycle,cur,si,sj);
//         else if(path(g,B,i,j-1,cur,si,sj))
//                 dfs(g,B,i,j-1,cycle,cur,si,sj);        
//     }
//     bool containsCycle(vector<vector<char>>& g){
//         vector<vector<bool>> B(g.size(),vector<bool>(g[0].size(),false));
//         for(int i = 0 ; i < g.size() ; i++){
//             for(int j = 0 ; j < g[0].size() ; j++){
//                 if(B[i][j]){
//                 bool cycle = false;
//                 dfs(g,B,i,j,cycle ,g[i][j],i,j);
//                 if(cycle)
//                    return true ;
//                 }   
//             }
//         }
//         return false ;
//     } 
// };

class Solution {
public:
    bool dfs(vector<vector<char>>& g, vector<vector<bool>>& B, int i, int j, int pi, int pj, char cur) {
        if (i < 0 || j < 0 || i >= g.size() || j >= g[0].size() || g[i][j] != cur)
            return false;
        if (B[i][j])  // We've found a visited cell, which indicates a cycle.
            return true;
        
        B[i][j] = true;
        
        // Explore all four directions
        int directions[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        for (auto& dir : directions) {
            int ni = i + dir[0], nj = j + dir[1];
            if (ni == pi && nj == pj) continue;  // Skip the previous cell
            if (dfs(g, B, ni, nj, i, j, cur)) return true;
        }
        
        return false;
    }
    
    bool containsCycle(vector<vector<char>>& g) {
        vector<vector<bool>> B(g.size(), vector<bool>(g[0].size(), false));
        for (int i = 0; i < g.size(); ++i) {
            for (int j = 0; j < g[0].size(); ++j) {
                if (!B[i][j]) {
                    if (dfs(g, B, i, j, -1, -1, g[i][j]))
                        return true;
                }
            }
        }
        return false;
    }
};

98024