Skip to content

Commit 183ef27

Browse files
authored
Merge pull request #158 from zaqquum/main
Hongjoo/3월 4주차 / 3개
2 parents f4d980d + 90a02dc commit 183ef27

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

Hongjoo/백준/계단수.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
https://www.acmicpc.net/problem/1562
3+
"""
4+
N = int(input())
5+
MOD = 1e9
6+
# dp[N번째 수][마지막 수][방문한 수 bitmasking(0~1023)]
7+
dp = [[[0]*1024 for _ in range(10)] for _ in range(N+1)]
8+
9+
for i in range(1, 10):
10+
dp[1][i][1<<i] = 1
11+
12+
# n = N번째 수
13+
for n in range(2, N+1):
14+
# i = 마지막 방문 숫자가 i
15+
for i in range(10):
16+
# 0~9까지 모든 수를 방문해야 한다는 조건이 있으므로, 방문 여부를 bitmasking을 통해 저장해야 함.
17+
for bit in range(1024):
18+
if i == 0:
19+
dp[n][i][bit | (1<<i)] += dp[n-1][i+1][bit]
20+
elif i == 9:
21+
dp[n][i][bit | (1<<i)] += dp[n-1][i-1][bit]
22+
else:
23+
dp[n][i][bit | (1<<i)] += dp[n-1][i-1][bit] + dp[n-1][i+1][bit]
24+
25+
dp[n][i][bit | (1<<i)] %= MOD
26+
27+
res = 0
28+
for i in range(10):
29+
res += dp[N][i][2**10-1]
30+
31+
print(int(res%MOD))

Hongjoo/백준/버섯농장.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import math
2+
import sys
3+
from collections import deque
4+
5+
def bfs(y_1 , x_1 , visited) :
6+
# 초기 위치 방문
7+
queue = deque()
8+
queue.append((y_1,x_1))
9+
visited[y_1][x_1] = True
10+
count = 0 # 방문할 node 개수
11+
# 큐 시작
12+
while queue :
13+
y,x =queue.popleft()
14+
count+=1
15+
for k in range(4):
16+
next_y, next_x = y + dy[k] , x + dx[k]
17+
# 필드 범위 포함 확인 & 방문 확인 &이동 가능 지역(0)
18+
if 0<=next_y<N and 0<=next_x<N :
19+
if not visited[next_y][next_x] and not field[next_y][next_x] :
20+
queue.append((next_y, next_x))
21+
visited[next_y][next_x] = True
22+
return count
23+
24+
25+
input =sys.stdin.readline
26+
N , M , K = map(int, input().split()) # field 범위, 씨앗 개수, 확산량
27+
X = M
28+
# 1. field 와 방문 등록 받기
29+
field = [[0 for _ in range(N)] for k in range(N)]
30+
visited = [[False for _ in range(N)] for k in range(N)]
31+
for i in range(N) :
32+
field[i]=list(map(int, input().split()))
33+
# 상하좌우
34+
dy = [-1,1,0,0]
35+
dx = [0,0, -1,1]
36+
37+
#2. bfs
38+
39+
for i in range(N):
40+
for j in range(N):
41+
#포자 가능 지역 + 방문 안 한곳
42+
if field[i][j] == 0 and not visited[i][j]:
43+
cur_node = bfs( i, j , visited)
44+
# 사용할 씨앗 청구하기
45+
X = X - math.ceil(cur_node/K)
46+
if X < 0 : #씨앗이 음수 남았으면 -> 불가능
47+
print("IMPOSSIBLE")
48+
exit()
49+
# 3. 마무리 출력
50+
if X == M : # 필드 자체가 농사 불가 -> 1개도 사용 못함
51+
print("IMPOSSIBLE")
52+
exit()
53+
elif X >= 0 : # 씨앗 남음
54+
print("POSSIBLE")
55+
print(X)

Hongjoo/백준/적록색약.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
https://www.acmicpc.net/problem/10026
3+
4+
5+
- NXN (<= 1000) , R,G,B
6+
- 상하좌우로 겹쳐진 "구역" = 1개
7+
- 적록 색약 R = G , B
8+
9+
10+
=(1) 정상
11+
R + G + B
12+
(2) 색약
13+
max(R,G) + B
14+
# 유형 : DFS
15+
16+
# flow
17+
18+
19+
1.(0,0) 부터 DFS 수행 + 방문(visited) 등록 : 1 X N*N
20+
2. 방문 리스트 : iT False not in visited , 까지 반복
21+
"""
22+
23+
24+
import sys
25+
# 1. 입력 받기
26+
n = int(input())
27+
field = [ [] for _ in range(n) ]
28+
colors_g = [[] , [] , []]
29+
for i in range(n):
30+
tmp = sys.stdin.readline().split()
31+
# print(f"tmp {tmp}")
32+
for t in range(len(tmp[0])) :
33+
field[i].append(tmp[0][t])
34+
if tmp[0][t] == "R" :
35+
colors_g[0].append([i,t])
36+
elif tmp[0][t] == "G":
37+
colors_g[1].append([i,t])
38+
else :
39+
colors_g[2].append([i,t])
40+
# print(f"colors-G {colors_g}")
41+
42+
# 2. DFS
43+
dx = [0,0,-1,1]
44+
dy = [-1,1,0,0,]
45+
def dfs(start_y,start_x, visited, flag , colors ):
46+
r , g, b= colors
47+
if field[start_y][start_x] == "R":
48+
r+=1
49+
elif field[start_y][start_x] == "G":
50+
g+=1
51+
else :
52+
b+=1
53+
queue = list()
54+
queue.append([start_y,start_x])
55+
visited.append([start_y,start_x])
56+
while queue :
57+
cur_y , cur_x= queue.pop()
58+
for i in range(4):
59+
next_y ,next_x = cur_y + dy[i] , cur_x + dx[i]
60+
if next_y>= n or next_y < 0 or next_x>= n or next_x < 0:
61+
print("####")
62+
continue
63+
64+
if flag : #적록색약 경우
65+
if field[cur_y][cur_x] in ["R","G"] and field[next_y][next_x] in ["R","G"] and [next_y,next_x] not in visited :
66+
queue.append([next_y,next_x])
67+
visited.append([next_y,next_x])
68+
continue
69+
if field[cur_y][cur_x] == field[next_y][next_x] and [next_y,next_x] not in visited : #색상이 같은 경우만 진행
70+
queue.append([next_y, next_x])
71+
visited.append([next_y, next_x])
72+
print(f"visited : {visited}")
73+
return [r,g,b] ,visited
74+
75+
76+
# 1. 정상
77+
# visited= [ [False for _ in range(n)] for k in range(n)]
78+
79+
colors = [ 0, 0, 0]
80+
for c in range(3) :
81+
category_colors = colors_g[c]
82+
visited = []
83+
while len(visited) <= len(category_colors): # R
84+
for y,x in category_colors :
85+
if [y,x] not in visited :
86+
colors , visited = dfs(y, x, visited , False , colors)
87+
print(f"i {colors}")
88+
89+
print(sum(colors))
90+
# visited = []
91+
# colors = [ 0, 0, 0]
92+
# for c in range(3) :
93+
# category_colors = colors_g[c]
94+
# while len(visited) <= len(category_colors ): # R
95+
# for y,x in category_colors :
96+
# if [y,x] not in visited :
97+
# colors , visited = dfs(y, x, visited , True,colors)
98+
# print(sum(colors))
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+

0 commit comments

Comments
 (0)