-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path510B.cpp
More file actions
84 lines (49 loc) · 1.18 KB
/
510B.cpp
File metadata and controls
84 lines (49 loc) · 1.18 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
int n,m;
string grafo[100];
bool passou[100][100];
int mx[] = {-1,1,0,0};
int my[] = {0,0,-1,1};
bool dentro(int y,int x){
if(y >= 0 && y < n && x >= 0 && x < m) return true;
return false;
}
bool dfs(int x,int y,int px,int py){
passou[y][x] = true;
for(int i=0;i<4;i++){
int adjx = x + mx[i];
int adjy = y + my[i];
if(dentro(adjy,adjx) && !passou[adjy][adjx] && grafo[adjy][adjx]==grafo[y][x]){
if(dfs(adjx,adjy,x,y)) return true;
}else if(dentro(adjy,adjx) && passou[adjy][adjx] && grafo[adjy][adjx]==grafo[y][x] && (adjy!=py && adjx!=px)){
return true;
}
}
return false;
}
int main(){
cin >> n >> m;
for(int i=0;i<n;i++){
cin >> grafo[i];
}
bool tem_ciclo = false;
for(int i=0;i<n;i++){
for(int c=0;c<m;c++){
if(!passou[i][c]){
if(dfs(c,i,-1,-1)){
//cout << "DFS(" << i << "," << c << ",-1,-1)\n";
tem_ciclo = true;
c=m;
i=n;
}
}
}
}
if(tem_ciclo){
cout << "Yes\n";
}else{
cout << "No\n";
}
return 0;
}