๐Ÿง€ ์น˜์ฆˆ

๋ฐฑ์ค€ Class4 Gold3

solved_ac[Class4] ์น˜์ฆˆ

๋ฌธ์ œ


img

                        <๊ทธ๋ฆผ 1>

Nร—M์˜ ๋ชจ๋ˆˆ์ข…์ด ์œ„์— ์•„์ฃผ ์–‡์€ ์น˜์ฆˆ๊ฐ€ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. ๋‹จ, N ์€ ์„ธ๋กœ ๊ฒฉ์ž์˜ ์ˆ˜์ด๊ณ , M ์€ ๊ฐ€๋กœ ๊ฒฉ์ž์˜ ์ˆ˜์ด๋‹ค. ์ด ์น˜์ฆˆ๋Š” ๋ƒ‰๋™ ๋ณด๊ด€์„ ํ•ด์•ผ๋งŒ ํ•˜๋Š”๋ฐ ์‹ค๋‚ด์˜จ๋„์— ๋‚ด์–ด๋†“์œผ๋ฉด ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•˜์—ฌ ์ฒœ์ฒœํžˆ ๋…น๋Š”๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฌํ•œ ๋ชจ๋ˆˆ์ข…์ด ๋ชจ์–‘์˜ ์น˜์ฆˆ์—์„œ ๊ฐ ์น˜์ฆˆ ๊ฒฉ์ž(์ž‘ ์€ ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘)์˜ 4๋ณ€ ์ค‘์—์„œ ์ ์–ด๋„ 2๋ณ€ ์ด์ƒ์ด ์‹ค๋‚ด์˜จ๋„์˜ ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•œ ๊ฒƒ์€ ์ •ํ™•ํžˆ ํ•œ์‹œ๊ฐ„๋งŒ์— ๋…น์•„ ์—†์–ด์ ธ ๋ฒ„๋ฆฐ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜ <๊ทธ๋ฆผ 1> ๋ชจ์–‘๊ณผ ๊ฐ™์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๋ผ๋ฉด C๋กœ ํ‘œ์‹œ๋œ ๋ชจ๋“  ์น˜์ฆˆ ๊ฒฉ์ž๋Š” ํ•œ ์‹œ๊ฐ„ ํ›„์— ์‚ฌ๋ผ์ง„๋‹ค.

img

                        <๊ทธ๋ฆผ 2>

img

                        <๊ทธ๋ฆผ 3>

<๊ทธ๋ฆผ 2>์™€ ๊ฐ™์ด ์น˜์ฆˆ ๋‚ด๋ถ€์— ์žˆ๋Š” ๊ณต๊ฐ„์€ ์น˜์ฆˆ ์™ธ๋ถ€ ๊ณต๊ธฐ์™€ ์ ‘์ด‰ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€ ๋กœ ์ด ๊ณต๊ฐ„์— ์ ‘์ด‰ํ•œ ์น˜์ฆˆ ๊ฒฉ์ž๋Š” ๋…น์ง€ ์•Š๊ณ  C๋กœ ํ‘œ์‹œ๋œ ์น˜์ฆˆ ๊ฒฉ์ž๋งŒ ์‚ฌ๋ผ์ง„๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ•œ ์‹œ๊ฐ„ ํ›„, ์ด ๊ณต๊ฐ„์œผ๋กœ ์™ธ๋ถ€๊ณต๊ธฐ๊ฐ€ ์œ ์ž…๋˜๋ฉด <๊ทธ๋ฆผ 3>์—์„œ์™€ ๊ฐ™์ด C๋กœ ํ‘œ์‹œ๋œ ์น˜์ฆˆ ๊ฒฉ์ž๋“ค์ด ์‚ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.

๋ชจ๋ˆˆ์ข…์ด์˜ ๋งจ ๊ฐ€์žฅ์ž๋ฆฌ์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“์ด์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ •ํ™•ํ•œ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ


