We will find a way, we always have.

-interstellar

Problem Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1926๋ฒˆ: ๊ทธ๋ฆผ - ํŒŒ์ด์ฌ

Redddy 2022. 6. 8. 21:00

๐Ÿ”ˆ ๋ฌธ์ œ

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

 

 

๐Ÿ“์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋„ํ™”์ง€์˜ ์„ธ๋กœ ํฌ๊ธฐ n(1 ≤ n ≤ 500)๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ m(1 ≤ m ≤ 500)์ด ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ n+1 ์ค„ ๊นŒ์ง€ ๊ทธ๋ฆผ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (๋‹จ ๊ทธ๋ฆผ์˜ ์ •๋ณด๋Š” 0๊ณผ 1์ด ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ฃผ์–ด์ง€๋ฉฐ, 0์€ ์ƒ‰์น ์ด ์•ˆ๋œ ๋ถ€๋ถ„, 1์€ ์ƒ‰์น ์ด ๋œ ๋ถ€๋ถ„์„ ์˜๋ฏธํ•œ๋‹ค)

 

๐Ÿ“‘์ถœ๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ๊ทธ๋ฆผ์˜ ๊ฐœ์ˆ˜, ๋‘˜์งธ ์ค„์—๋Š” ๊ทธ ์ค‘ ๊ฐ€์žฅ ๋„“์€ ๊ทธ๋ฆผ์˜ ๋„“์ด๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ๋ผ. ๋‹จ, ๊ทธ๋ฆผ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๋„“์€ ๊ทธ๋ฆผ์˜ ๋„“์ด๋Š” 0์ด๋‹ค.

 

๐Ÿ“š ๋ฌธ์ œ ํ•ด์„

์‰ฝ๊ฒŒ ํ•ด์„ํ•˜๋ฉด ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์š”์†Œ์˜ ๊ฐฏ์ˆ˜์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์š”์†Œ์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค. BFS ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DFS ๋ณด๋‹ค ๋น ๋ฅธ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๐Ÿ“– ํ’€์ด

๋‹ค๋ฅธ BFS ๋ฌธ์ œ๋“ค์ฒ˜๋Ÿผ ์ƒ‰์น ๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ฐพ์•„ ๋ฐฉ๋ฌธํ•˜๊ณ  BFS ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค. ์ด๋•Œ ์—ฐ๊ฒฐ ์š”์†Œ์˜ ์ตœ๋Œ€๊ฐ’(๊ทธ๋ฆผ์˜ ๋„“์ด)๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๊ธฐ ์œ„ํ•ด are ๋ผ๋Š” ๋ณ€์ˆ˜์— ์—ฐ๊ฒฐ ์ˆ˜๋ฅผ ๋‹ด๊ณ  res ๋ฐฐ์—ด์— ๋‹ด์•„์ฃผ์—ˆ๋‹ค. ์ฒดํฌํ•ด์•ผ ํ•  ๊ฒƒ์€ ๊ทธ๋ฆผ์ด ํ•˜๋‚˜๋„ ๊ทธ๋ ค์ง€์ง€ ์•Š์„ ๊ฒฝ์šฐ 0์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š”๋ฐ max ํ•จ์ˆ˜๋Š” ๋นˆ ๋ฐฐ์—ด์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ถ€๋ถ„์„ if ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ•„์ž๋Š”(์œผ์•… ์˜ค๊ธ€๊ฑฐ๋ฆผ) res ๋ฐฐ์—ด ์ƒ์„ฑํ•  ๋•Œ 0์„ ๋„ฃ์Œ์œผ๋กœ์จ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ์‹œ๊ฐ„๋ฉด์—์„œ๋Š” ๋ณ„์ฐจ์ด ์—†์–ด๋ณด์ด์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ๋” ์˜ˆ๋ป๋ณด์ธ๋‹ค.

 

๐Ÿ’ป ์ฝ”๋“œ

import sys
from collections import deque
input = sys.stdin.readline

n,m = map(int,input().rstrip().split())
do = [] # ๋„ํ™”์ง€, ๊ทธ๋ž˜ํ”„
cnt = 0 # ๊ทธ๋ฆผ์˜ ๊ฐฏ์ˆ˜ ๋ณ€์ˆ˜
res = [0] # ๊ทธ๋ฆผ์˜ ๋„“์ด ๋‹ด์„ ๋ฐฐ์—ด

# ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ
for i in range(n):
    do.append(list(map(int,input().rstrip().split())))
    
def bfs(x,y):
	# ์ƒํ•˜์ขŒ์šฐ 4๋ฐฉํ–ฅ
    dx = [-1,1,0,0]
    dy = [0,0,-1,1]
    queue = deque()
    queue.append([x,y])
    do[x][y] = 0 # ์‹œ์ž‘์  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    are = 1 # ๋„“์ด๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘
    while queue:
        a,b = queue.popleft()
        for i in range(4):
            nx = a + dx[i]
            ny = b + dy[i]
			# ๋„ํ™”์ง€ ์•ˆ์— ์œ„์น˜ํ•˜๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฐฉ๋ฌธ
            if 0 <= nx < n and 0 <= ny < m and do[nx][ny] == 1:
                are += 1 # ๋„“์ด ํ‚ค์šฐ๊ธฐ
                do[nx][ny] = 0 # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ            
                queue.append([nx, ny]) # ํ์— ์‚ฝ์ž…
    res.append(are) # ๊ทธ๋ฆผ์˜ ๋„“์ด ๋ฐฐ์—ด์— ์‚ฝ์ž…

for x in range(n):
    for y in range(m):
        if do[x][y] == 1: # ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ ธ์žˆ๋‹ค๋ฉด BFS ํƒ์ƒ‰ ์‹œ์ž‘
            bfs(x,y)
            cnt += 1 # ๊ทธ๋ฆผ ๊ฐฏ์ˆ˜ +1

print(cnt) # ๊ทธ๋ฆผ๊ฐฏ์ˆ˜ ์ถœ๋ ฅ
print(max(res)) # ๊ทธ๋ฆผ์˜ ๋„“์ด ์ตœ๋Œ€๊ฐ’ ์ถœ๋ ฅ

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ : ๊ทธ๋ฆผ

 

1926๋ฒˆ: ๊ทธ๋ฆผ

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

www.acmicpc.net