Skip to content

Commit b0c2a3b

Browse files
authored
Merge pull request #111 from learntosurf/main
12์›” 4์ฃผ์ฐจ ์Šคํ„ฐ๋”” ์ž๋ฃŒ ๊ณต์œ 
2 parents d974637 + b1c1de6 commit b0c2a3b

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

โ€Ž_WeeklyChallenges/W05-[Greedy]/Assignment_BOJ_1931_ํšŒ์˜์‹ค๋ฐฐ์ •.pyโ€Ž

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## ๐Ÿš€ 12์›” 4์ฃผ์ฐจ (12/23) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: Greedy Algorithm
2+
> ๋ฐœ์ œ์ž: ์ •์ˆ˜๋ฏธ
3+
4+
### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
5+
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ]()
6+
![image](https://github.com/user-attachments/assets/eda88039-4fa3-4c7e-8f0e-b610faa8081a)
7+
8+
9+
### ๐Ÿ“– ๋ฌธ์ œ
10+
- [๋ฐฑ์ค€ #1700. ๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง](https://www.acmicpc.net/problem/1700): ๊ทธ๋ฆฌ๋”” / ๊ณจ๋“œ1
11+
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_3190_๋ฑ€.py]()
12+
13+
### ๐Ÿ’ป ๊ณผ์ œ
14+
- [๋ฐฑ์ค€ #1931. ํŠธ๋Ÿญ](https://www.acmicpc.net/problem/1931): ๊ทธ๋ฆฌ๋””, ์ •๋ ฌ / ์‹ค๋ฒ„1
15+
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_1931_ํšŒ์˜์‹ค๋ฐฐ์ •.py]()
422 KB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
BOJ #1700. ๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง (๊ณจ๋“œ1)
3+
https://www.acmicpc.net/problem/1700
4+
์œ ํ˜•: Greedy
5+
'''
6+
7+
import sys
8+
input = sys.stdin.read
9+
10+
data = input().split()
11+
N, K = int(data[0]), int(data[1]) # ๋ฉ€ํ‹ฐํƒญ ๊ตฌ๋ฉ ์ˆ˜์™€ ์ „๊ธฐ ์šฉํ’ˆ ์‚ฌ์šฉ ํšŸ์ˆ˜
12+
sequence = list(map(int, data[2:])) # ์ „๊ธฐ ์šฉํ’ˆ ์‚ฌ์šฉ ์ˆœ์„œ
13+
14+
multitap = [] # ํ˜„์žฌ ๋ฉ€ํ‹ฐํƒญ ์ƒํƒœ
15+
unplug_count = 0 # ํ”Œ๋Ÿฌ๊ทธ๋ฅผ ๋ฝ‘๋Š” ํšŸ์ˆ˜
16+
17+
# ์ˆœ์ฐจ์ ์œผ๋กœ ์ „๊ธฐ์šฉํ’ˆ ์‚ฌ์šฉ
18+
for i in range(K):
19+
current = sequence[i]
20+
21+
# 1. ์ด๋ฏธ ๋ฉ€ํ‹ฐํƒญ์— ๊ฝ‚ํ˜€ ์žˆ๋Š” ๊ฒฝ์šฐ
22+
if current in multitap:
23+
continue # ์•„๋ฌด ์ž‘์—…๋„ ํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ
24+
25+
# 2. ๋ฉ€ํ‹ฐํƒญ์— ๋นˆ ์ž๋ฆฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
26+
if len(multitap) < N:
27+
multitap.append(current) # ์ƒˆ๋กœ์šด ์ „๊ธฐ์šฉํ’ˆ์„ ์ถ”๊ฐ€ํ•จ
28+
continue
29+
30+
# 3. ๋ฉ€ํ‹ฐํƒญ์ด ๊ฐ€๋“ ์ฐจ ์žˆ๋Š” ๊ฒฝ์šฐ: ๋ฝ‘์„ ์ „๊ธฐ ์šฉํ’ˆ ๊ฒฐ์ •
31+
farthest_idx = -1
32+
to_unplug = -1
33+
for plug in multitap:
34+
if plug not in sequence[i:]: # ์•ž์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ํ”Œ๋Ÿฌ๊ทธ
35+
to_unplug = plug
36+
break
37+
else:
38+
# ๊ฐ€์žฅ ๋‚˜์ค‘์— ์‚ฌ์šฉ๋˜๋Š” ํ”Œ๋Ÿฌ๊ทธ
39+
idx = sequence[i:].index(plug)
40+
if idx > farthest_idx:
41+
farthest_idx = idx
42+
to_unplug = plug
43+
44+
# ํ”Œ๋Ÿฌ๊ทธ ๊ต์ฒด
45+
multitap.remove(to_unplug)
46+
multitap.append(current)
47+
unplug_count += 1
48+
49+
print(unplug_count)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from collections import deque
2+
3+
N, M = map(int, input().split())
4+
graph = [list(map(int, input())) for _ in range(N)]
5+
6+
# ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ ์ •์˜
7+
directions = [(-1,0), (0,1), (1,0), (0,-1)]
8+
9+
# BFS
10+
def bfs():
11+
# 3์ฐจ์› ๋ฐฉ๋ฌธ ๋ฐฐ์—ด: visited[x][y][๋ฒฝ ๋ถ€์ˆจ ์—ฌ๋ถ€]
12+
# wall=0: ๋ฒฝ์„ ๋ถ€์ˆ˜์ง€ ์•Š๊ณ  ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ
13+
# wall=1: ๋ฒฝ์„ ํ•œ๋ฒˆ ๋ถ€์ˆ˜๊ณ  ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ
14+
visited = [[[0] * 2 for _ in range(M)] for _ in range(N)]
15+
queue = deque([(0,0,0)]) # (x, y, ๋ฒฝ ๋ถ€์ˆœ ์—ฌ๋ถ€)
16+
visited[0][0][0] = 1 # ์‹œ์ž‘์  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ (๋ฒฝ์„ ๋ถ€์ˆ˜์ง€ ์•Š๊ณ )
17+
18+
while queue:
19+
x, y, wall = queue.popleft()
20+
21+
# ๋„์ฐฉ์ง€์— ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ ๊ฑฐ๋ฆฌ ๋ฐ˜ํ™˜
22+
if x == N-1 and y == M-1:
23+
return visited[x][y][wall]
24+
25+
# 4๊ฐ€์ง€ ๋ฐฉํ–ฅ ํƒ์ƒ‰
26+
for dx, dy in directions:
27+
nx, ny = x + dx, y + dy
28+
29+
# ๋งต ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ฒ˜๋ฆฌ
30+
if 0 <= nx < N and 0 <= ny < M:
31+
# ์ด๋™ํ•˜๋ ค๋Š” ์นธ์ด ๋นˆ์นธ(0)์ด๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
32+
if graph[nx][ny] == 0 and visited[nx][ny][wall] == 0:
33+
visited[nx][ny][wall] = visited[x][y][wall] + 1
34+
queue.append((nx, ny, wall))
35+
36+
# ์ด๋™ํ•˜๋ ค๋Š” ์นธ์ด ๋ฒฝ(1)์ด๊ณ , ์•„์ง ๋ฒฝ์„ ๋ถ€์ˆœ ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ
37+
if graph[nx][ny] == 1 and wall == 0 and visited[nx][ny][1] == 0:
38+
visited[nx][ny][1] = visited[x][y][wall] + 1
39+
queue.append((nx, ny, 1))
40+
41+
# BFS ์ข…๋ฃŒ ํ›„์—๋„ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
42+
return -1
43+
44+
print(bfs())

0 commit comments

Comments
ย (0)