๐Ÿ… ํ† ๋งˆํ† 

  • ์‹œ๊ฐ„ ์ œํ•œ: 1์ดˆ
  • ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ: 256MB

๐Ÿ“„ ๋ฌธ์ œ 

  • ์ฒ ์ˆ˜์˜ ํ† ๋งˆํ†  ๋†์žฅ์—์„œ๋Š” ํ† ๋งˆํ† ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ํฐ ์ฐฝ๊ณ ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ํ† ๋งˆํ† ๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฒฉ์ž ๋ชจ์–‘ ์ƒ์ž์˜ ์นธ์— ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์„œ ์ฐฝ๊ณ ์— ๋ณด๊ด€ํ•œ๋‹ค.

  • ์ฐฝ๊ณ ์— ๋ณด๊ด€๋˜๋Š” ํ† ๋งˆํ† ๋“ค ์ค‘์—๋Š” ์ž˜ ์ต์€ ๊ฒƒ๋„ ์žˆ์ง€๋งŒ, ์•„์ง ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๋“ค๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. 
  • ๋ณด๊ด€ ํ›„ ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚˜๋ฉด, ์ต์€ ํ† ๋งˆํ† ๋“ค์˜ ์ธ์ ‘ํ•œ ๊ณณ์— ์žˆ๋Š” ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๋“ค์€ ์ต์€ ํ† ๋งˆํ† ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ์ต๊ฒŒ ๋œ๋‹ค.
  • ํ•˜๋‚˜์˜ ํ† ๋งˆํ† ์˜ ์ธ์ ‘ํ•œ ๊ณณ์€ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ์•ž, ๋’ค ๋„ค ๋ฐฉํ–ฅ์— ์žˆ๋Š” ํ† ๋งˆํ† ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 
  • ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ์— ์žˆ๋Š” ํ† ๋งˆํ† ๋“ค์—๊ฒŒ๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ๋ชปํ•˜๋ฉฐ, ํ† ๋งˆํ† ๊ฐ€ ํ˜ผ์ž ์ €์ ˆ๋กœ ์ต๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.
  • ์ฒ ์ˆ˜๋Š” ์ฐฝ๊ณ ์— ๋ณด๊ด€๋œ ํ† ๋งˆํ† ๋“ค์ด ๋ฉฐ์น ์ด ์ง€๋‚˜๋ฉด ๋‹ค ์ต๊ฒŒ ๋˜๋Š”์ง€, ๊ทธ ์ตœ์†Œ ์ผ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์–ด ํ•œ๋‹ค.
  • ํ† ๋งˆํ† ๋ฅผ ์ฐฝ๊ณ ์— ๋ณด๊ด€ํ•˜๋Š” ๊ฒฉ์ž๋ชจ์–‘์˜ ์ƒ์ž๋“ค์˜ ํฌ๊ธฐ์™€ ์ต์€ ํ† ๋งˆํ† ๋“ค๊ณผ ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๋“ค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฉฐ์น ์ด ์ง€๋‚˜๋ฉด ๋ชจ๋‘ ์ต๋Š”์ง€, ๊ทธ ์ตœ์†Œ ์ผ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ.
  • ๋‹จ, ์ƒ์ž์˜ ์ผ๋ถ€ ์นธ์—๋Š” ํ† ๋งˆํ† ๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ž…๋ ฅ

  • ์ฒซ ์ค„์—๋Š” ์ƒ์ž์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ M, N์ด ์ฃผ์–ด์ง„๋‹ค.
  • M์€ ์ƒ์ž์˜ ๊ฐ€๋กœ ์นธ์˜ ์ˆ˜, N์€ ์ƒ์ž์˜ ์„ธ๋กœ ์นธ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ๋‹จ, 2 <= M, N <= 1000 ์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ํ•˜๋‚˜์˜ ์ƒ์ž์— ์ €์žฅ๋œ ํ† ๋งˆํ† ๋“ค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฆ‰, ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ƒ์ž์— ๋‹ด๊ธด ํ† ๋งˆํ† ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ•˜๋‚˜์˜ ์ค„์—๋Š” ์ƒ์ž ๊ฐ€๋กœ์ค„์— ๋“ค์–ด์žˆ๋Š” ํ† ๋งˆํ† ์˜ ์ƒํƒœ๊ฐ€ M๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ •์ˆ˜ 1์€ ์ต์€ ํ† ๋งˆํ† , ์ •์ˆ˜ 0์€ ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† , ์ •์ˆ˜ -1์€ ํ† ๋งˆํ† ๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์€ ์นธ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ํ† ๋งˆํ† ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

  • ์—ฌ๋Ÿฌ๋ถ„์€ ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์„ ๋•Œ๊นŒ์ง€์˜ ์ตœ์†Œ ๋‚ ์งœ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
  • ๋งŒ์•ฝ, ์ €์žฅ๋  ๋•Œ๋ถ€ํ„ฐ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์–ด์žˆ๋Š” ์ƒํƒœ์ด๋ฉด 0์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ณ , ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์ง€๋Š” ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด๋ฉด -1์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ’ก ๋ฌธ์ œ ํ’€์ด ์•„์ด๋””์–ด

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜: BFS
  • end ์กฐ๊ฑด:
    1. ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์€ ์ƒํƒœ: set(Tomato) == {1, -1} ์ธ ๊ฒฝ์šฐ
    2. ํ† ๋งˆํ† ๊ฐ€ ๋ชจ๋‘ ์ต์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ: 0์ธ ํ† ๋งˆํ†  ์ฃผ์œ„์— -1๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ

 

