Skip to content

Commit c81a371

Browse files
authored
Merge pull request #221 from AlgorithmStudy-Allumbus/present
feat: 6์›” 2์ฃผ์ฐจ ๋ฐœ์ œ ์ž๋ฃŒ ์—…๋กœ๋“œ
2 parents 4ffb07a + beb9ee7 commit c81a371

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
BOJ #1697. ์ˆจ๋ฐ”๊ผญ์งˆ (์‹ค๋ฒ„1)
3+
https://www.acmicpc.net/problem/1697
4+
์œ ํ˜•: Graph, BFS
5+
"""
6+
7+
# PR ์˜ฌ๋ฆด ๋•Œ ์ •๋‹ต๋„ ํ•จ๊ป˜ ๊ณต๊ฐœ ์˜ˆ์ •
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## ๐Ÿš€6์›” 2์ฃผ์ฐจ (4/07) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: BFS
2+
> ๋ฐœ์ œ์ž: ๊น€๋ฏผ์ •
3+
4+
> ์ฃผ์ œ: BFS
5+
6+
### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
7+
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ
8+
](./Study_BOJ_12851..pdf)
9+
10+
### ๐Ÿ“– ๋ฌธ์ œ
11+
- [๋ฐฑ์ค€ #12851. ์‰ฌ์šด๊ณ„๋‹จ์ˆ˜](https://www.acmicpc.net/problem/12851): BFS / ๊ณจ๋“œ4
12+
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_12851_์ˆจ๋ฐ”๊ผญ์งˆ2.py](./Study_BOJ_12851_์ˆจ๋ฐ”๊ผญ์งˆ2.py)
13+
14+
15+
### ๐Ÿ’ป ๊ณผ์ œ
16+
- [๋ฐฑ์ค€ #1697. ์ˆจ๋ฐ”๊ผญ์งˆ](https://www.acmicpc.net/problem/1697): BFS / ์‹ค๋ฒ„1
17+
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_1697_์ˆจ๋ฐ”๊ผญ์งˆ.py](./Assignment_BOJ_1697_์ˆจ๋ฐ”๊ผญ์งˆ.py)
625 KB
Binary file not shown.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""
2+
BOJ #12851. ์ˆจ๋ฐ”๊ผญ์งˆ2 (๊ณจ๋“œ4)
3+
https://www.acmicpc.net/problem/12851
4+
์œ ํ˜•: Graph, BFS
5+
"""
6+
7+
"""
8+
ํ’€์ด1
9+
"""
10+
import sys
11+
from collections import deque
12+
13+
input = sys.stdin.readline
14+
15+
MAX = 100000
16+
N, K = map(int, input().split())
17+
dist = [-1] * (MAX + 1)
18+
ways = [0] * (MAX + 1)
19+
20+
queue = deque([N])
21+
dist[N] = 0
22+
ways[N] = 1
23+
24+
while queue:
25+
x = queue.popleft()
26+
27+
for nx in (x - 1, x + 1, x * 2):
28+
# ๋ฒ”์œ„ ๋‚ด
29+
if 0 <= nx <= MAX:
30+
# ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์œ„์น˜
31+
if dist[nx] == -1:
32+
dist[nx] = dist[x] + 1 # ํ˜„์žฌ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ +1 ์ดˆ
33+
ways[nx] = ways[x] # ์ด์ „ ์œ„์น˜์—์„œ ์˜ค๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
34+
queue.append(nx)
35+
# ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ์ง€๋งŒ, ๊ฐ™์€ ์‹œ๊ฐ„์— ๋‹ค์‹œ ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ
36+
elif dist[nx] == dist[x] + 1:
37+
ways[nx] += ways[x] # ๊ธฐ์กด ๋ฐฉ๋ฒ• ์ˆ˜ + ์ƒˆ๋กœ์šด ๊ฒฝ๋กœ ์ˆ˜
38+
39+
print(dist[K])
40+
print(ways[K])
41+
42+
"""
43+
ํ’€์ด2
44+
"""
45+
import sys
46+
from collections import deque
47+
48+
N, K = map(int, sys.stdin.readline().split())
49+
queue = deque()
50+
queue.append(N)
51+
way = [0] * 100001 # ์ตœ๋Œ€ ํฌ๊ธฐ
52+
cnt, result = 0, 0
53+
while queue:
54+
a = queue.popleft()
55+
temp = way[a]
56+
if a == K: # ๋‘˜์ด ๋งŒ๋‚ฌ์„ ๋•Œ
57+
result = temp # ๊ฒฐ๊ณผ
58+
cnt += 1 # ๋ฐฉ๋ฌธ ํšŸ์ˆ˜ +1
59+
continue
60+
61+
for i in [a - 1, a + 1, a * 2]:
62+
if 0 <= i < 100001 and (way[i] == 0 or way[i] == way[a] + 1): # ๋ฒ”์œ„ ์•ˆ์—์žˆ๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜, ๋‹ค์Œ ๋ฐฉ๋ฌธ์ด ์ด์ „ ๋ฐฉ๋ฌธ+1์ด๋ฉด
63+
way[i] = way[a] + 1
64+
queue.append(i)
65+
print(result)
66+
print(cnt)
67+
68+
"""
69+
ํ’€์ด3
70+
"""
71+
from sys import stdin
72+
73+
input = stdin.readline
74+
from collections import deque
75+
76+
# ์ˆ˜๋นˆ ์œ„์น˜, ๋™์ƒ ์œ„์น˜
77+
N, K = map(int, input().split())
78+
79+
MAX_SIZE = 100001
80+
81+
que = deque()
82+
que.append(N)
83+
visited = [-1] * MAX_SIZE
84+
visited[N] = 0
85+
cnt = 0
86+
87+
while que:
88+
# ํ˜„ ์œ„์น˜
89+
current = que.popleft()
90+
# ๋„์ฐฉ
91+
if current == K:
92+
cnt += 1
93+
# ์ด๋™
94+
for next in [current * 2, current + 1, current - 1]:
95+
# ๋ฒ”์œ„ ๋‚ด
96+
if 0 <= next < MAX_SIZE:
97+
# ์ฒซ๋ฐฉ๋ฌธ ํ˜น์€ ๋ฐฉ๋ฌธ ์‹œ๊ฐ„์ด ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์ด๋ฏธ ์žˆ์Œ(๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์œ„ํ•ด)
98+
if visited[next] == -1 or visited[next] >= visited[current] + 1:
99+
visited[next] = visited[current] + 1
100+
que.append(next)
101+
102+
print(visited[K])
103+
print(cnt)
104+
105+
"""
106+
ํ’€์ด4
107+
"""
108+
import sys
109+
from collections import deque
110+
input = sys.stdin.readline
111+
112+
N, K = map(int, input().split())
113+
if N == K:
114+
print(0)
115+
print(1)
116+
sys.exit()
117+
118+
MAX = 100_000
119+
visited = [False] * (MAX + 1)
120+
visited[N] = True
121+
122+
q = deque([N])
123+
time = 0
124+
ans = 0
125+
found = False
126+
127+
while q and not found:
128+
time += 1
129+
level_size = len(q)
130+
# ์ด๋ฒˆ ๋ ˆ๋ฒจ์—์„œ ์ƒˆ๋กœ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๋“ค ๊ธฐ๋ก (๋ฐฉ๋ฌธ ํ‘œ์‹œ๋Š” ๋ ˆ๋ฒจ ๋์—)
131+
next_level = []
132+
133+
for _ in range(level_size):
134+
x = q.popleft()
135+
for nx in (x - 1, x + 1, x * 2):
136+
if 0 <= nx <= MAX:
137+
if nx == K:
138+
# ๋™์ƒ ์œ„์น˜๋ฅผ ๋ฐœ๊ฒฌํ•  ๋•Œ๋งˆ๋‹ค ์นด์šดํŠธ
139+
ans += 1
140+
found = True
141+
# ์•„์ง ํ•œ ๋ฒˆ๋„ ๋‹ค์Œ ๋ ˆ๋ฒจ์—์„œ enqueue๋˜์ง€ ์•Š์€ ๋…ธ๋“œ๋งŒ
142+
elif not visited[nx]:
143+
q.append(nx)
144+
next_level.append(nx)
145+
146+
# ์ด๋ฒˆ ๋ ˆ๋ฒจ์˜ ํƒ์ƒ‰์ด ๋๋‚œ ๋’ค, ์ƒˆ๋กœ enqueue๋œ ๋…ธ๋“œ๋“ค์— ํ•œ๊บผ๋ฒˆ์— ๋ฐฉ๋ฌธ ํ‘œ์‹œ
147+
for node in next_level:
148+
visited[node] = True
149+
150+
# ๋‹ต ์ถœ๋ ฅ
151+
print(time)
152+
print(ans)
153+

0 commit comments

Comments
ย (0)