상태공간 check에 같은 상태가 안오도록 가지치기.
import sys
input = sys.stdin.readline
def bfs(x, y):
q = set([(x, y, board[x][y])])
check[x][y] = board[x][y]
answer = 1
while q:
x, y, string = q.pop()
for dx, dy in delta:
nx, ny = x + dx, y + dy
if 0 <= nx < R and 0 <= ny < C and board[nx][ny] not in string:
new_string = string + board[nx][ny]
if check[nx][ny] != new_string:
check[nx][ny] = new_string
q.add((nx, ny, new_string))
answer = max(answer, len(string) + 1)
if answer == 26:
return 26
return answer
R, C = map(int, input().split())
board = [list(input().rstrip()) for _ in range(R)]
delta = [(0, 1), (1, 0), (-1, 0), (0, -1)]
check = [[''] * C for _ in range(R)]
print(bfs(0, 0))
'practivceAlgorithm > 백준' 카테고리의 다른 글
[백준][Python] 2469 사다리 타기 (0) | 2021.10.20 |
---|---|
[백준][Python] 2729 이진수 덧셈 (0) | 2021.10.19 |
[백준][Python] 18115 카드 놓기 (0) | 2021.10.19 |
[백준][Python] 18429 근손실 (0) | 2021.10.19 |
[백준][Python] 2164 카드2 (0) | 2021.10.19 |