Advertisement
Guest User

Untitled

a guest
Jul 21st, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define pb push_back
  7.  
  8. ll n, m;
  9. char a[100][100];
  10. bool b[100][100];
  11.  
  12. bool bfs(ll i, ll j)
  13. {
  14. queue<pair<ll, ll>> q;
  15. q.push({i, j});
  16. b[i][j] = 1;
  17.  
  18. while (!q.empty())
  19. {
  20. ll f = q.front().first;
  21. ll s = q.front().second;
  22.  
  23. q.pop();
  24.  
  25. if (s < m - 1)
  26. {
  27. if (a[f][s + 1] == a[i][j])
  28. {
  29. if (!b[f][s + 1])
  30. {
  31. b[f][s + 1] = 1;
  32. q.push({f, s + 1});
  33. }
  34. else
  35. {
  36. return 1;
  37. }
  38. }
  39. }
  40.  
  41. if (f < n - 1)
  42. {
  43. if (a[f + 1][s] == a[i][j])
  44. {
  45. if (!b[f + 1][s])
  46. {
  47. b[f + 1][s] = 1;
  48. q.push({f + 1, s});
  49. }
  50. else
  51. {
  52. return 1;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. return 0;
  59. }
  60.  
  61. int main()
  62. {
  63. cin >> n >> m;
  64.  
  65. memset(a, 0, sizeof(a[0][0]) * 100 * 100);
  66. memset(b, 0, sizeof(b[0][0]) * 100 * 100);
  67.  
  68. for (int i = 0; i < n; i++)
  69. {
  70. for (int j = 0; j < m; j++)
  71. {
  72. cin >> a[i][j];
  73. }
  74. }
  75.  
  76. for (int i = 0; i < n; i++)
  77. {
  78. for (int j = 0; j < m; j++)
  79. {
  80. if (!b[i][j])
  81. {
  82. bool f = bfs(i, j);
  83. if (f)
  84. {
  85. cout << "Yes" << endl;
  86. return 0;
  87. }
  88. }
  89. }
  90. }
  91.  
  92. cout << "No" << endl;
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement