728x90
일반적인 BFS 로 접근했는데, 매번 max 값을 초기화 해주는 부분에서 막혔고, 찾아보니 set 을 사용한 bfs 를 해주어야 한다고 한다. 포인트는 queue 에 위치 값 뿐 아니라, 현재까지 지나온 알파벳 리스트를 넣어주는 것이다.
import sys
from collections import deque
a,b = list(map(int,sys.stdin.readline().split(" ")))
k = [ ]
for i in range(0,a):
k.append(list(input()))
# bfs 돌리기
queue= set([(0,0,k[0][0])])
answer=1
while queue:
x,y,z = queue.pop()
answer = max(answer,len(z))
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(4):
temp_x = x + dx[i]
temp_y = y + dy[i]
if 0<=temp_x<a and 0<=temp_y<b and k[temp_x][temp_y] not in z:
queue.add((temp_x,temp_y,k[temp_x][temp_y]+z))
print(answer)
728x90
'🟢 알고리즘 문제 풀이 > Baekjoon' 카테고리의 다른 글
| [파이썬] 백준 1922 네트워크 연결 (0) | 2023.05.06 |
|---|---|
| [파이썬] 백준 14500 테트로미노 (0) | 2023.05.01 |
| [파이썬] 백준 1753 최단경로 (0) | 2023.04.30 |
| [파이썬] 백준 14502 연구소 (0) | 2023.04.28 |
| [파이썬] 백준 16938 캠프 준비 (0) | 2023.04.14 |