[BOJ][๐ŸŸก5][๋ฐฑ์ค€#02589] ๋ณด๋ฌผ์„ฌ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2589


๋ฌธ์ œ

๋ณด๋ฌผ์„ฌ ์ง€๋„๋ฅผ ๋ฐœ๊ฒฌํ•œ ํ›„ํฌ ์„ ์žฅ์€ ๋ณด๋ฌผ์„ ์ฐพ์•„๋‚˜์„ฐ๋‹ค. ๋ณด๋ฌผ์„ฌ ์ง€๋„๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋ฉฐ ์—ฌ๋Ÿฌ ์นธ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ์นธ์€ ์œก์ง€(L)๋‚˜ ๋ฐ”๋‹ค(W)๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. ์ด ์ง€๋„์—์„œ ์ด๋™์€ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด์›ƒํ•œ ์œก์ง€๋กœ๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ•œ ์นธ ์ด๋™ํ•˜๋Š”๋ฐ ํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ๋ณด๋ฌผ์€ ์„œ๋กœ ๊ฐ„์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š”๋ฐ ์žˆ์–ด ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ์œก์ง€ ๋‘ ๊ณณ์— ๋‚˜๋‰˜์–ด ๋ฌปํ˜€์žˆ๋‹ค. ์œก์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๊ณณ ์‚ฌ์ด๋ฅผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋ ค๋ฉด ๊ฐ™์€ ๊ณณ์„ ๋‘ ๋ฒˆ ์ด์ƒ ์ง€๋‚˜๊ฐ€๊ฑฐ๋‚˜, ๋ฉ€๋ฆฌ ๋Œ์•„๊ฐ€์„œ๋Š” ์•ˆ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์ด ์ง€๋„๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๋ฉด ๋ณด๋ฌผ์€ ์•„๋ž˜ ํ‘œ์‹œ๋œ ๋‘ ๊ณณ์— ๋ฌปํ˜€ ์žˆ๊ฒŒ ๋˜๊ณ , ์ด ๋‘˜ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์€ 8์‹œ๊ฐ„์ด ๋œ๋‹ค.

๋ณด๋ฌผ ์ง€๋„๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ณด๋ฌผ์ด ๋ฌปํ˜€ ์žˆ๋Š” ๋‘ ๊ณณ ๊ฐ„์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ๋ณด๋ฌผ ์ง€๋„์˜ ์„ธ๋กœ์˜ ํฌ๊ธฐ์™€ ๊ฐ€๋กœ์˜ ํฌ๊ธฐ๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด L๊ณผ W๋กœ ํ‘œ์‹œ๋œ ๋ณด๋ฌผ ์ง€๋„๊ฐ€ ์•„๋ž˜์˜ ์˜ˆ์™€ ๊ฐ™์ด ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ๋ฌธ์ž ์‚ฌ์ด์—๋Š” ๋นˆ ์นธ์ด ์—†๋‹ค. ๋ณด๋ฌผ ์ง€๋„์˜ ๊ฐ€๋กœ, ์„ธ๋กœ์˜ ํฌ๊ธฐ๋Š” ๊ฐ๊ฐ 50์ดํ•˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ณด๋ฌผ์ด ๋ฌปํ˜€ ์žˆ๋Š” ๋‘ ๊ณณ ์‚ฌ์ด๋ฅผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

5 7
WLLWWWL
LLLWLLL
LWLWLWW
LWLWLLL
WLLWLWW


์ถœ๋ ฅ

8


My Sol

import sys
input = sys.stdin.readline

def BFS(i, j):
    visited = [[0]*J for _ in range(I)]
    didj = [(-1,0),(1,0),(0,1),(0,-1)]
    visited[i][j] = 1
    Q = [(i, j)]
    front = -1
    rear = 0
    max_now = 0
    while front < rear:
        front += 1
        ni, nj = Q[front]
        for di, dj in didj:
            si, sj = ni+di, nj+dj
            if not (0<=si<I and 0<=sj<J): continue
            if visited[si][sj]: continue
            if not mat[si][sj]: continue
            visited[si][sj] = visited[ni][nj]+1
            max_now = max(visited[si][sj], max_now)
            Q.append((si, sj))
            rear += 1
    return max_now-1


I, J = map(int, input().split())
mat = []
for _ in range(I):
    l = [1 if i=='L' else 0 for i in list(input())]
    mat.append(l)

max_val = 0
for i in range(I):
    for j in range(J):
        if not mat[i][j]: continue
        val = BFS(i, j)
        if max_val < val:
            max_val = val
print(max_val)

BFS๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์™„์ „ํƒ์ƒ‰์„ ์ด์šฉํ•ด ์ „์ฒด ์นธ์— ๋Œ€ํ•ด ์กฐํšŒํ•˜๋Š”๋ฐ ๋งŒ์•ฝ โ€˜Lโ€™ ์นธ์„ ๋งŒ๋‚˜๋ฉด ์ƒํ•˜์ขŒ์šฐ์˜ ๊ฐ€์žฅ ์ธ์ ‘ํ•œ ์นธ๋งˆ๋‹ค ์‹œ๊ฐ„์„ ๋”ํ•ด๊ฐ€๋ฉฐ ์ฑ„์šฐ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ์นธ์˜ ๋งˆ์ง€๋ง‰ ์นธ์˜ ๊ฐ’์„ ๋งค๋ฒˆ ์ตœ๋Œ“๊ฐ’๊ณผ ๋น„๊ตํ•˜๋ฉฐ ์™„์ „ํƒ์ƒ‰์„ ๋งˆ์น˜๋ฉด ํ•ด๋‹น ์ตœ๋Œ“๊ฐ’์ด ์ง€๋„์—์„œ ๊ฐ€์žฅ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ œ์ผ ๋จผ ๊ณณ๊นŒ์ง€์˜ ์†Œ์š” ์‹œ๊ฐ„์ด ๋œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys, collections
n, m = [int(x) for x in sys.stdin.readline().split()]
board = [list(sys.stdin.readline().rstrip()) for _ in range(n)]
dx, dy = (1, -1, 0, 0), (0, 0, 1, -1)
def bfs(x, y):
    q = collections.deque([(x, y)])
    visited = [[0]*m for _ in range(n)]
    visited[x][y] = 1
    mx = 0
    while q:
        x, y = q.popleft()
        for i in range(4):
            nx, ny = x+dx[i], y+dy[i]
            if not (0 <= nx < n and 0 <= ny < m) or visited[nx][ny] != 0 or board[nx][ny] == "W": continue
            visited[nx][ny] = visited[x][y] + 1
            mx = max(visited[nx][ny], mx)
            q.append((nx, ny))
    return mx-1
result = 0
for i in range(n):
    for j in range(m):
        if board[i][j] == "L":
            if 0 <= i-1 and i+1 < n:
                if board[i-1][j] == 'L' and board[i+1][j] == 'L': continue
            if 0 <= j-1 and j+1 < m:
                if board[i][j-1] == 'L' and board[i][j+1] == 'L': continue
            result = max(result, bfs(i, j))
print(result)

์—ฐ์‚ฐ ์‹œ๊ฐ„์ด ์งง์€ ๋‹ต์ด ์žˆ์–ด์„œ ๊ฐ€์ ธ์™€ ๋ณด์•˜๋‹ค. ์™„์ „ํƒ์ƒ‰์„ ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ง€์น˜๊ธฐํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์‚ฌ๋ฐฉ์ด ์œก์ง€์ธ ๊ณณ๋ถ€ํ„ฐ๋Š” ์ ˆ๋Œ€ ์ตœ๋Œ€๊ธธ์ด๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์—†๋Š” ์ ์„ ์ด์šฉํ•ด์„œ bfs๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ์ƒํ•˜์ขŒ์šฐ ๋ชจ๋‘ โ€˜Lโ€™ ์žˆ๋‹ค๋ฉด ์ œ์™ธํ•˜๊ณ  ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋˜‘๋˜‘ํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