์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ - 2468๋ฒˆ: ์•ˆ์ „์˜์—ญ

  • -

#๋ฌธ์ œ๋งํฌ :https://www.acmicpc.net/problem/2468

 

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

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

www.acmicpc.net

#๋‚œ์ด๋„ : Silver1

 

#๋ถ„๋ฅ˜ : DFS

 

#์ ‘๊ทผ๋ฐฉ๋ฒ•

DFS๋ฅผ ํ†ตํ•ด ์ˆ˜์‹ฌ๋ณด๋‹ค ๋‚ฎ์€ ์ง€์—ญ์„ 0์œผ๋กœ ๋ฐ”๊พธ๊ณ , ๊ทธ ์ด์™ธ์˜ ์ง€์—ญ์ด ๋ช‡๊ฐœ์˜ ๋ฒ”์œ„๊ฐ€ ๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ์ด๋•Œ ์ˆ˜์‹ฌ์„ ๊ฐ€์žฅ ๋‚ฎ์€ ์ง€์—ญ ~ ๊ฐ€์žฅ ๋†’์€ ์ง€์—ญ์„ ๋ฒ”์œ„๋กœ +1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ ๋ชจ๋‘ ํ™•์ธํ•ด๋ณธ๋‹ค.

๋ฌธ์ œ์—์„œ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š์„์ˆ˜๋„ ์žˆ๋‹ค ๋ผ๋Š” ์กฐ๊ฑด์„ ๊ฑธ์—ˆ์œผ๋ฏ€๋กœ, ์ •๋‹ต์„ ๋‹ด์€ ๋ฐฐ์—ด์—๋Š” 1์„ ํ•˜๋‚˜ ๋„ฃ์–ด์ค€๋‹ค. (๋‹ต์ด ๋  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ๊ฐ’์ด 1์ด๋ฏ€๋กœ)

 

Code(Python)

from sys import setrecursionlimit
setrecursionlimit(10000)
n = int(input())

grid = []
cnt = 0
visited = [[False for i in range(n)] for i in range(n)]
for _ in range(n):
    grid.append(list(map(int, input().split())))



def is_valid(x,y):
    if x >= 0 and x < n and y < n and y >= 0:
        return True
    return False

def dfs(x,y):
    if not is_valid(x,y):
        return False
    if visited[x][y] == False and grid[x][y] != 0:
        visited[x][y] = True
        dfs(x+1,y)
        dfs(x-1,y)
        dfs(x,y+1)
        dfs(x,y-1)
        return True
    return False


def rain(depth):
    global visited
    visited = [[False for i in range(n)] for i in range(n)]
    for x in range(n):
        for y in range(n):
            if grid[x][y] <= depth:
                grid[x][y] = 0

min_depth = min(min(grid))
max_depth = max(max(grid))
depth = min_depth
answers = [1]

while(depth <= max_depth):
    rain(depth)
    cnt = 0
    for x in range(n):
        for y in range(n):
            if dfs(x,y):
                cnt += 1

    answers.append(cnt)
    depth += 1
print(max(answers))



Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.