Algorithm
[백준 1987] 알파벳
moguogu
2024. 9. 17. 16:48
문제
https://www.acmicpc.net/problem/1987
코드
# 백트래킹 문제
import sys
from collections import deque
sys.setrecursionlimit(10**9)
def Input_Data():
input = sys.stdin.readline
R, C = map(int, input().split())
graph = []
for _ in range(R):
graph.append(list(input()))
return R,C, graph
d = ((1,0),(-1,0),(0,1),(0,-1))
def Dfs(x,y,cnt):
global ans
ans = max(cnt, ans)
for dx, dy in d:
nx , ny = dx+ x, dy+y
if not (0<=nx<R and 0<=ny<C): continue
if visited[ord(graph[nx][ny])-65] == True: continue
visited[ord(graph[nx][ny])-65] = True
Dfs(nx,ny,cnt+1)
visited[ord(graph[nx][ny])-65] = False
R,C,graph = Input_Data()
ans = 0
visited = [False] * (26)
visited[ord(graph[0][0])-65] = True
Dfs(0,0,1)
print(ans)