This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathbfs.cpp
More file actions
68 lines (62 loc) · 1.32 KB
/
Copy pathbfs.cpp
File metadata and controls
68 lines (62 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
char mx[100][100];
bool visited[100][100];
int V,E,k,l,N,M;
bool isvalid(int x,int y)
{
if(x<=N && y<=M && x>=1 && y>=1)
return true;
return false;
}
bool bfs(int x,int y)
{
for(int i=1;i<100;i++)
for(int j=1;j<100;j++)
visited[i][j]=0;
queue<pair<int,int > > q;
q.push({x,y});
visited[x][y]=1;
while(!q.empty())
{
x=q.front().first;
y=q.front().second;
visited[x][y]=true;
q.pop();
if(isvalid(x,y+1) && !visited[x][y+1] && mx[x][y+1]==mx[x][y])
q.push({x,y+1});
if(isvalid(x,y-1) && !visited[x][y-1] && mx[x][y-1]==mx[x][y])
q.push({x,y-1});
if(isvalid(x+1,y) && !visited[x+1][y] && mx[x+1][y]==mx[x][y])
q.push({x+1,y});
if(isvalid(x-1,y) && !visited[x-1][y] && mx[x-1][y]==mx[x][y])
q.push({x-1,y});
int counter=0;
if(visited[x][y+1])
counter++;
if(visited[x][y-1])
counter++;
if(visited[x+1][y])
counter++;
if(visited[x-1][y])
counter++;
if(counter>1)
return true;
}
return false;
}
int main()
{
cin>>N>>M;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
cin>>mx[i][j];
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
if( bfs(i,j)){
cout<<"Yes";
return 0;
}
cout<<"No";
return 0;
}