출처 : https://www.acmicpc.net/problem/1987
1987번: 알파벳
문제 세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으로 이동할 수 있는데, 새로 이동한 칸에 적혀 있는 알파벳은 지금까지 지나온 모든 칸에 적혀 있는 알파벳과는 달라야 한다. 즉, 같은 알파벳이 적힌 칸을 두 번 지날 수 없다. 좌측 상단에서 시작해서, 말이 최대한 몇 칸을 지날 수 있는지를 구하는
www.acmicpc.net
# 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 |