๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2636_์น˜์ฆˆ

๋ฟŒ์•ผ._. 2022. 4. 13. 19:06

Gold V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/2636)

<์น˜์ฆˆ>

๋ฌธ์ œ 

 

์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํŒ์ด ์žˆ๊ณ , ๊ทธ ์œ„์— ์–‡์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ํŒ์˜ ๊ฐ€์žฅ์ž๋ฆฌ(<๊ทธ๋ฆผ 1>์—์„œ ๋„ค๋ชจ ์นธ์— X ์นœ ๋ถ€๋ถ„)์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“์—ฌ ์žˆ์ง€ ์•Š์œผ๋ฉฐ ์น˜์ฆˆ์—๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ตฌ๋ฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

์ด ์น˜์ฆˆ๋ฅผ ๊ณต๊ธฐ ์ค‘์— ๋†“์œผ๋ฉด ๋…น๊ฒŒ ๋˜๋Š”๋ฐ ๊ณต๊ธฐ์™€ ์ ‘์ด‰๋œ ์นธ์€ ํ•œ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ๋…น์•„ ์—†์–ด์ง„๋‹ค. ์น˜์ฆˆ์˜ ๊ตฌ๋ฉ ์†์—๋Š” ๊ณต๊ธฐ๊ฐ€ ์—†์ง€๋งŒ ๊ตฌ๋ฉ์„ ๋‘˜๋Ÿฌ์‹ผ ์น˜์ฆˆ๊ฐ€ ๋…น์•„์„œ ๊ตฌ๋ฉ์ด ์—ด๋ฆฌ๋ฉด ๊ตฌ๋ฉ ์†์œผ๋กœ ๊ณต๊ธฐ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. <๊ทธ๋ฆผ 1>์˜ ๊ฒฝ์šฐ, ์น˜์ฆˆ์˜ ๊ตฌ๋ฉ์„ ๋‘˜๋Ÿฌ์‹ผ ์น˜์ฆˆ๋Š” ๋…น์ง€ ์•Š๊ณ  ‘c’๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„๋งŒ ํ•œ ์‹œ๊ฐ„ ํ›„์— ๋…น์•„ ์—†์–ด์ ธ์„œ <๊ทธ๋ฆผ 2>์™€ ๊ฐ™์ด ๋œ๋‹ค.

<๊ทธ๋ฆผ 1> ์›๋ž˜ ์น˜์ฆˆ ๋ชจ์–‘

๋‹ค์‹œ ํ•œ ์‹œ๊ฐ„ ํ›„์—๋Š” <๊ทธ๋ฆผ 2>์—์„œ ‘c’๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„์ด ๋…น์•„ ์—†์–ด์ ธ์„œ <๊ทธ๋ฆผ 3>๊ณผ ๊ฐ™์ด ๋œ๋‹ค.

<๊ทธ๋ฆผ 2> ํ•œ ์‹œ๊ฐ„ ํ›„์˜ ์น˜์ฆˆ ๋ชจ์–‘
<๊ทธ๋ฆผ 3> ๋‘ ์‹œ๊ฐ„ ํ›„์˜ ์น˜์ฆˆ ๋ชจ์–‘

<๊ทธ๋ฆผ 3>์€ ์›๋ž˜ ์น˜์ฆˆ์˜ ๋‘ ์‹œ๊ฐ„ ํ›„ ๋ชจ์–‘์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์œผ๋ฉฐ, ๋‚จ์€ ์กฐ๊ฐ๋“ค์€ ํ•œ ์‹œ๊ฐ„์ด ๋” ์ง€๋‚˜๋ฉด ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฒ˜์Œ ์น˜์ฆˆ๊ฐ€ ๋ชจ๋‘ ๋…น์•„ ์—†์–ด์ง€๋Š” ๋ฐ๋Š” ์„ธ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. <๊ทธ๋ฆผ 3>๊ณผ ๊ฐ™์ด ์น˜์ฆˆ๊ฐ€ ๋…น๋Š” ๊ณผ์ •์—์„œ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„์–ด์งˆ ์ˆ˜๋„ ์žˆ๋‹ค.

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

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์—๋Š” ์‚ฌ๊ฐํ˜• ๋ชจ์–‘ ํŒ์˜ ์„ธ๋กœ์™€ ๊ฐ€๋กœ์˜ ๊ธธ์ด๊ฐ€ ์–‘์˜ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. ์„ธ๋กœ์™€ ๊ฐ€๋กœ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 100์ด๋‹ค. ํŒ์˜ ๊ฐ ๊ฐ€๋กœ์ค„์˜ ๋ชจ์–‘์ด ์œ— ์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ค„๊นŒ์ง€ ์ฃผ์–ด์ง„๋‹ค. ์น˜์ฆˆ๊ฐ€ ์—†๋Š” ์นธ์€ 0, ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š” ์นธ์€ 1๋กœ ์ฃผ์–ด์ง€๋ฉฐ ๊ฐ ์ˆซ์ž ์‚ฌ์ด์—๋Š” ๋นˆ์นธ์ด ํ•˜๋‚˜์”ฉ ์žˆ๋‹ค.

 

