출처 : https://www.acmicpc.net/problem/1987
# code
## import sys
## input = sys.stdin.readline
def bfs():
mx = 0
queue = set()
queue.add((0, 0, board[0][0]))
while queue:
x, y, sentence = queue.pop()
mx = max(mx, len(sentence))
if mx == 26: return 26
## 동 남 서 북
for dx, dy in zip(dxs, dys):
nx, ny = x + dx, y + dy
if nx < 0 or nx >= r or ny < 0 or ny >= c: continue
if board[nx][ny] in sentence: continue
queue.add((nx, ny, sentence + board[nx][ny]))
return mx
if __name__ == '__main__':
r, c = map(int, input().split())
board = [list(input()) for _ in range(r)]
## 동 남 서 북
dxs = (0, 1, 0, -1)
dys = (1, 0, -1, 0)
print(bfs())
|
cs |
'알고리즘 > [Python] 백준' 카테고리의 다른 글
[Python] 백준 17837번: 새로운 게임 2 (0) | 2020.04.23 |
---|---|
[Python] 백준 17140번: 이차원 배열과 연산 (0) | 2020.04.19 |
(Python) 백준 17143번: 낚시왕 (0) | 2020.03.28 |
(Python) 백준 18809번: Gaaaaaaaaaarden (0) | 2020.03.25 |
(Python) 백준 18808번: 스티커 붙이기 (0) | 2020.03.24 |