ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

2468๋ฒˆ: ์•ˆ์ „ ์˜์—ญ

์žฌ๋‚œ๋ฐฉ์žฌ์ฒญ์—์„œ๋Š” ๋งŽ์€ ๋น„๊ฐ€ ๋‚ด๋ฆฌ๋Š” ์žฅ๋งˆ์ฒ ์— ๋Œ€๋น„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์„ ๊ณ„ํšํ•˜๊ณ  ์žˆ๋‹ค. ๋จผ์ € ์–ด๋–ค ์ง€์—ญ์˜ ๋†’์ด ์ •๋ณด๋ฅผ ํŒŒ์•…ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ์— ๊ทธ ์ง€์—ญ์— ๋งŽ์€ ๋น„๊ฐ€ ๋‚ด๋ ธ์„ ๋•Œ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์ด์ „์— ๋‹ค๋ฃฌ ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ, ์„ฌ์˜ ๊ฐœ์ˆ˜์™€ ์œ ์‚ฌํ•œ ๋ฌธ์ œ์ด๋‹ค. ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋ฉด ๋น„๊ฐ€ ์˜ค๋Š” ์–‘์— ๋”ฐ๋ผ ์นจ์ˆ˜๋˜๋Š” ์ง€์—ญ์ด ๋‹ฌ๋ผ์ง€๋ฉฐ, ๊ทธ๋กœ ์ธํ•ด ์•ˆ์ „ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ณ€๋™๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” BFS๋กœ ํƒ์ƒ‰์„ ํ•˜๋˜, ๋†’์ด๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ณณ์ด ์นจ์ˆ˜๋  ๋•Œ๋ถ€ํ„ฐ, ๊ฐ€์žฅ ๋†’์€ ๊ณณ์ด ์นจ์ˆ˜๋  ๋•Œ๊นŒ์ง€ BFS๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ์ตœ๋Œ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

 

 

  • 3์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด BFS๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    • ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ : ๊ฐ€์žฅ ๋‚ฎ์€ ๋†’์ด ~ ๊ฐ€์žฅ ๋†’์€ ๋†’์ด
    • ๋‘ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ : ํ–‰์„ ์„ ํƒํ•˜๋Š” i
    • ์„ธ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ : ์—ด์„ ์„ ํƒํ•˜๋Š” j
      • BFS๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ํ˜„์žฌ ์นจ์ˆ˜๋˜๋Š” ๋†’์ด๋ณด๋‹ค ํฐ ๊ฒƒ์„ ํƒ์ƒ‰ํ•œ๋‹ค.

์œ„์˜ ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ ์˜ˆ์ œ ์ž…๋ ฅ์—์„œ ๋†’์ด 2๊ฐ€ ์นจ์ˆ˜๋  ๊ฒฝ์šฐ์ด๋‹ค. ์นจ์ˆ˜๋˜๋Š” ๊ณณ ์ด์™ธ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์€ ํ•œ ์˜์—ญ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋ฏ€๋กœ, ๋†’์ด 2๊ฐ€ ์นจ์ˆ˜๋  ๋•Œ ์•ˆ์ „ ์˜์—ญ์˜ ์ˆ˜๋Š” 1๊ฐœ์ด๋‹ค.

 

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def visitable(x, y, height):
    return 0 <= x < n and 0 <= y < n and graph[x][y] > height


def bfs(start, height):
    dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
    q = deque([start])

    while q:
        x, y = q.popleft()
        for dx, dy in dirs:
            next_x, next_y = x + dx, y + dy

            if visitable(next_x, next_y, height) and not visited[next_x][next_y]:
                q.append([next_x, next_y])
                visited[next_x][next_y] = True

    return 1

if __name__ == "__main__":
    n = int(stdin.readline())
    graph = [list(map(int, stdin.readline().split())) for _ in range(n)]
    answer = 1

    for cur_height in range(min(min(graph)), max(max(graph))):
        cnt = 0
        visited = [[False] * n for _ in range(n)]
        for i in range(n):
            for j in range(n):
                if not visited[i][j] and graph[i][j] > cur_height:
                    cnt += bfs([i, j], cur_height)
        answer = max(answer, cnt)
    print(answer)

 ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ค‘์— ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ์ผ€์ด์Šค(์ฑ„์  ์ค‘ ์•ฝ 95%์ฏค)์ธ [[1, 1], [1, 1]]์„ ์ž…๋ ฅํ•˜๋ฉด 1์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ์•ผ ํ•˜๋Š”๋ฐ, 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰ ๋ชจ๋“  ๋†’์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ, BFS๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ์ตœ์†Œ ์•ˆ์ „ ์˜์—ญ์€ 1์ด๊ธฐ์— answer๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€