์ฒซ์งธ ์ค„์—๋Š” ๋ชจ๋ˆˆ์ข…์ด์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ N, M (5 โ‰ค N, M โ‰ค 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๋ชจ๋ˆˆ์ข…์ด ์œ„์˜ ๊ฒฉ์ž์— ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์€ 1๋กœ ํ‘œ์‹œ๋˜๊ณ , ์น˜์ฆˆ๊ฐ€ ์—†๋Š” ๋ถ€๋ถ„์€ 0์œผ๋กœ ํ‘œ์‹œ๋œ๋‹ค. ๋˜ํ•œ, ๊ฐ 0๊ณผ 1์€ ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋‹ค.

์ถœ๋ ฅ


์ถœ๋ ฅ์œผ๋กœ๋Š” ์ฃผ์–ด์ง„ ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ •ํ™•ํ•œ ์‹œ๊ฐ„์„ ์ •์ˆ˜๋กœ ์ฒซ ์ค„์— ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

8 9
0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 1 1 0 1 1 0
0 0 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 1 1 0 1 1 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

์˜ˆ์ œ ์ถœ๋ ฅ 1

4

๐Ÿ“ ๊ณผ์ •์„ค๊ณ„

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-20

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-21

์„ค๋ช…ํ•˜๊ธฐ์— ์•ž์„œ ๋‚˜๋Š” ๊ธฐ๋ณธ BFS ํ‹€์— ์กฐ๊ฑด๋งŒ ์ถ”๊ฐ€ํ•˜์—ฌ ํ’€์—ˆ๋‹ค.

๋ฒฝ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๊ธฐ์™€ ๋น„์Šทํ•œ ๊ฒฐ์˜ ๋ฌธ์ œ์ด๋‹ค.

์˜ˆ์ œ๋ฅผ ์‹œ๊ฐ„์˜ ๊ฒฝ๊ณผ์— ๋”ฐ๋ผ ๋‚˜ํƒ€๋‚ด๋ณด์•˜๋‹ค.

(0,0) ์ดˆ๋ก์ƒ‰ ํ˜•๊ด‘ํŽœ ์ง€์ ์„ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•˜์˜€๋‹ค. ์น˜์ฆˆ์— ํ‘œ๊ธฐ๋˜์–ด ์žˆ๋Š” ํŒŒ๋ž€์ƒ‰ ์ˆซ์ž๋Š” ์น˜์ฆˆ๊ฐ€ ๋ช‡ ๊ฐœ์˜ ์™ธ๋ถ€ ๊ณต๊ธฐ์— ๋งž๋‹ฟ์•„ ์žˆ๋Š”์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

์ฆ‰, ์™ธ๋ถ€ ๊ณต๊ธฐ(0) ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰์„ ํ•˜๋ฉฐ ์น˜์ฆˆ(1)๋ฅผ ๋งŒ๋‚  ๋•Œ๋งˆ๋‹ค ํ„ฐ์น˜ ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ํ–ˆ์„ ๋•Œ, ์™ธ๋ถ€ ๊ณต๊ธฐ์˜ ํ„ฐ์น˜ ์นด์šดํŠธ๊ฐ€ 2๋ฒˆ ์ด์ƒ๋œ ์น˜์ฆˆ๋งŒ ๊ณจ๋ผ์„œ ๋…น์ธ๋‹ค(1์„ 0์œผ๋กœ ๋ฐ”๊พผ๋‹ค.)

  • ์ถœ๋ฐœ ๋…ธ๋“œ์—์„œ BFS๋ฅผ ๋Œ๋ฆฐ ๋‹ค์Œ์— ๋” ์ด์ƒ ๋‚จ์•„์žˆ๋Š” ์น˜์ฆˆ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ BFS๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

  • ๋™์‹œ์— BFS๋ฅผ ํ•œ ๋ฒˆ ๋Œ๋ฆด ๋•Œ๋งˆ๋‹ค ์‹œ๊ฐ„์„ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๋ฉด(hour += 1) ์ตœ์ข…์ ์ธ ์น˜์ฆˆ๊ฐ€ ๋…น๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป CODE

from collections import deque
N,M = map(int,input().split())

cheese = []

for _ in range(N):
    cheese.append(list(map(int,input().split())))



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

def bfs(a,b):
    q = deque([(a,b)])
    visited = [[0]*M for _ in range(N)]
    touch = [[0]*M for _ in range(N)]
    while q:
        x,y = q.popleft()

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

            if 0<=nx<N and 0<=ny<M and visited[nx][ny] == 0:
                
                if cheese[nx][ny] == 0:
                    q.append((nx,ny))
                    visited[nx][ny] = 1
                
                if cheese[nx][ny] == 1:
                    touch[nx][ny] += 1
    
    for i in range(N):
        for j in range(M):
            if touch[i][j] >= 2:
                cheese[i][j] = 0

def empty_check(cheese):
    
    for i in range(N):
        for j in range(M):
            if cheese[i][j] == 1:
                return False
    return True

hour = 0

while True:

    if empty_check(cheese):
        print(hour)
        break

    bfs(0,0)
    hour += 1

ยฉ 2022. All rights reserved.

Powered by Hydejack v9.1.6