Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24-alstjr7437 #187

Merged
merged 3 commits into from
May 18, 2024
Merged

24-alstjr7437 #187

merged 3 commits into from
May 18, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented May 14, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ† ๋งˆํ† 

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

20๋ถ„

๐Ÿง TMI

์ธํ„ฐ๋„ท ์ฐธ๊ณ  X
๋‹ค๋ฅธ ์ฝ”๋“œ ์ฐธ๊ณ  X
์›ํŠธ๋งŒ์— ํ‘ธ๋‹ˆ๊นŒ ๊ธฐ๋ถ„์ด ๋ฟŒ๋“ฏํ•˜๊ตฌ๋งŒ์œ 
image

์–ด๋Š์ •๋„ ๊ธฐ๋ณธ์ ์ธ BFS๋ฌธ์ œ๋Š” ์ด์ œ ํ’€ ์ˆ˜ ์žˆ์„๋“ฏ..




โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ฒ˜์Œ ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์œผ๋กœ๋Š”
แ„แ…ณแ„Œแ…ฅแ†จ

์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. tomato์—์„œ 1์ธ ๋ถ€๋ถ„ ์ฐพ์•„์„œ queue์— ๋„ฃ๊ธฐ
  2. BFS ๋Œ๋ฆฌ๊ธฐ(2~4)
  3. Visited ์ฒดํฌํ•ด์ฃผ๊ธฐ
  4. ์ต๊ฒŒ ๋˜๋Š” tomato์˜ ์ˆซ์ž๋ฅผ ์˜ฎ๊ฒจ์ง€๋Š” ํ† ๋งˆํ† ์˜ ์ˆซ์ž์— +1์„ ํ•˜๊ธฐ
    • ์ด ๋ถ€๋ถ„์„ ์•ˆํ•˜๊ฒŒ ๋˜๋ฉด BFS๊ฐ€ ๋Œ๋•Œ๋งˆ๋‹ค ์ˆซ์ž๊ฐ€ ์ปค์ง€๊ฑฐ๋‚˜ 1๋งŒ ๋˜์„œ ์ฒดํฌํ•˜๊ธฐ ํž˜๋“ค์–ด์ง
  5. ํ† ๋งˆํ† ์—์„œ 0์ด ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ
    • ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ๊ณณ์ด ์žˆ์œผ๋ฉด -1 ์ถœ๋ ฅ
  6. ํ† ๋งˆํ† ์—์„œ ์ตœ๋Œ€ ๊ฐ’ ์ฐพ๊ธฐ
    • ์ตœ๋Œ€ ๊ฐ’์—์„œ ์ฒซ๋‚ ์€ ๋นผ์•ผํ•˜๋ฏ€๋กœ -1

๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!!!

from collections import deque
import sys

input = sys.stdin.readline

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

n, m = map(int, input().split())
tomato = []
visited = [[0] * n for _ in range(m)]

for _ in range(m):
    tomato.append(list(map(int, input().split())))

queue = deque()

# 1. 1์ธ ๋ถ€๋ถ„ queue์— ๋„ฃ๊ธฐ
for y in range(m):
    for x in range(n):
        if tomato[y][x] == 1:
            queue.append((y,x))
            visited[y][x] = 1

# 2. BFS Go
while queue:
    y, x = queue.popleft()
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if nx >= n or nx < 0 or ny >= m or ny < 0:
            continue
        # 3. visited ์ฒดํฌ 
        if tomato[ny][nx] != -1 and visited[ny][nx] == 0:
            visited[ny][nx] = 1
            # 4. ์ต๊ฒŒ ๋œ ํ† ๋งˆํ† ์˜ ์ˆ˜๋ฅผ ์˜ฎ๊ฒจ์ง€๋Š” ํ† ๋งˆํ† ์˜ ์ˆ˜ + 1
            tomato[ny][nx] = tomato[y][x] + 1
            queue.append((ny, nx))

# 5. 0์ด ์žˆ์œผ๋ฉด -1 ์ถœ๋ ฅ
if any(0 in l for l in tomato):
    print(-1)
# 6. ์ตœ๋Œ€๊ฐ’ ์ฐพ์•„์„œ -1 ํ•˜์—ฌ ์ถœ๋ ฅ
else : 
    test = max(map(max, tomato))
    print(test - 1) 

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

๊ทธ๋ฆฌ๊ณ  VSCode์—์„œ ํ•˜๋ฉด tempCodeRunnerFile์ด ํ•œ๋ฒˆ์”ฉ ์ƒ๊ฒจ์„œ ์•„๋ž˜ ์„ค์ •์„ ์ผœ์„œ ์•ˆ์ƒ๊ธฐ๊ฒŒ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค!!!
image

