๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด/DFS,BFS

[๋ฐฑ์ค€]2573๋ฒˆ: ๋น™์‚ฐ

by syLim___ 2023. 7. 28.
728x90

https://www.acmicpc.net/problem/2573

 

2573๋ฒˆ: ๋น™์‚ฐ

์ฒซ ์ค„์—๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ N๊ณผ M์ด ํ•œ ๊ฐœ์˜ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 3 ์ด์ƒ 300 ์ดํ•˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ๋ฐฐ์—ด์˜ ๊ฐ ํ–‰์„

www.acmicpc.net


bfs๋กœ ์ •์งํ•˜๊ฒŒ ํ’€์–ด์ฃผ์—ˆ๋‹ค..!

๋‚ด ์ฝ”๋“œ๋กœ๋Š” python3๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๊ณ , pypy3๋กœ๋Š” ์ •๋‹ต ํŒ์ •์„ ๋ฐ›์•˜๋‹ค.

 

1. ๋น™์‚ฐ์ด ๋ช‡๋ฉ์–ด๋ฆฌ์ธ์ง€ ํ™•์ธ

1-1. ๋‘ ๋ฉ์–ด๋ฆฌ ์ด์ƒ์ด๋ผ๋ฉด, ๊ฒฝ๊ณผ์‹œ๊ฐ„(year)์„ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

2. ๋น™์‚ฐ ๋…น์ด๊ธฐ ์ž‘์—… ๋ฐ ๊ฒฝ๊ณผ์‹œ๊ฐ„(๋…„) ์ฆ๊ฐ€

3. ๋น™์‚ฐ์ด ๋ชจ๋‘ ๋…น์•˜๋Š”์ง€ ์ฒดํฌํ•˜๊ณ , ๋ชจ๋‘ ๋…น์•˜๋‹ค๋ฉด 0์„ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

 


 

์ฝ”๋“œ

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

n,m = map(int, input().split())

arr = []
for _ in range(n):
  arr.append( list(map(int, input().split())) )

visited = [[0 for _ in range(m)] for _ in range(n)]


answer = 0

def bfs(x,y):
  q = deque()
  q.append((x,y))
  visited[x][y] = 1
  dx, dy = [-1,0,1,0], [0,1,0,-1]
  while q:
    x,y = q.popleft()
    for i in range(4):
      nx, ny = x + dx[i], y + dy[i]
      if 0 <= nx < n and 0 <= ny < m and visited[nx][ny] == 0 and arr[nx][ny]>0:
        visited[nx][ny] = 1
        q.append((nx,ny))

# ๋น™์‚ฐ์ด ํ•œ๋ฉ์–ด๋ฆฌ์ธ์ง€ ์ฒดํฌ
def check():
  # visited ์ดˆ๊ธฐํ™”
  for i in range(n):
    for j in range(m):
      visited[i][j] = 0
  cnt = 0
  for i in range(n):
    for j in range(m):
      if arr[i][j] != 0 and visited[i][j] == 0:
        bfs(i,j)
        cnt += 1
  return cnt

q = deque()

def melt(x,y):
  # ๋ฐฉ๋ฌธ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
  for i in range(n):
    for j in range(m):
      visited[i][j] = 0

  q.append((x,y))
  visited[x][y] = 1

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

  for i in range(4):
    nx, ny = x + dx[i], y + dy[i]
    if 0<=nx<n and 0<=ny<m and arr[nx][ny]==0 and arr[x][y]>0:
      arr[x][y] -= 1

  while q:
    x, y = q.popleft()

    for i in range(4):
      nx, ny = x + dx[i], y + dy[i]
      if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and arr[nx][ny]>0:
        visited[nx][ny] = 1
        for j in range(4):
          tx, ty = nx + dx[j], ny + dy[j]
          if 0 <= tx < n and 0 <= ty < m and not visited[tx][ty] and arr[tx][ty]==0:
            if arr[nx][ny] > 0:
              arr[nx][ny] -= 1
        q.append((nx,ny))
  

while True:

  # ๋น™์‚ฐ์ด ๋ช‡๋ฉ์–ด๋ฆฌ์ธ์ง€ ์ฒดํฌ
  if check() > 1:
    print(answer)
    break

  # ๋น™์‚ฐ ๋…น๋Š” ์ž‘์—…
  flag = False
  for i in range(n):
    for j in range(m):
      if arr[i][j] > 0:
        melt(i,j)
        flag = True
        break
    if flag:
      break
  answer += 1 # 1๋…„ ์ถ”๊ฐ€

  # ๋น™์‚ฐ์ด ๋‹ค ๋…น์•˜๋Š”์ง€ ์ฒดํฌ
  flag = False
  for i in range(n):
    for j in range(m):
      if arr[i][j] != 0:
        flag = True
        break
    if flag:
      break
  if flag == False:
    print(0)
    break
728x90