<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:
    vector<pair<int, int>> d = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    bool dfs(vector<vector<char>>& g, int i, int j, int pi, int pj, int s,
             char c, char r) {
        if (i < 0 or i >= g.size() or j < 0 or j >= g[0].size())
            return 0;
        if (g[i][j] != c and g[i][j] != c - 32)
            return 0;
        if (g[i][j] == c - 32) {
            // cout<<i<<" "<<j<<endl;
            if (s > 3)
                return 1;
            return 0;
        }
        g[i][j] -= 32;
        for (int k = 0; k < 4; ++k) {
            if (i + d[k].first != pi or j + d[k].second != pj)
                if (dfs(g, i + d[k].first, j + d[k].second, i, j, s + 1, c, r))
                    return 1;
        }
        // g[i][j] = c;
        return 0;
    }
    bool containsCycle(vector<vector<char>>& grid) {

        vector<vector<int>> t(grid.size(), vector<int>(grid[0].size(), 0));
        char r = 'A';
        for (int i = 0; i < grid.size(); ++i) {
            for (int j = 0; j < grid[0].size(); ++j) {
                // for (int i = 0; i < grid.size(); ++i) {
                //     for (int j = 0; j < grid[0].size(); ++j)
                //         cout << grid[i][j] << " ";
                //     cout << endl;
                // }
                // cout<<endl;
                if(grid[i][j] >= 'a' and grid[i][j] <= 'z')
                if (dfs(grid, i, j, -1, -1, 0, grid[i][j], r))
                    return 1;
                r = (r + 1) % 26;
            }
        }
        // cout<< 'a' - 'A';
        // for (int i = 0; i < grid.size(); ++i) {
        //     for (int j = 0; j < grid[0].size(); ++j)
        //         cout << grid[i][j] << " ";
        //     cout << endl;
        // }
        return 0;
    }

    // bool dfs(vector<vector<char>>& g, int i, int j, int prev_i, int prev_j,
    //          vector<vector<int>>& visited, char c, int depth) {
    //     // Boundary conditions and character check
    //     if (i < 0 || i >= g.size() || j < 0 || j >= g[0].size()) {
    //         return false;
    //     }

    //     // If the current cell has a different character, no cycle possible
    //     here if (g[i][j] != c) {
    //         return false;
    //     }

    //     // If we revisit a cell and the depth is at least 4, we found a cycle
    //     if (visited[i][j]) {
    //         return depth >= 4;
    //     }

    //     // Mark the cell as visited
    //     visited[i][j] = true;

    //     // Explore all four directions (up, down, left, right)
    //     static vector<pair<int, int>> directions = {
    //         {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    //     for (auto& d : directions) {
    //         int new_i = i + d.first;
    //         int new_j = j + d.second;

    //         // Only continue DFS if the next cell is not the parent
    //         (immediate
    //         // previous cell)
    //         if (new_i != prev_i || new_j != prev_j) {
    //             if (dfs(g, new_i, new_j, i, j, visited, c, depth + 1)) {
    //                 return true; // Cycle found
    //             }
    //         }
    //     }

    //     return false; // No cycle found
    // }

    // bool containsCycle(vector<vector<char>>& grid) {
    //     int rows = grid.size();
    //     int cols = grid[0].size();

    //     vector<vector<int>> visited(
    //         rows, vector<int>(cols, 0)); // To track visited cells

    //     // Loop through each cell of the grid
    //     for (int i = 0; i < rows; ++i) {
    //         for (int j = 0; j < cols; ++j) {
    //             // If the cell has not been visited, start DFS from this cell
    //             if (!visited[i][j]) {
    //                 if (dfs(grid, i, j, -1, -1, visited, grid[i][j],
    //                         1)) {    // Start with depth = 1
    //                     return true; // Cycle found
    //                 }
    //             }
    //         }
    //     }

    //     return false; // No cycle found
    // }
};
70895