문제 링크: https://www.acmicpc.net/problem/16929
백준 알고리즘 기초 2/2 601에서 1번째 - 16929번 Two Dots를 풀어보았다.
풀이: https://chelseashin.tistory.com/83 를 참고했다.
C++
Python
def dfs(a,b,c,d,e):
dx=(1,0,-1,0)
dy=(0,1,0,-1)
if visit[a][b]:
print("Yes")
exit()
visit[a][b]=True
for i in range(4):
nx=a+dx[i]
ny=b+dy[i]
if not (0<=nx<n and 0<=ny<m) or graph[nx][ny]!=c:
continue
if (nx,ny)==(d,e):
continue
dfs(nx,ny,c,a,b)
n,m=map(int,input().split())
graph=[list(input()) for _ in range(n)]
visit=[[False]*m for i in range(n)]
for i in range(n):
for j in range(m):
if not visit[i][j]:
dfs(i,j,graph[i][j],-1,-1)
print("No")
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
BFS 스페셜 저지 풀이 (0) | 2023.02.07 |
---|---|
서울 지하철 2호선 풀이 (0) | 2023.02.07 |
나이트의 이동 풀이 (0) | 2023.02.07 |
토마토 풀이 (0) | 2023.02.07 |
미로 탐색 풀이 (0) | 2023.02.07 |