Skip to content

Commit fc9a360

Browse files
authored
찬민 책 숙제 (#83)
1 parent 7a4fd68 commit fc9a360

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from collections import deque
2+
import heapq
3+
4+
n, k = map(int, input().split())
5+
arr = [[]]
6+
7+
temp = []
8+
q = deque()
9+
heap = []
10+
def dfs(count):
11+
while q:
12+
virus, x, y = q.popleft()
13+
for i in range(4):
14+
nx, ny = x + dx[i], y + dy[i]
15+
if 0 < nx <= n and 0 < ny <= n:
16+
if arr[nx][ny] == 0:
17+
arr[nx][ny] = virus
18+
q.append((virus, nx, ny))
19+
count -= 1
20+
21+
if count == 0:
22+
return
23+
24+
25+
dx = [-1, 1, 0, 0] # 상하좌우
26+
dy = [0, 0, -1, 1]
27+
28+
for i in range(1, n+1):
29+
arr.append(list(map(int, input().split())))
30+
arr[i].insert(0, 0)
31+
for j in range(n+1):
32+
if arr[i][j]:
33+
temp.append((arr[i][j], i, j))# 바이러스 종류, 행, 열
34+
#heapq.heappush(heap, (arr[i][j], (i, j)))
35+
#q.append((arr[i][j], i, j))# 바이러스 종류, 행, 열
36+
37+
38+
count = 1
39+
while True: # 너무 무식하게 넣었는데 더 깔끔한 방법은 없을까?
40+
for i in range(len(temp)):
41+
if temp[i][0] == count:
42+
q.append((temp[i][0], temp[i][1], temp[i][2]))
43+
count += 1
44+
if count > k:
45+
break
46+
47+
48+
49+
s, x, y = map(int, input().split())
50+
51+
for i in range(s):
52+
dfs(len(q))
53+
54+
print(arr[x][y])
55+
56+
57+
58+
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from _collections import deque
2+
3+
n, m, k, x = map(int, input().split())
4+
5+
arr = [[] for _ in range(n+1)]
6+
dist = [1000000000] * (n+1)
7+
8+
for i in range(m):
9+
a, b = map(int, input().split())
10+
arr[a].append(b)
11+
#arr[b].append(a)
12+
13+
q = deque()
14+
q.append((x, 1))
15+
16+
while q:
17+
start, cost = q.popleft()
18+
19+
for end in arr[start]:
20+
dist[end] = min(dist[end], cost)
21+
q.append((end, cost+1))
22+
23+
count = 0
24+
for i in range(1, n+1):
25+
if dist[i] == k:
26+
print(i)
27+
count += 1
28+
29+
if not count:
30+
print(-1)
31+
32+
33+
34+
'''
35+
4 4 2 1
36+
1 2
37+
1 3
38+
2 3
39+
2 4
40+
'''

0 commit comments

Comments
 (0)