dfs, 알파벳 배열 생성 후 방문 체크
import sys
input=sys.stdin.readline
R,C=map(int,input().split())
board=[list(input().strip()) for _ in range(R)]
dirs=[(1,0),(0,1),(-1,0),(0,-1)]
alpha=[0]*26
answer=0
def dfs(row,col,cnt):
global answer
answer=max(answer,cnt)
for i in range(4):
nr,nc=row+dirs[i][0],col+dirs[i][1]
if 0<=nr<R and 0<=nc<C:
next=ord(board[nr][nc])-65
if alpha[next]==0:
alpha[next]=1
dfs(nr,nc,cnt+1)
alpha[next] = 0
alpha[ord(board[0][0])-65]=1
dfs(0,0,1)
print(answer)
'IT > coding study' 카테고리의 다른 글
[acmicpc] 20437. 문자열 게임 2 (python) (0) | 2022.09.22 |
---|---|
[acmicpc] 1806. 부분합(python) (2) | 2022.09.21 |
[acmicpc] 2467. 용액(python) (0) | 2022.09.19 |
[acmicpc] 20922. 겹치는 건 싫어(python) (0) | 2022.09.18 |
[acmicpc] 3758. KCPC(python) (0) | 2022.09.18 |