fork(1) download
  1. #include <iostream>
  2. #include <queue>
  3. #include <stdio.h>
  4. #include <memory.h>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int N = 51;
  9. char a[N][N];
  10. int vis[N][N], cost[N][N], n, m, dx[] = { 0, 0, 1, -1 }, dy[] = { 1, -1, 0, 0 };
  11. void bfs (int x, int y)
  12. {
  13. queue <pair < int, int > > q;
  14. q.push(make_pair(x, y));
  15. vis[x][y] = 1;
  16. cost[x][y] = 1;
  17. while (!q.empty())
  18. {
  19. int X = q.front().first, Y = q.front().second;
  20. cout << X << ' ' << Y << endl;
  21. q.pop();
  22. for (int i = 0; i < 4; i++)
  23. {
  24. int nx = dx[i] + X, ny = dy[i] + Y;
  25. if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] != a[X][Y])
  26. continue;
  27. if (abs(cost[X][Y] - cost[nx][ny]) > 2)
  28. {
  29. puts("Yes");
  30. exit (0);
  31. }
  32. else if (vis[nx][ny])
  33. continue;
  34. vis[nx][ny] = 1;
  35. cost[nx][ny] = cost[X][Y] + 1;
  36. q.push(make_pair(nx, ny));
  37. }
  38. }
  39. }
  40. int main() {
  41. scanf("%d%d", &n, &m);
  42. for (int i = 0; i < n; i++)
  43. scanf("%s", &a[i]);
  44. for (int i = 0; i < n; i++)
  45. for (int j = 0; j < m; j++)
  46. {
  47. if (!vis[i][j])
  48. {
  49. memset (cost, 0, sizeof cost);
  50. bfs(i, j);
  51. }
  52. }
  53. puts("No");
  54. return 0;
  55. }
Success #stdin #stdout 0s 3488KB
stdin
3 4
AAAA
ABCA
AAAA
stdout
0 0
0 1
1 0
0 2
Yes