Skip to content

Commit 3dab910

Browse files
committed
[22.02.22] 지금까지 풀었던 것 업로드
1 parent 8652d7f commit 3dab910

File tree

10 files changed

+286
-4
lines changed

10 files changed

+286
-4
lines changed

Python_BOJ_2022/14502.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 연구소
2+
from collections import deque
3+
import sys
4+
sys.setrecursionlimit(100001)
5+
input = lambda: sys.stdin.readline().rstrip()
6+
7+
n, m = map(int, input().split())
8+
graph = [list(map(int, input().split())) for _ in range(n)]
9+
tmp_graph = [[0] * m for _ in range(n)]
10+
11+
# 방향
12+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
13+
14+
# 안전지대를 카운트한다
15+
def safeAreaCnt():
16+
cnt = 0
17+
for i in range(n):
18+
for j in range(m):
19+
if tmp_graph[i][j] == 0:
20+
cnt += 1
21+
22+
return cnt
23+
24+
25+
# 바이러스가 퍼진다 - bfs
26+
def spreadVirus(x, y):
27+
queue = deque()
28+
queue.append((x, y))
29+
while queue:
30+
xx, yy = queue.popleft()
31+
for i in range(4):
32+
nx, ny = xx + dx[i], yy + dy[i]
33+
if 0 <= nx < n and 0 <= ny < m:
34+
if tmp_graph[nx][ny] == 0:
35+
tmp_graph[nx][ny] = 2
36+
queue.append((nx, ny))
37+
38+
# 벽을 친다 - 재귀
39+
ans = 0
40+
def recursive(depth):
41+
global ans
42+
# 벽을 3개 세웠을 때
43+
if depth == 3:
44+
# 그 그래프를 임시 그래프에 복사해서
45+
for i in range(n):
46+
for j in range(m):
47+
tmp_graph[i][j] = graph[i][j]
48+
49+
# 바이러스가 있는 자리에서 바이러스를 한번 퍼트려본다
50+
for i in range(n):
51+
for j in range(m):
52+
if tmp_graph[i][j] == 2:
53+
spreadVirus(i, j)
54+
55+
# 안전 지대의 갯수를 최댓값이 되도록 계속 업데이트
56+
ans = max(ans, safeAreaCnt())
57+
return
58+
59+
# 빈공간에 한번 벽을 새워본다
60+
for i in range(n):
61+
for j in range(m):
62+
if graph[i][j] == 0:
63+
graph[i][j] = 1
64+
depth += 1
65+
recursive(depth)
66+
graph[i][j] = 0
67+
depth -= 1
68+
69+
recursive(0)
70+
print(ans)

Python_BOJ_2022/15649.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# N과 M(1)
2+
import sys
3+
sys.setrecursionlimit(10000001)
4+
5+
n, m = map(int, input().split())
6+
ans = [0] * m
7+
chk = [False] * (n + 1)
8+
9+
def recursive(depth):
10+
if depth == m:
11+
for i in range(m):
12+
print(ans[i], end=' ')
13+
print()
14+
return
15+
16+
for i in range(1, n + 1):
17+
if chk[i]:
18+
continue
19+
ans[depth] = i
20+
chk[i] = True
21+
recursive(depth + 1)
22+
chk[i] = False
23+
24+
recursive(0)

Python_BOJ_2022/15650.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# N과 M(2)
2+
n, m = map(int, input().split())
3+
ans = [0] * m
4+
5+
def recursive(depth, start):
6+
if depth == m:
7+
for i in range(m):
8+
print(ans[i], end=' ')
9+
print()
10+
return
11+
12+
for i in range(start, n + 1):
13+
14+
ans[depth] = i
15+
recursive(depth + 1, i + 1)
16+
17+
18+
recursive(0, 1)

Python_BOJ_2022/15651.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# N과 M(3)
2+
import sys
23

3-
n, m = map(int, input().split())
4+
n, m = map(int, sys.stdin.readline().split())
45

56
ans = [0] * (n + 1)
67

7-
def combination(depth):
8+
def recursive(depth):
89
if depth == m:
910
for i in range(m):
1011
print(ans[i], end=' ')
@@ -13,6 +14,6 @@ def combination(depth):
1314

1415
for i in range(1, n + 1):
1516
ans[depth] = i
16-
combination(depth + 1)
17+
recursive(depth + 1)
1718

18-
combination(0)
19+
recursive(0)

Python_BOJ_2022/15651_2.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# N과 M(3)
2+
3+
n, m = map(int, input().split())
4+
ans = [0] * m
5+
6+
def recursive(depth):
7+
if depth == m:
8+
for i in range(m):
9+
print(ans[i], end=' ')
10+
print()
11+
return
12+
13+
for i in range(1, n + 1):
14+
ans[depth] = i
15+
recursive(depth + 1)
16+
17+
recursive(0)

