๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
๊ฒฝ์์ ์ ์ผ | 30% | Gold_5 |
๋ฌธ์ ์์ฝ
์ ์ฌ๊ฐํ ์ํ๊ด ๊ธธ์ด N, ๋ฐ์ด๋ฌ์ค ์ข
๋ฅ K๊ฐ ์ฒซ ๋ฒ์งธ ์ค์ ์ฃผ์ด์ง๋ค(๋ฐ์ด๋ฌ์ค๋ 1~K๋ฒ๊น์ง ์๋ค๊ณ ๊ฐ์ ํ๋ค.
1~N+1์ค์๋ ์ํ๊ด์ ์๋ ์ํ(๋ฐ์ด๋ฌ์ค๊ฐ ์์ผ๋ฉด ๋ฐ์ด๋ฌ์ค ์ข ๋ฅ, ์์ผ๋ฉด 0)๊ฐ ์ฃผ์ด์ง๋ค.
N+2 ์ค์๋ S, X, Y๊ฐ ์ฃผ์ด์ง๋
์กฐ๊ฑด 1) 1 <=N <=200, 1 <=K <=1000, 0 <=S <=10000, 1 <=X, Y <=N
์กฐ๊ฑด 2) ๋ฐ์ด๋ฌ์ค๋ 1์ด๋ง๋ค ์, ํ, ์ข, ์ฐ ๋น ๊ณต๊ฐ์ ์นจํฌํ๋ค.(์ด๋ฏธ ๋ฐ์ด๋ฌ์ค๊ฐ ์กด์ฌํ๋ ๊ณณ์๋ ์นจํฌํ์ง ๋ชปํ๋ค)
์กฐ๊ฑด 3) ๋ฐ์ด๋ฌ์ค ์ข ๋ฅ์ ์ซ์๊ฐ ์์ ์์๋๋ก ์ฐ์ ์นจํฌ๊ถ์ ๊ฐ์ง๋ค
S์ด ํ์ ์ ์ฌ๊ฐํ ์ํ๊ด X๋ฒ์งธ row Y๋ฒ์งธ col์ ์ํ๋ฅผ ๋ฐํํ์ฌ๋ผ
ํ์ด
Step1. ๋ฐ์ด๋ฌ์ค ๊ด์ ์์ ์๊ฐํ ์ง ์ํ๊ด ๊ด์ ์์ ์๊ฐํ ์ง ํ๋จ
- ๋ฐ์ด๋ฌ์ค ๊ด์ ์์ ์๊ฐํ ๊ฒฝ์ฐ -> 1์ด๋ง๋ค ์ํ ์ ๋ฐ์ดํธ ํ์
- ์ํ๊ด ๊ด์ ์์ ์๊ฐํ ๊ฒฝ์ฐ -> ์ฃผ์์ ๋ฐ์ด๋ฌ์ค ์ฐพ์์ผ ํจ
Step2. ์ง๊ด์ ์ผ๋ก ๋ฐ์ด๋ฌ์ค ๊ด์ ์์ ์๊ฐํด ๋ณด์
- ๋ฐ์ด๋ฌ์ค๊ฐ ๋ค์ด์๋ ๊ณณ์ ์ ๋ณด๋ฅผ ๋ฐ์์
- 1์ด๋ง๋ค location update
- ๋ฐ์ด๋ฌ์ค๋ ์ธ๋ฑ์ค๊ฐ ์์ ๊ฒ ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋๊น -> heapq ์ด์ฉํ์
- ๋ ์์ธํ ๊ธฐ์ ํ๋ฉด, ์๊ฐ์ด ์ ์์๋ก ์ธ๋ฑ์ค๊ฐ ์์์๋ก ์ฐ์ ์์๋ฅผ ๊ฐ์ง๊ณ , ํ์ฌ ์์น์ ๋ํ ์ ๋ณด๊ฐ ์์ด์ผ ํ๋ฏ๋ก heapq์ [second, virus_idx, row, col]์ด๋ฐ ์์ผ๋ก ์ฝ์ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
- ์ต๋ O(๋ฐ์ด๋ฌ์ค๊ฐ ์๋ location ์*Second)์ด๋๊น K*10000 (K <=40000) ์๊ฐ๋ณต์ก๋๋ฅผ ์ํด ๋ ๋นจ๋ฆฌ ๋๋ผ ์ ์๋ ์กฐ๊ฑด์ด ํ์)
- XY์์น์ ๋ฐ์ด๋ฌ์ค๊ฐ ์ฝ์ (์ด๋ฏธ ์ฝ์ ๋ ๊ณณ์๋ ์นจํฌํ์ง ๋ชปํ๋๊น) : location [X-1][Y-1]==0
- ์๋ก ์ฝ์ ๋ ๋ฐ์ด๋ฌ์ค๊ฐ ์๋ ๊ฒฝ์ฐ : virus_q
์ฝ๋
import sys
import heapq
f = sys.stdin.readline
N, K= map(int, f().split())
location=[]
virus_q=[]
for row in range(N):
location.append(list(map(int, input().split())))
for col in range(N):
if location[row][col]!=0:
heapq.heappush(virus_q, [0, location[row][col], row, col]) #second, virus_idx, row_idx, col_idx
f = sys.stdin.readline
S, X, Y= map(int, f().split())
while virus_q and virus_q[0][0] <= (S-1):
if location[X-1][Y-1] != 0:
break
now = heapq.heappop(virus_q)
four = [[now[2]-1, now[3]], [now[2]+1, now[3]],[now[2], now[3]-1], [now[2], now[3]+1]]
for row_idx, col_idx in four:
if row_idx >= 0 and row_idx < N and col_idx >= 0 and col_idx < N:
if location[row_idx][col_idx] == 0:
location[row_idx][col_idx] = now[1]
heapq.heappush(virus_q, [now[0]+1, now[1], row_idx, col_idx])
print(location[X-1][Y-1])
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] ๊ฐ์ฌ ๊ฒ์ (Lv.4) 2024/4/18 (0) | 2024.04.18 |
---|---|
[Baekjoon]16234. ์ธ๊ตฌ์ด๋ (Gold_4) 2024/4/15 (0) | 2024.04.15 |
[Programmers] ๊ดํธ ๋ณํ (Lv.2) 2024/4/5 (0) | 2024.04.06 |
[Programmers] ๋ฌธ์์ด ์์ถ (Lv.2) 2024/4/2 (0) | 2024.04.02 |
[Programmers] ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น (Lv.3) 2024/4/1 (1) | 2024.04.01 |