Skip to content

Commit a93e43e

Browse files
authored
Merge pull request #223 from AlgorithmStudy-Allumbus/YoonYn9915
YoonYn9915/ 6์›” 2์ฃผ์ฐจ/ 3๋ฌธ์ œ
2 parents 6f14148 + cb96cd3 commit a93e43e

4 files changed

+111
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
3+
import sys
4+
from collections import deque
5+
6+
inp = sys.stdin.readline
7+
8+
N, K = map(int, inp().strip().split())
9+
# ํ•ด๋‹น ์œ„์น˜๋กœ ๋„๋‹ฌํ•œ ์ตœ์†Œ ์‹œ๊ฐ„ ์ €์žฅ
10+
visited = [-1] * (100_000 + 1)
11+
visited[N] = 0
12+
13+
# ์ตœ์†Œ์‹œ๊ฐ„์— ํ•ด๋‹น ์œ„์น˜๋กœ ๋„๋‹ฌํ•œ ํšŸ์ˆ˜ ์ €์žฅ
14+
ways = [-1] * (100_000 + 1)
15+
ways[N] = 1
16+
17+
# (์œ„์น˜, ์‹œ๊ฐ„) ํ˜•์‹
18+
queue = deque()
19+
queue.append((N,0))
20+
21+
dx = [-1, 1, 2]
22+
23+
min_time = -1
24+
25+
while queue:
26+
subin_loc,time = queue.popleft()
27+
28+
# ์ˆ˜๋นˆ์ด๊ฐ€ ๋™์ƒ์—๊ฒŒ ๋„๋‹ฌํ•œ ์‹œ๊ฐ„๊นŒ์ง€๋งŒ bfsํƒ์ƒ‰ํ•˜๊ณ  ๊ทธ ํ›„์— ์ข…๋ฃŒ
29+
if min_time != -1 and min_time +2 == time:
30+
break
31+
32+
# ์ˆ˜๋นˆ์ด๊ฐ€ ๋™์ƒ์—๊ฒŒ ๋„๋‹ฌํ•œ ์‹œ๊ฐ„์ฒดํฌ
33+
if min_time == -1 and subin_loc == K:
34+
min_time = visited[subin_loc]
35+
36+
37+
# ์ˆ˜๋นˆ์˜ ์œ„์น˜์—์„œ 3๊ฐ€์ง€ ์ด๋™
38+
for i in range(3):
39+
if i == 2:
40+
new_loc = subin_loc * dx[i]
41+
else:
42+
new_loc = subin_loc + dx[i]
43+
44+
# ์ƒˆ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ์•ˆ
45+
if 0 <= new_loc <= 100_000:
46+
# ์ƒˆ๋กœ ๋ฐฉ๋ฌธํ•œ ์œ„์น˜๊ฐ€ ์ด์ „์— ์™€๋ณด์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด,
47+
if visited[new_loc] == -1:
48+
# ๋ฐฉ๋ฌธ ์‹œ๊ฐ„ ์„ค์ •ํ•ด์ฃผ๊ณ  ๊ฒฝ๋กœ ์ดˆ๊ธฐํ™”
49+
visited[new_loc] = visited[subin_loc] + 1
50+
ways[new_loc] = ways[subin_loc]
51+
queue.append((new_loc, time +1))
52+
53+
# ์ƒˆ๋กœ ๋ฐฉ๋ฌธํ•œ ์œ„์น˜๊ฐ€ ์ด์ „์— ์™€๋ดค๋‹ค๋ฉด, ์ง€๊ธˆ ์‹œ๊ฐ„์ด ์ด์ „์— ์™€๋ดค๋˜ ์‹œ๊ฐ„๊ณผ ๊ฐ™์•„์•ผ ํ•จ.
54+
else:
55+
if visited[new_loc] == visited[subin_loc] + 1:
56+
# ๊ฒฝ๋กœ ๋ˆ„์ ์‹œ์ผœ์คŒ
57+
ways[new_loc] = ways[new_loc] + ways[subin_loc]
58+
59+
60+
print(visited[K])
61+
print(ways[K])
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
from collections import deque
3+
4+
inp = sys.stdin.readline
5+
6+
N, K = map(int, inp().strip().split())
7+
# ํ•ด๋‹น ์œ„์น˜๋กœ ๋„๋‹ฌํ•œ ์ตœ์†Œ ์‹œ๊ฐ„ ์ €์žฅ
8+
visited = [-1] * (100_000 + 1)
9+
visited[N] = 0
10+
11+
# (์œ„์น˜, ์‹œ๊ฐ„) ํ˜•์‹
12+
queue = deque()
13+
queue.append(N)
14+
15+
dx = [-1, 1, 2]
16+
17+
while queue:
18+
loc = queue.popleft()
19+
20+
# ์ˆ˜๋นˆ์ด๊ฐ€ ๋™์ƒ์—๊ฒŒ ๋„์ฐฉํ–ˆ๋‹ค๋ฉด ์ข…๋ฃŒ
21+
if loc == K:
22+
print(visited[loc])
23+
break
24+
25+
# ์ˆ˜๋นˆ์˜ ์œ„์น˜์—์„œ 3๊ฐ€์ง€ ์ด๋™
26+
for i in range(3):
27+
if i == 2:
28+
new_loc = loc * dx[i]
29+
else:
30+
new_loc = loc + dx[i]
31+
32+
# ์ƒˆ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ์•ˆ์ด๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
33+
if 0 <= new_loc <= 100_000 and visited[new_loc] == -1:
34+
queue.append(new_loc)
35+
visited[new_loc] = visited[loc] + 1
36+
37+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
rating = ['A+', 'A0', 'B+', 'B0', 'C+', 'C0', 'D+', 'D0', 'F']
2+
grade = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0]
3+
4+
total = 0
5+
result = 0
6+
for _ in range(20):
7+
s, p, g = input().split()
8+
p = float(p)
9+
if g != 'P':
10+
total += p
11+
result += p * grade[rating.index(g)]
12+
13+
print('%.6f' % (result / total))

0 commit comments

Comments
ย (0)