Python_BOJ_2022/15652_2.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# N과 M(4)
2+
3+
n, m = map(int, input().split())
4+
ans = [0] * m
5+
6+
def recursive(depth, start):
7+
if depth == m:
8+
for i in range(m):
9+
print(ans[i], end=' ')
10+
print()
11+
return
12+
13+
for i in range(start, n + 1):
14+
ans[depth] = i
15+
recursive(depth + 1, i)
16+
17+
recursive(0, 1)

Python_BOJ_2022/15655.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# N과 M(6)
2+
3+
n, m = map(int, input().split())
4+
nums = list(map(int, input().split()))
5+
nums.sort()
6+
7+
ans = [0] * m
8+
9+
def recursive(depth, start):
10+
if depth == m:
11+
for i in range(m):
12+
print(ans[i], end=' ')
13+
print()
14+
return
15+
16+
for i in range(start, n + 1):
17+
ans[depth] = nums[i - 1]
18+
recursive(depth + 1, i + 1)
19+
20+
recursive(0, 1)

Python_BOJ_2022/15663.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# N과 M(9)
2+
3+
n, m = map(int, input().split())
4+
nums = list(map(int, input().split()))
5+
nums.sort()
6+
7+
chk = [False] * (n + 1)
8+
ans = [0] * m
9+
10+
def recursive(depth):
11+
if depth == m:
12+
for i in range(m):
13+
print(ans[i], end=' ')
14+
print()
15+
return
16+
17+
tmp_num = 0
18+
for i in range(n):
19+
if chk[i]:
20+
continue
21+
elif tmp_num != nums[i]:
22+
chk[i] = True
23+
ans[depth] = nums[i]
24+
tmp_num = nums[i]
25+
recursive(depth + 1)
26+
chk[i] = False
27+
28+
recursive(0)

Python_BOJ_2022/15686.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 치킨배달
2+
import sys
3+
sys.setrecursionlimit(10000001)
4+
# (r1, c1) (r2, c2) >> |r1 - r2| + |c1 - c2|
5+
6+
n, m = map(int, input().split()) # 마을 크기 n x n / 치킨집 갯수
7+
graph = [list(map(int, input().split())) for _ in range(n)]
8+
9+
home, kfc = [], [] # 집과 kfc 좌표 저장할 리스트
10+
ans = 123456789
11+
kfc_nums = []
12+
13+
for i in range(n):
14+
for j in range(n):
15+
if graph[i][j] == 1:
16+
home.append((i + 1, j + 1))
17+
elif graph[i][j] == 2:
18+
kfc.append((i + 1, j + 1))
19+
20+
21+
def solve(kfc_num, kfc_cnt): # kfc 몇 호점인지 나타내는 숫자, kfc갯수
22+
global ans
23+
# 기저조건
24+
if kfc_num > len(kfc):
25+
return
26+
if kfc_cnt == m:
27+
tmp = 0
28+
for x, y in home:
29+
min_distance = 123456789
30+
for i in kfc_nums: # 선택된 치킨집들
31+
kfc_x, kfc_y = kfc[i]
32+
min_distance = min(min_distance, abs(x - kfc_x) + abs(y - kfc_y))
33+
tmp += min_distance
34+
ans = min(ans, tmp)
35+
return
36+
37+
kfc_nums.append(kfc_num)
38+
solve(kfc_num + 1, kfc_cnt + 1)
39+
kfc_nums.pop()
40+
solve(kfc_num + 1, kfc_cnt)
41+
42+
solve(0, 0)
43+
print(ans)

Python_BOJ_2022/7576.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 토마토
2+
from collections import deque
3+
4+
# 입력
5+
n, m = map(int, input().split())
6+
graph = [list(map(int, input().split())) for _ in range(m)]
7+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
8+
9+
# 토마토가 익는다
10+
def find():
11+
while queue:
12+
xx, yy = queue.popleft()
13+
for i in range(4):
14+
nx, ny = xx + dx[i], yy + dy[i]
15+
if 0 <= nx < m and 0 <= ny < n:
16+
if graph[nx][ny] == 0:
17+
graph[nx][ny] = graph[xx][yy] + 1
18+
queue.append((nx, ny))
19+
20+
21+
# 토마토 존재하는 위치 체크후, 위치를 덱에 저장
22+
queue = deque()
23+
for i in range(m):
24+
for j in range(n):
25+
if graph[i][j] == 1:
26+
queue.append((i, j))
27+
28+
# 토마토를 익혀본다
29+
find()
30+
31+
# 만약 익지 않은 토마토가 있으면 -1 출력
32+
for i in range(m):
33+
for j in range(n):
34+
if graph[i][j] == 0:
35+
print(-1)
36+
exit()
37+
38+
# 토마토가 전부 익는데 걸리는 시간 확인
39+
ans = -123456789
40+
for i in range(m):
41+
for j in range(n):
42+
ans = max(graph[i][j], ans)
43+
44+
print(ans - 1)

0 commit comments

Comments
 (0)