EPS/11-03-24/c.py

53 lines
1.1 KiB
Python
Raw Normal View History

2024-03-25 15:27:12 +01:00
def main():
n, m = [int(i) for i in input().split()]
data = [[a for a in input()] for _ in range(n)]
colors = set()
for i in data:
for j in i:
colors.add(j)
def voisins(i, j):
col = data[i][j]
v = [
(i-1, j),
(i+1, j),
(i, j-1),
(i, j+1)
]
return [
(a, b) for (a, b) in v
if a >= 0 and b >= 0
and a < n and b < m
and data[a][b] == col
]
vus = set()
def parcours(i, j, pere=None):
vus.add((i, j))
vois = voisins(i, j)
#print(f"voisins de {(i, j)}: {vois}")
for v_ in vois:
if v_ != pere:
if v_ in vus:
return True
vus.add(v_)
if parcours(*v_, pere=(i, j)):
return True
return False
for i in range(n):
for j in range(m):
if (i, j) not in vus:
# print(vus)
if parcours(i, j):
return True
return False
if main():
print("Yes")
else:
print("No")