์ถœ๋ ฅ 

 

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

 

 

๋ฌธ์ œ ํ’€์ด

 

 - my solution (Python)

import sys
from collections import deque

def cheese(arr, queue, visited, cheese_temp):
    dx=[-1,1,0,0]
    dy=[0,0,-1,1]

    while queue:
        temp=queue.popleft()
        for i in range(4):
            x=temp[0]+dx[i]
            y=temp[1]+dy[i]
            if x>=0 and x<len(arr) and y>=0 and y<len(arr[0]): # ํŒ ๋ฒ”์œ„ ์•ˆ
                if arr[x][y]==0 and visited[x][y]==0: # ์น˜์ฆˆ x, ๋ฐฉ๋ฌธ x
                    queue.append([x,y])
                    visited[x][y]=1
                elif arr[x][y]==1 and visited[x][y]==0: # ์น˜์ฆˆ - ๊ณต๊ธฐ
                    cheese_temp.append([x,y])
                    visited[x][y]=1
    return cheese_temp

if __name__=='__main__':
    x,y=map(int,sys.stdin.readline().split())

    arr=[]
    for i in range(x):
        arr.append(list(map(int, sys.stdin.readline().split())))

    visited=[[0]*y for i in range(x)] # ๋ฐฉ๋ฌธ

    day=0 # ์‹œ๊ฐ„

    cheese_temp = deque([])
    queue=deque([[0,0]])
    cheese_cnt=len(cheese(arr, queue, visited, cheese_temp))


    while cheese_temp:
        day+=1
        cheese_temp=cheese(arr,cheese_temp, visited, deque([]))
        if len(cheese_temp)!=0:
            cheese_cnt=len(cheese_temp)

    print(day) # ์‹œ๊ฐ„
    print(cheese_cnt) # ๋ชจ๋‘ ๋…น๊ธฐ ํ•œ ์‹œ๊ฐ„ ์ „์— ๋‚จ์•„์žˆ๋Š” ์น˜์ฆˆ ์กฐ๊ฐ ์ˆ˜

- my solution (JAVA)

โ“

 

  • cheese ( ์น˜์ฆˆ ์ •๋ณด, queue, ๋ฐฉ๋ฌธ, ๋…น์€ ์น˜์ฆˆ)
    : dx, dy = ์ƒํ•˜์ขŒ์šฐ ์ด๋™
    : queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต -> ์ œ์ผ ์ฒ˜์Œ ๊ฐ’์„ pop -> ์ƒํ•˜์ขŒ์šฐ ์ด๋™ํ•˜์—ฌ ํŒ ๋ฒ”์œ„ ์•ˆ์ด๋ฉด -> ์น˜์ฆˆ๊ฐ€ ์—†๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด queue์— ์ถ”๊ฐ€ ๋ฐ ๋ฐฉ๋ฌธ ํ‘œ์‹œ but ์น˜์ฆˆ๊ฐ€ ์žˆ๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ๊ณต๊ธฐ์™€ ๋‹ฟ์ธ ๊ฒƒ์ด๋ฏ€๋กœ cheese_temp์— ์ถ”๊ฐ€ ๋ฐ ๋ฐฉ๋ฌธ ํ‘œ์‹œ
    : cheese_temp ๋ฐ˜ํ™˜
  • x, y = ํŒ์˜ ์„ธ๋กœ, ๊ฐ€๋กœ๊ธธ์ด ์ž…๋ ฅ
  • arr = ํŒ์˜ ์ •๋ณด
  • visited = ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
  • day = ์‹œ๊ฐ„
  • cheese_temp = ๋…น์€ ์น˜์ฆˆ
  • cheese_cnt = ์‹œ๊ฐ„๋งˆ๋‹ค ์น˜์ฆˆ ์กฐ๊ฐ ๋†“์—ฌ ์žˆ๋Š” ์นธ์˜ ๊ฐœ์ˆ˜
  • cheese_temp ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต -> day +1 -> ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜ ๊ฐ’ ์ €์žฅ ๋ฐ cheese_cnt์— cheese_temp ๊ธธ์ด ์ €์žฅ
  • day, cheese cnt ์ถœ๋ ฅ

 

โ“ ์ฒ˜์Œ์— ์™œ ํ‹€๋ ธ๋Š”๊ฐ€?

1์‹œ๊ฐ„ ๋งŒ์— ์น˜์ฆˆ๊ฐ€ ๋‹ค ๋…น์•„ ์—†์–ด์งˆ ๊ฒฝ์šฐ ํ•œ ์‹œ๊ฐ„ ์ „์— ๋‚จ์•„์žˆ๋Š” ์น˜์ฆˆ ์กฐ๊ฐ์ด ๋†“์—ฌ์žˆ๋Š” ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ‹€๋ ธ์—ˆ๋‹ค.


์ƒ๊ฐ๐Ÿค”

 

๋˜‘๊ฐ™์ด ๊ตฌํ˜„ํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ ์™œ JAVA๋กœ๋Š” ๋‹ต์ด ์•ˆ ๋‚˜์˜ค์ง€..?