โŒ 85% ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค ์ฝ”๋“œ 

  • ์–ด๋–ค ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋ชปํ–ˆ์„์ง€ ๊ณ ๋ฏผ,,
  • ๋งˆ์ง€๋ง‰์— bfs๋Œ๋ฆฐ ํ›„ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์ต์—ˆ๋Š”์ง€ ์•ˆ์ต์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋นผ๋จน์Œ
from sys import stdin
from collections import deque
input = stdin.readline


M, N = map(int, input().split())
# M ๊ฐ€๋กœ, N ์„ธ๋กœ
tomatoes = []
set_tomatoes = set()
day = 0

for _ in range(N):
    inputs = list(map(int, input().split()))
    set_tomatoes.update(set(inputs))
    tomatoes.append(inputs)

# ํ˜„์žฌ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์žˆ์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
if len(set_tomatoes) == 1 and set_tomatoes.pop() == 1:
    print(0)
    exit()

# ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
if 1 not in set_tomatoes:
    print(-1)
    exit()

queue = deque([])

# ์ต์€ ํ† ๋งˆํ† ๋“ค queue์— ๋‹ด๊ธฐ
for x in range(N):
    for y in range(M):
        if tomatoes[x][y] == 1:
            queue.append((x, y))

# bfs
def bfs(graph, queue):
    # ์ƒ ํ•˜ ์ขŒ ์šฐ
    directionX = [-1, 1, 0, 0]
    directionY = [0, 0, -1, 1]

    next = deque([])

    while queue:
        x, y = queue.popleft()
        for i in range(len(directionX)):
            nx, ny = x + directionX[i], y + directionY[i]
            if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] == 0:
                graph[nx][ny] = 1
                next.append((nx, ny))
    return graph, next

while queue:
    tomatoes, queue = bfs(tomatoes, queue)
    day += 1
    # print(day, queue)
    # 
    # for tomato in tomatoes:
    #     print(tomato)

print(day - 1)

 

โœ… ์ •๋‹ต ์ฝ”๋“œ

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


M, N = map(int, input().split())
# M ๊ฐ€๋กœ, N ์„ธ๋กœ
tomatoes = []
set_tomatoes = set()
day = 0

for _ in range(N):
    inputs = list(map(int, input().split()))
    set_tomatoes.update(set(inputs))
    tomatoes.append(inputs)

# ํ˜„์žฌ ๋ชจ๋“  ํ† ๋งˆํ† ๊ฐ€ ์žˆ์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
if len(set_tomatoes) == 1 and set_tomatoes.pop() == 1:
    print(0)
    exit()

# ์ต์€ ํ† ๋งˆํ† ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
if 1 not in set_tomatoes:
    print(-1)
    exit()

queue = deque([])

# ์ต์€ ํ† ๋งˆํ† ๋“ค queue์— ๋‹ด๊ธฐ
for x in range(N):
    for y in range(M):
        if tomatoes[x][y] == 1:
            queue.append((x, y))

# bfs
def bfs(graph, queue):
    # ์ƒ ํ•˜ ์ขŒ ์šฐ
    directionX = [-1, 1, 0, 0]
    directionY = [0, 0, -1, 1]

    next = deque([])

    while queue:
        x, y = queue.popleft()
        for i in range(len(directionX)):
            nx, ny = x + directionX[i], y + directionY[i]
            if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] == 0:
                graph[nx][ny] = 1
                next.append((nx, ny))
    return graph, next

while queue:
    tomatoes, queue = bfs(tomatoes, queue)
    day += 1

for tomato in tomatoes:
    # ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
    if 0 in tomato:
        print(-1)
        exit()
print(day - 1)
728x90

+ Recent posts