Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define ld long double
- #define pb push_back
- ll n, m;
- char a[100][100];
- bool b[100][100];
- bool bfs(ll i, ll j)
- {
- queue<pair<ll, ll>> q;
- q.push({i, j});
- b[i][j] = 1;
- while (!q.empty())
- {
- ll f = q.front().first;
- ll s = q.front().second;
- q.pop();
- if (s < m - 1)
- {
- if (a[f][s + 1] == a[i][j])
- {
- if (!b[f][s + 1])
- {
- b[f][s + 1] = 1;
- q.push({f, s + 1});
- }
- else
- {
- return 1;
- }
- }
- }
- if (f < n - 1)
- {
- if (a[f + 1][s] == a[i][j])
- {
- if (!b[f + 1][s])
- {
- b[f + 1][s] = 1;
- q.push({f + 1, s});
- }
- else
- {
- return 1;
- }
- }
- }
- }
- return 0;
- }
- int main()
- {
- cin >> n >> m;
- memset(a, 0, sizeof(a[0][0]) * 100 * 100);
- memset(b, 0, sizeof(b[0][0]) * 100 * 100);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cin >> a[i][j];
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- if (!b[i][j])
- {
- bool f = bfs(i, j);
- if (f)
- {
- cout << "Yes" << endl;
- return 0;
- }
- }
- }
- }
- cout << "No" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement