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)

'Algorithm' 카테고리의 다른 글

[백준 2146] 다리 만들기  (0) 2024.09.17
[백준 1922] 네트워크 연결  (0) 2024.09.17
[백준 1431] 시리얼 번호  (0) 2024.09.17
[백준 1092] 배  (0) 2024.09.17
[백준 5014] 스타트링크  (0) 2024.09.02