์ฒซ PR์ธ๋ฐ ์ž˜๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!!๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฏผ์„์‹œ์น˜ Empty File!!!!!!!

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from collections import deque
import sys

def input(): return sys.stdin.readline().rstrip()

M, N = map(int ,input().split())

tomato_box = []
ripe_tomatoes = deque()
visited = set()
for row in range(N):
    row_tomato = list(map(int, input().split()))

    for col, tomato in enumerate(row_tomato):
        if tomato == 1: 
            visited.add((row, col))
            ripe_tomatoes.append((row, col, 0))

    tomato_box.append(row_tomato)

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

answer = 0
while ripe_tomatoes:
    now_row, now_col, now_day = ripe_tomatoes.popleft()
    answer = now_day
    
    for dir in range(4):
        new_row = now_row + dy[dir]
        new_col = now_col + dx[dir]
        
        if new_row < 0 or new_row >= N: continue
        if new_col < 0 or new_col >= M: continue
        if (new_row, new_col) in visited: continue
        if tomato_box[new_row][new_col] != 0: continue
    
        tomato_box[new_row][new_col] = 1
        visited.add((new_row, new_col))
        ripe_tomatoes.append((new_row, new_col, now_day+1))

flag = False
for row in range(N):
    for col in range(M):
        if tomato_box[row][col] == 0:
            flag = True
            break
    
    if flag: break
    
print(-1) if flag == True else print(answer)

์ง !!!!!!!!!!!!!!!!

@alstjr7437
Copy link
Member Author

์ง !!!!!!!!!!!!!!!!

์•„๋‹ˆ 10์‹œ 50๋ถ„์— ํ‘ธ๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ ๋Œ“๊ธ€์— ์ด๋ฏธ ์Šคํฌ๊ฐ€ ๋˜์žˆ๋”๋ผ๊ตฌ์š”
๋ฐ”๋กœ ๋ˆˆ ์งˆ๋ˆ ๊ฐ๊ณ  ํ’€๋Ÿฌ ๊ฐ”์Šต๋‹ˆ๋‹ค ๋•๋ถ„์—

@tgyuuAn
๋‹ค์‹œ ๋ฆฌ๋ทฐ ํ•˜์‹ญ์‡ผ

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ž˜ํ•œ๋‹ค....................

๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘

