From 4387fe286f85e1ea711c140a80266018deffb417 Mon Sep 17 00:00:00 2001 From: seongwon030 <105052068+seongwon030@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:31:24 +0900 Subject: [PATCH] =?UTF-8?q?2024-07-30=20=EC=95=8C=ED=8C=8C=EB=B2=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\225\214\355\214\214\353\262\263.py" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "seongwon030/bfs/\354\225\214\355\214\214\353\262\263.py" diff --git "a/seongwon030/bfs/\354\225\214\355\214\214\353\262\263.py" "b/seongwon030/bfs/\354\225\214\355\214\214\353\262\263.py" new file mode 100644 index 0000000..6b45c90 --- /dev/null +++ "b/seongwon030/bfs/\354\225\214\355\214\214\353\262\263.py" @@ -0,0 +1,25 @@ +import sys +input = sys.stdin.readline + +def bfs(original, x, y): + ans = 1 + q = set([(x, y, k[y][x])]) + while q: + x, y, alpha = q.pop() + ans = max(ans, len(alpha)) + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if 0 <= nx < c and 0 <= ny < r and k[ny][nx] not in alpha: + q.add((nx, ny, alpha + original[ny][nx])) + return ans + +r, c = map(int, input().split()) +k = [] +for _ in range(r): + k.append(list(input())) + +dx = [0, 0, -1, 1] +dy = [-1, 1, 0, 0] + +print(bfs(k, 0, 0)) \ No newline at end of file