<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:
    int m;
    int n;

    struct Position {
        int x;
        int y;

        bool operator==(const Position& other) const {
            return x == other.x && y == other.y;
        }

        bool operator<(const Position& other) const {
            return std::tie(x, y) < std::tie(other.x, other.y);
        }
    
        friend ostream & operator << (ostream &out, const Position &c);
    };



    vector<Position> GetNeighbours(Position& pos) {
        vector<Position> result = {};
        auto& x = pos.x;
        auto& y = pos.y;

        result.reserve(4);

        if (y != m - 1) {
            result.push_back({x, y + 1});
        }

        if (y != 0) {
            result.push_back({x, y - 1});
        }

        if (x != n - 1) {
            result.push_back({x + 1, y});
        }

        if (x != 0) {
            result.push_back({x - 1, y});
        }

        return result;
    }

    bool searchFrom(Position pos, vector<vector<char>>& grid, char goal,
                    std::set<Position>& visited, Position from, std::queue<Position>& seeds) {        
        // cout << "Search on" << pos << " from " << from << " goal: " << goal << " visited: " << visited.size() << endl;

        grid[pos.y][pos.x] = '-';

        auto neighbors = GetNeighbours(pos);

        visited.insert(pos);

        for (auto& neighbor : neighbors) {
            if (neighbor == from) {
                continue;
            }
    
            auto& nb_val = grid[neighbor.y][neighbor.x];

            if (nb_val == goal) {
                bool cycle = searchFrom(neighbor, grid, goal, visited, pos, seeds);
            
                if (cycle) {
                    return true;
                }
        
            } else if (nb_val != '-') {
                seeds.push(neighbor);
                // cout << "Added new seed: " << neighbor << endl;
            } else if (visited.contains(neighbor)) {
                // cout << "Success" <<  std::endl;
                return true;
            }
        }

        return false;
    }

    bool containsCycle(vector<vector<char>>& grid) {
        m = grid.size();
        n = grid[0].size();
        std::queue<Position> seeds;
        seeds.push({0, 0});

        while(!seeds.empty())
        {
            std::set<Position> visited;
            auto seed = seeds.front();
            auto& seed_val = grid[seed.y][seed.x];
            seeds.pop();
            
            if (seed_val == '-') {
                continue;
            }

            bool cycle = searchFrom(seed, grid, seed_val, visited, {-1, -1}, seeds);

            if (cycle) {
                return true;
            }
        }
        


        return false;
    }
};

ostream & operator << (ostream &out, const Solution::Position &c)
{
    out << "(x: " << c.x << ", y: " << c.y << ")";
    return out;
}
172011