Comment on lines +36 to +37
if any(0 in l for l in tomato):
print(-1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค...................................................................... any ๋ง›์žˆ๋Š”๋ฐ์š” ...?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ตณ์ด for๋ฌธ์„ ๋‘๊ฐœ ์ค‘์ฒฉํ•  ํ•„์š”๊ฐ€ ์—†๋„ค์š” ๐Ÿ‘๐Ÿ‘

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ์ฒ˜์Œ์—๋Š” ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ํ–ˆ๋‹ค๊ฐ€ ๊ทธ๋ ‡๊ฒŒ ์ฐพ์œผ๋ฉด ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋”๋งŒ๋“ค์–ด์•ผ ๊ฒ ๋”๋ผ๊ตฌ์š”..
๊ทธ๋ž˜์„œ any๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์จ๋ดค์Šต๋‹ˆ๋‹ค~~~~

Comment on lines +33 to +34
tomato[ny][nx] = tomato[y][x] + 1
queue.append((ny, nx))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ ๋‚ ์งœ ์ •๋ณด๋ฅผ tomato ์— ๋”ํ•˜๋Š” ์‹์œผ๋กœ ์ €์žฅํ–ˆ๊ตฐ์š” ์ข‹์€๋””์š” ?????????

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 ์•„ ๊ทธ๋ƒฅ ์ด์ „ tamao ์—์„œ +1 ํ•ด๋ฒ„๋ฆฌ๋ฉด ๋”ฐ๋กœ days ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†๊ฒ ๋„ค์š” ใ„ทใ„ท

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๊ฒƒ๋„ ์ฒ˜์Œ์— max_daysํ•ด์„œ ๋„ฃ์—ˆ๋Š”๋ฐ ํ์— ๊ฐ™์ด ์ €์žฅ์„ ์•ˆํ•˜๋ฉด ๊ทธ๋ƒฅ BFS ๋Œ๋•Œ๋งˆ๋‹ค +1์ด ๋˜๋ฒ„๋ ค์„œ ์ถœ๋ ฅ์ด ์ด์ƒํ•ด์ง€๊ธธ๋ž˜ ์–ด๋–กํ•˜์ง€ ํ•˜๋‹ค๊ฐ€ ์ด๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!!~!

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‚˜์ค‘์— ํ’€์–ด๋ด์•ผ์ง€ ~ ํ•˜๊ณ  ๊นŒ๋จน๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ์— ํ’€์—ˆ์Šต๋‹ˆ๋‹ท

max_days ์ฒ˜๋ฆฌ์—์„œ ๊ณ ๋ฏผ ์ซŒ ํ–ˆ๋„ค์šฉ.. ๊ทผ๋ฐ ๋ฏผ์„๋‹˜ ์ฝ”๋“œ ๋ณด๋‹ˆ๊นŒ ๊ตณ์ด ๋”ฐ๋กœ ์ €์žฅ์•ˆํ•˜๊ณ  ๋ฐ”๋กœ ๋ฐฉํ–ฅ ํƒ์ƒ‰ ์ „ tomato์— +1๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๋„ค์š” ใ„ทใ„ท

๋ฌธ์ œ ํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ท ~~ โ€ผ๏ธโ€ผ๏ธ๐Ÿ’ช

import sys
from collections import deque

def input(): return sys.stdin.readline().rstrip()

M, N = map(int, input().split())
tomatos = [[int(i) for i in input().split()] for _ in range(N)]
visited = [[0 for _ in range(M)] for _ in range(N)]
dir = [(0,1), (0,-1), (1,0), (-1,0)]
q = deque()
max_days = 0
check = True # ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋Š”์ง€

# ์ต์€ ํ† ๋งˆํ†  ๋„ฃ๊ธฐ
for i in range(N):
    for k in range(M):
        if tomatos[i][k] == 1:
            q.append((0, i,k))
            visited[i][k] == 1

while q:
    days, x, y = q.popleft()
    for dir_x, dir_y in dir:
        if 0<= x+dir_x < N and 0<= y+dir_y < M:
            if tomatos[x+dir_x][y+dir_y] == 0 and visited[x+dir_x][y+dir_y] == 0:
                tomatos[x+dir_x][y+dir_y] = 1 # ์ต์€ ํ† ๋งˆํ†  ๋งŒ๋“ค๊ธฐ
                visited[x+dir_x][y+dir_y] = 1
                max_days = days+1
                q.append((days +1, x+dir_x, y+dir_y))

for i in range(N):
    for k in range(M):
        if tomatos[i][k] == 0:
            check = False

if check:
    print(max_days)
else:
    print(-1)
        

Comment on lines +33 to +34
tomato[ny][nx] = tomato[y][x] + 1
queue.append((ny, nx))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 ์•„ ๊ทธ๋ƒฅ ์ด์ „ tamao ์—์„œ +1 ํ•ด๋ฒ„๋ฆฌ๋ฉด ๋”ฐ๋กœ days ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†๊ฒ ๋„ค์š” ใ„ทใ„ท

Comment on lines +36 to +37
if any(0 in l for l in tomato):
print(-1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22 ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ตณ์ด for๋ฌธ์„ ๋‘๊ฐœ ์ค‘์ฒฉํ•  ํ•„์š”๊ฐ€ ์—†๋„ค์š” ๐Ÿ‘๐Ÿ‘

if any(0 in l for l in tomato):
print(-1)
else :
test = max(map(max, tomato))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ๋ผ.. ์š”๋ ‡๊ฒŒ ํ•˜๋ฉด tomato์— ๋Œ€ํ•œ ์ตœ๋Œ€๊ฐ’์„ ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๊ตฐ์š”..
์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์„œ ์ข‹๋„ค์š” ใ„ทใ„ท ๐Ÿ‘ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค (_ _)

@tgyuuAn
Copy link
Member

tgyuuAn commented May 16, 2024

๋‚˜์ค‘์— ํ’€์–ด๋ด์•ผ์ง€ ~ ํ•˜๊ณ  ๊นŒ๋จน๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ์— ํ’€์—ˆ์Šต๋‹ˆ๋‹ท

max_days ์ฒ˜๋ฆฌ์—์„œ ๊ณ ๋ฏผ ์ซŒ ํ–ˆ๋„ค์šฉ.. ๊ทผ๋ฐ ๋ฏผ์„๋‹˜ ์ฝ”๋“œ ๋ณด๋‹ˆ๊นŒ ๊ตณ์ด ๋”ฐ๋กœ ์ €์žฅ์•ˆํ•˜๊ณ  ๋ฐ”๋กœ ๋ฐฉํ–ฅ ํƒ์ƒ‰ ์ „ tomato์— +1๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๋„ค์š” ใ„ทใ„ท

๋ฌธ์ œ ํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ท ~~ โ€ผ๏ธโ€ผ๏ธ๐Ÿ’ช

import sys

from collections import deque



def input(): return sys.stdin.readline().rstrip()



M, N = map(int, input().split())

tomatos = [[int(i) for i in input().split()] for _ in range(N)]

visited = [[0 for _ in range(M)] for _ in range(N)]

dir = [(0,1), (0,-1), (1,0), (-1,0)]

q = deque()

max_days = 0

check = True # ์ต์ง€ ์•Š์€ ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋Š”์ง€



# ์ต์€ ํ† ๋งˆํ†  ๋„ฃ๊ธฐ

for i in range(N):

    for k in range(M):

        if tomatos[i][k] == 1:

            q.append((0, i,k))

            visited[i][k] == 1



while q:

    days, x, y = q.popleft()

    for dir_x, dir_y in dir:

        if 0<= x+dir_x < N and 0<= y+dir_y < M:

            if tomatos[x+dir_x][y+dir_y] == 0 and visited[x+dir_x][y+dir_y] == 0:

                tomatos[x+dir_x][y+dir_y] = 1 # ์ต์€ ํ† ๋งˆํ†  ๋งŒ๋“ค๊ธฐ

                visited[x+dir_x][y+dir_y] = 1

                max_days = days+1

                q.append((days +1, x+dir_x, y+dir_y))



for i in range(N):

    for k in range(M):

        if tomatos[i][k] == 0:

            check = False



if check:

    print(max_days)

else:

    print(-1)

        

ํ™์ฅฌ๋ฅด ๋‚˜๋ž‘ ์™„์กด ๋น„์Šท ๋Œ€๋ฐ•

@alstjr7437
Copy link
Member Author

๋‚˜์ค‘์— ํ’€์–ด๋ด์•ผ์ง€ ~ ํ•˜๊ณ  ๊นŒ๋จน๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ์— ํ’€์—ˆ์Šต๋‹ˆ๋‹ท

max_days ์ฒ˜๋ฆฌ์—์„œ ๊ณ ๋ฏผ ์ซŒ ํ–ˆ๋„ค์šฉ.. ๊ทผ๋ฐ ๋ฏผ์„๋‹˜ ์ฝ”๋“œ ๋ณด๋‹ˆ๊นŒ ๊ตณ์ด ๋”ฐ๋กœ ์ €์žฅ์•ˆํ•˜๊ณ  ๋ฐ”๋กœ ๋ฐฉํ–ฅ ํƒ์ƒ‰ ์ „ tomato์— +1๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๋„ค์š” ใ„ทใ„ท

๋ฌธ์ œ ํ‘ธ๋Š๋ผ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ท ~~ โ€ผ๏ธโ€ผ๏ธ๐Ÿ’ช

์ €๋„ ๋‚ ์งœ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์—์„œ ์ฒ˜์Œ์— max_days๋ฅผ ๋‹ค 1์”ฉ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ ํ–ˆ๋Š”๋ฐ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ์˜ˆ์ œ์—์„œ 25์ธ๊ฐ€ ๋‚˜์˜ค๋”๋ผ๊ตฌ์š” ใ„ทใ„ท

๊ทธ๋ž˜์„œ ์œ„์™€ ๊ฐ™์ด ๋ฐฉ์‹์„ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค! ์–ด์งœํ”ผ 0์ด ์žˆ์œผ๋ฉด ๋Œ์•„๊ฐ€๋‹ˆ๊นŒ 1๋กœ ๋ฐ”๊ฟ”์ฃผ์ง€ ์•Š๊ณ  ๋‚ ์งœ๋กœ ๋ฐ”๊ฟ”์คฌ์Šต๋‹ˆ๋‹น

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋จผ์ € ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ฏผ์„๋‹˜์ด๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™์ด ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  visited ๋Š” ์“ฐ์ง€ ์•Š์•˜๊ตฌ์š”.

private val dx_1 = listOf(0, 0, 1, -1)
private val dy_1 = listOf(1, -1, 0, 0)
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (m, n) = br.readLine().split(" ").map { it.toInt() }
    val graph = Array(n) { Array(m) { 0 } }
    val q = ArrayDeque<Pair<Int, Int>>()

    repeat(n) {
        val line = br.readLine().split(" ").map { it.toInt() }
        line.forEachIndexed { index: Int, item: Int ->
            graph[it][index] = item
            if (item == 1) {
                q.add(it to index)
            }
        }
    }

    while (q.isNotEmpty()) {
        val now = q.removeFirst()
        for (i in 0..3) {
            val nextX = now.first + dx_1[i]
            val nextY = now.second + dy_1[i]
            if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) {
                continue
            }
            if (graph[nextX][nextY] != 0) {
                continue
            }
            graph[nextX][nextY] = graph[now.first][now.second] + 1
            q.add(nextX to nextY)
        }
    }

//flatten() -> 2์ฐจ์›์„ 1์ฐจ์›์œผ๋กœ ๋ฐ”๊พผ๋‹ค
    val flattenGraph = graph.flatten()
    if (flattenGraph.contains(0)) println(-1) else println(flattenGraph.max() - 1)
}

