Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- #define si(a) scanf("%d",&a)
- #define MAX 1005
- int X[]= {1,-1,0,0};
- int Y[]= {0,0,1,-1};
- int vis[MAX][MAX];
- char str[MAX][MAX];
- bool is_circle;
- void go(int nowx,int nowy,int fromx,int fromy)
- {
- if(vis[nowx][nowy])
- {
- is_circle=true;
- return ;
- }
- vis[nowx][nowy]=1;
- int i;
- for(i=0; i<4; i++)
- {
- int tox=nowx+X[i],toy=nowy+Y[i];
- if((tox==fromx && toy==fromy) || str[nowx][nowy]!=str[tox][toy])
- continue;
- go(tox,toy,nowx,nowy);
- }
- return ;
- }
- int main()
- {
- int n,m,i,j;
- si(n);
- si(m);
- for(i=0; i<n; i++)
- scanf("%s",str[i]);
- for(i=0; i<n; i++)
- {
- for(j=0; j<m; j++)
- {
- if(vis[i][j])
- continue;
- go(i,j,-1,-1);
- }
- }
- if(is_circle)
- printf("Yes\n");
- else
- printf("No\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement