Post

๐Ÿข ๋ณด๋ฌผ์„ฌ

1. ๋ฌธ์ œ ๋งํฌ

2589๋ฒˆ: ๋ณด๋ฌผ์„ฌ


2. ์ฝ”๋“œ

PyPy3 116128KB 1008ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
[2589] ๋ณด๋ฌผ์„ฌ

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

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

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

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

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

from collections import deque

# ๋ณด๋ฌผ ์ง€๋„์˜ ์„ธ๋กœ์˜ ํฌ๊ธฐ์™€ ๊ฐ€๋กœ์˜ ํฌ๊ธฐ
N, M = map(int, input().split())

good = []
graph = []

dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

for i in range(N):
    temp = list(map(str, input()))
    for j in range(len(temp)):
        # ๊ฐ ์นธ์€ ์œก์ง€(L)๋‚˜ ๋ฐ”๋‹ค(W)
        if temp[j] == 'L':
            graph.append((i, j))
    good.append(temp)


def bfs(r, c):
    queue = deque([[r, c, 0]])
    visited[r][c] = True

    while queue:
        r, c, distance = queue.popleft()

        for i in range(4):
            nx = r + dx[i]
            ny = c + dy[i]

            if 0 <= nx < N and 0 < ny < M :
                visited[nx][ny] = True
                queue.append([nx, ny, distance + 1])
    return distance


result = 0
for i in range(len(graph)):
    visited = [[False] * M for _ in range(N)]
    distance = bfs(graph[i][0], graph[i][1])
    if result < distance:
        result = distance

print(result)


3. ํ•ด์„ค

This post is licensed under CC BY 4.0 by the author.