Skip to content

Commit

Permalink
2024-08-05 κ²Œμž„
Browse files Browse the repository at this point in the history
  • Loading branch information
seongwon030 committed Aug 5, 2024
1 parent 88f6b71 commit 62321e6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions seongwon030/dfs/κ²Œμž„.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections import deque
import sys
sys.setrecursionlimit(10**6)

input = sys.stdin.readline

dx = [-1,1,0,0]
dy = [0,0,-1,1]

n,m = map(int,input().split())
k = []
for i in range(n):
k.append(list(input().rstrip()))

visited = [[False]*m for _ in range(n)] # λ°©λ¬Έμ—¬λΆ€
dp = [[0]*m for _ in range(n)]

ans = 0
def dfs(x,y,cnt):
global ans
ans = max(ans, cnt)
for i in range(4):
nx = x + int(k[x][y]) * dx[i]
ny = y + int(k[x][y]) * dy[i]
if 0 <= nx < n and 0 <= ny < m and k[nx][ny] != 'H' and cnt+1 > dp[nx][ny]:
if visited[nx][ny]:
print(-1)
exit()
else:
dp[nx][ny] = cnt+1
visited[nx][ny] = True
dfs(nx,ny,cnt+1)
visited[nx][ny] = False

dfs(0,0,0)
print(ans+1)

0 comments on commit 62321e6

Please sign in to comment.