Skip to content

Commit 9fe278a

Browse files
authored
Merge pull request #157 from learntosurf/main
Learntosurf / 3์›” 4์ฃผ์ฐจ / 6๋ฌธ์ œ
2 parents ed2f89e + 1aeaac0 commit 9fe278a

10 files changed

+230
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
BOJ #4963. ์„ฌ์˜ ๊ฐœ์ˆ˜ (์‹ค๋ฒ„2)
3+
https://www.acmicpc.net/problem/4963
4+
์œ ํ˜•: BFS/DFS
5+
'''
6+
7+
import sys
8+
input = sys.stdin.readline
9+
from collections import deque
10+
11+
# 8๋ฐฉํ–ฅ (์ƒ, ํ•˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„  4๊ฐœ)
12+
dx = [-1, 1, 0, 0, -1, -1, 1, 1]
13+
dy = [0, 0, -1, 1, -1, 1, -1, 1]
14+
15+
def bfs(x, y, graph, w, h):
16+
queue = deque([(x, y)])
17+
graph[y][x] = 0 # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ (1 โ†’ 0 ๋ณ€๊ฒฝ)
18+
19+
while queue: # ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
20+
cx, cy = queue.popleft() # ํ˜„์žฌ ์ขŒํ‘œ ๊บผ๋‚ด๊ธฐ
21+
for i in range(8): # 8๋ฐฉํ–ฅ ํƒ์ƒ‰
22+
nx, ny = cx + dx[i], cy + dy[i] # ์ƒˆ๋กœ์šด ์ขŒํ‘œ ๊ณ„์‚ฐ
23+
if 0 <= nx < w and 0 <= ny < h and graph[ny][nx] == 1:
24+
graph[ny][nx] = 0 # ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
25+
queue.append((nx, ny)) # ํ์— ์ถ”๊ฐ€ (๋‹ค์Œ์— ํƒ์ƒ‰ํ•  ๊ณณ)
26+
27+
while True:
28+
w, h = map(int, input().split())
29+
if w == 0 and h == 0:
30+
break # ์ข…๋ฃŒ ์กฐ๊ฑด
31+
island = [list(map(int, input().split())) for _ in range(h)]
32+
33+
count = 0 # ์„ฌ์˜ ๊ฐœ์ˆ˜
34+
for y in range(h):
35+
for x in range(w):
36+
if island[y][x] == 1: # ์ƒˆ๋กœ์šด ์„ฌ ๋ฐœ๊ฒฌ
37+
count += 1
38+
bfs(x, y, island, w, h) # BFS ์‹คํ–‰
39+
40+
print(count)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## ๐Ÿš€ 1์›” 4์ฃผ์ฐจ (03/24) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: DFS/BFS
2+
> ๋ฐœ์ œ์ž: ์ •์ˆ˜๋ฏธ
3+
4+
### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
5+
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ](./Study_BOJ_10026.pdf)
6+
![image](https://github.com/user-attachments/assets/41909fa7-14ed-4678-9725-9ab0721b31ff)
7+
8+
9+
### ๐Ÿ“– ๋ฌธ์ œ
10+
- [๋ฐฑ์ค€ #10026. ์ ๋ก์ƒ‰์•ฝ](https://www.acmicpc.net/problem/10026): DFS/BFS / ๊ณจ๋“œ5
11+
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_10026_์ ๋ก์ƒ‰์•ฝ.py](./Study_BOJ_10026_์ ๋ก์ƒ‰์•ฝ.py)
12+
13+
### ๐Ÿ’ป ๊ณผ์ œ
14+
- [๋ฐฑ์ค€ #4963. ์„ฌ์˜ ๊ฐœ์ˆ˜](https://www.acmicpc.net/problem/4963): DFS/BFS, ์‹ค๋ฒ„2
15+
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_4963_์„ฌ์˜๊ฐœ์ˆ˜.py](./Assignment_BOJ_4963_์„ฌ์˜๊ฐœ์ˆ˜.py)
607 KB
Binary file not shown.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
BOJ #10026. ์ ๋ก์ƒ‰์•ฝ (๊ณจ๋“œ5)
3+
https://www.acmicpc.net/problem/10026
4+
์œ ํ˜•: BFS/DFS
5+
'''
6+
7+
import sys
8+
input = sys.stdin.readline
9+
from collections import deque
10+
11+
N = int(input().strip())
12+
grid = [list(input().strip()) for _ in range(N)]
13+
14+
# ๋ฐฉํ–ฅ ๋ฒกํ„ฐ
15+
dx = [-1, 1, 0, 0]
16+
dy = [0, 0, -1, 1]
17+
18+
def bfs(x, y, color, visited, grid):
19+
queue = deque([(x, y)])
20+
visited[x][y] = True
21+
22+
while queue:
23+
x, y = queue.popleft()
24+
for i in range(4):
25+
nx, ny = x + dx[i], y + dy[i]
26+
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
27+
if grid[nx][ny] == color:
28+
visited[nx][ny] = True
29+
queue.append((nx, ny))
30+
31+
def count_regions(grid, is_color_blind):
32+
visited = [[False] * N for _ in range(N)]
33+
count = 0
34+
35+
for i in range(N):
36+
for j in range(N):
37+
if not visited[i][j]: # ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ BFS ํƒ์ƒ‰ ์‹œ์ž‘
38+
count += 1
39+
color = grid[i][j]
40+
41+
# ์ ๋ก์ƒ‰์•ฝ ๋ชจ๋“œ๋ผ๋ฉด R๊ณผ G๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ
42+
if is_color_blind and color in "RG":
43+
bfs(i, j, "R", visited, grid)
44+
else:
45+
bfs(i, j, color, visited, grid)
46+
47+
return count
48+
49+
# ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ
50+
normal_count = count_regions(grid, is_color_blind=False)
51+
52+
# ์ ๋ก์ƒ‰์•ฝ์ธ ๊ฒฝ์šฐ (R๊ณผ G๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•œ grid ์ƒ์„ฑ)
53+
for i in range(N):
54+
for j in range(N):
55+
if grid[i][j] == 'G':
56+
grid[i][j] = 'R'
57+
58+
color_blind_count = count_regions(grid, is_color_blind=True)
59+
60+
print(normal_count, color_blind_count)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N, M = map(int, input().split())
5+
grid = [list(map(int, input().split())) for _ in range(N)]
6+
7+
dp = [[0] * M for _ in range(N)]
8+
dp[0][0] = grid[0][0]
9+
10+
def max_resources(N, M, grid):
11+
# ์ฒซ ๋ฒˆ์งธ ํ–‰ ์ฑ„์šฐ๊ธฐ
12+
for j in range(1, M):
13+
dp[0][j] = dp[0][j-1] + grid[0][j]
14+
15+
# ์ฒซ ๋ฒˆ์งธ ์—ด ์ฑ„์šฐ๊ธฐ
16+
for i in range(1, N):
17+
dp[i][0] = dp[i-1][0] + grid[i][0]
18+
19+
# DP ํ…Œ์ด๋ธ” ์ฑ„์šฐ๊ธฐ
20+
for i in range(1, N):
21+
for j in range(1, M):
22+
dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + grid[i][j]
23+
24+
return dp[N-1][M-1]
25+
26+
print(max_resources(N, M, grid))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
steps = list(map(int, input().split()))
5+
if steps:
6+
steps.pop() # ๋งˆ์ง€๋ง‰ 0 ์ œ๊ฑฐ
7+
8+
# dp ํ…Œ์ด๋ธ”
9+
INF = float('inf') # ๋ฌดํ•œ๋Œ€ ๊ฐ’
10+
dp = [[INF]*5 for _ in range(5)]
11+
dp[0][0] = 0
12+
13+
def move_power(start, end):
14+
if start == 0: # ์ค‘์•™์—์„œ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ
15+
return 2
16+
if start == end: # ๊ฐ™์€ ์ง€์ ์„ ์—ฐ์†ํ•ด์„œ ๋ฐŸ๋Š” ๊ฒฝ์šฐ
17+
return 1
18+
if abs(start - end) == 2: # ๋ฐ˜๋Œ€ํŽธ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ
19+
return 4
20+
return 3 # ์ธ์ ‘ํ•œ ์ง€์ ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ
21+
22+
for step in steps:
23+
new_dp = [[INF]*5 for _ in range(5)] # ์ƒˆ๋กœ์šด dp ํ…Œ์ด๋ธ”
24+
25+
for left in range(5):
26+
for right in range(5):
27+
if dp[left][right] == INF:
28+
continue
29+
30+
current_power = dp[left][right] # ํ˜„์žฌ ์œ„์น˜์˜ ํž˜์˜ ์†Œ๋ชจ๋Ÿ‰
31+
32+
# ์™ผ๋ฐœ์„ ์›€์ง์ด๋Š” ๊ฒฝ์šฐ (์˜ค๋ฅธ๋ฐœ์ด step์— ์žˆ์œผ๋ฉด ์•ˆ ๋จ)
33+
if step != right:
34+
new_dp[step][right] = min(new_dp[step][right], current_power + move_power(left, step))
35+
36+
# ์˜ค๋ฅธ๋ฐœ์„ ์›€์ง์ด๋Š” ๊ฒฝ์šฐ (์™ผ๋ฐœ์ด step์— ์žˆ์œผ๋ฉด ์•ˆ ๋จ)
37+
if step != left:
38+
new_dp[left][step] = min(new_dp[left][step], current_power + move_power(right, step))
39+
40+
dp = new_dp # dp ํ…Œ์ด๋ธ” ์—…๋ฐ์ดํŠธ
41+
42+
print(min(min(row) for row in dp))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n, k = map(int, input().split())
5+
coins = [int(input()) for _ in range(n)]
6+
7+
dp = [0] * (k + 1)
8+
dp[0] = 1 # 0์›์„ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 1 (์•„๋ฌด ๋™์ „๋„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ)
9+
10+
for coin in coins:
11+
for j in range(coin, k+1):
12+
dp[j] += dp[j - coin] # ์ ํ™”์‹
13+
14+
print(dp[k])
File renamed without changes.
File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
input = sys.stdin.readline
3+
from itertools import permutations
4+
5+
N = int(input().strip()) # ์งˆ๋ฌธ ํšŸ์ˆ˜
6+
questions = [list(map(int, input().split())) for _ in range(N)] # ์งˆ๋ฌธ ๋ฆฌ์ŠคํŠธ
7+
8+
# 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘ 3๊ฐœ๋ฅผ ๋ฝ‘๋Š” ์ˆœ์—ด
9+
nums = list(permutations(range(1,10), 3))
10+
cnt = 0 # ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜
11+
12+
# ๋‘ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ŠคํŠธ๋ผ์ดํฌ์™€ ๋ณผ์„ ๋ฐ˜ํ™˜
13+
def strike_and_ball(num1, num2):
14+
strike = sum(a == b for a, b in zip(num1, num2))
15+
ball = sum(a in num2 for a in num1) - strike
16+
return strike, ball
17+
18+
for num in nums:
19+
num_str = ''.join(map(str, num)) # ์ˆœ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
20+
valid = True # ๊ฐ€๋Šฅํ•œ ์ˆซ์ž์ธ์ง€ ํŒ๋ณ„
21+
22+
for q_num, q_strike, q_ball in questions:
23+
q_str = str(q_num) # ์ž…๋ ฅ๋œ ์ˆซ์ž๋„ ๋ฌธ์ž์—ด ๋ณ€ํ™˜
24+
strike, ball = strike_and_ball(num_str, q_str)
25+
if strike != q_strike or ball != q_ball:
26+
valid = False
27+
break # ํ•˜๋‚˜๋ผ๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ๋” ๋ณผ ํ•„์š” ์—†์Œ
28+
29+
if valid:
30+
cnt += 1
31+
32+
print(cnt)
33+

0 commit comments

Comments
ย (0)