๋งˆ์ง€๋ง‰์— max ๊ฐ’ ๊ณ„์‚ฐ์ด๋‚˜ 0์„ ์ฐพ๋Š” ๋ถ€๋ถ„์—์„œ ์‹œ๊ฐ„์„ ๋” ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†์„๊นŒ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ์ฒ˜์Œ ๋ฐ์ดํ„ฐ ๋ฐ›์„ ๋•Œ 0 ์ธ ๊ฒƒ๋“ค (์•ˆ์ต์€ ํ† ๋งˆํ† ) ๋“ค ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•ด๋†“๊ณ  ํ•˜๋‚˜ ํƒ์ƒ‰ํ•  ๋•Œ๋งˆ๋‹ค count --
  2. max ๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ๊ณ„์† ์—…๋ฐ์ดํŠธ
private val dx_1 = listOf(0, 0, 1, -1)
private val dy_1 = listOf(1, -1, 0, 0)
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (m, n) = br.readLine().split(" ").map { it.toInt() }
    val graph = Array(n) { Array(m) { 0 } }
    val q = ArrayDeque<Pair<Int, Int>>()

    // ์š”๊ฑฐ ์ถ”๊ฐ€
    var tomatoCount = 0
    var maxDate = 1

    repeat(n) {
        val line = br.readLine().split(" ").map { it.toInt() }
        line.forEachIndexed { index: Int, item: Int ->
            graph[it][index] = item
            if (item == 1) q.add(it to index)
            if (item == 0) tomatoCount++ // ์š”๊ฑฐ ์ถ”๊ฐ€
        }
    }

    while (q.isNotEmpty()) {
        val now = q.removeFirst()
        for (i in 0..3) {
            val nextX = now.first + dx_1[i]
            val nextY = now.second + dy_1[i]
            if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) {
                continue
            }
            if (graph[nextX][nextY] != 0) {
                continue
            }
            val date = graph[now.first][now.second] + 1
            graph[nextX][nextY] = date
            q.add(nextX to nextY)
            tomatoCount-- // ์š”๊ฑฐ ์ถ”๊ฐ€
            maxDate = date // ์š”๊ฑฐ ์ถ”๊ฐ€
        }
    }

    if (tomatoCount > 0) println(-1) else println(maxDate - 1) // ์ด์ œ ๊ณ„์‚ฐ ์•ˆํ•ด๋„ ๋จ!
}
image

์‹ค์ œ๋กœ ์กฐ๊ธˆ ์ค„์—ˆ์Šต๋‹ˆ๋‹ค..์œ ์˜๋ฏธํ•œ ์ฐจ์ด์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ

ํ† ๋งˆํ†  ์žฌ๋ฐŒ์—ˆ๋„ค์š”!

Comment on lines +31 to +34
if tomato[ny][nx] != -1 and visited[ny][nx] == 0:
visited[ny][nx] = 1
tomato[ny][nx] = tomato[y][x] + 1
queue.append((ny, nx))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‚ ์งœ ์ •๋ณด๋ฅผ ํ† ๋งˆํ† ์— ๋”ํ•œ๋‹ค๋ฉด visited ์—†์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™๋„ค์š”! ์–ด์ฐจํ”ผ 0์ด๋ฉด ํƒ์ƒ‰ํ•˜์ง€ ์•Š์€ ํ† ๋งˆํ† ์ผํ…Œ๋‹ˆ๊นŒ์š”~

Suggested change
if tomato[ny][nx] != -1 and visited[ny][nx] == 0:
visited[ny][nx] = 1
tomato[ny][nx] = tomato[y][x] + 1
queue.append((ny, nx))
if tomato[ny][nx] == 0:
tomato[ny][nx] = tomato[y][x] + 1
queue.append((ny, nx))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants