Skip to content

Commit acdbc45

Browse files
committed
feat: 5월 4주차 과제 문제 풀이 업로드
1 parent 0a9fa53 commit acdbc45

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
BOJ #11559. Puyo Puyo (골드 4)
3+
https://www.acmicpc.net/problem/11559
4+
유형: BFS + Implementation
5+
"""
6+
7+
from collections import deque
8+
from sys import stdin
9+
input = stdin.readline
10+
11+
EMPTY = '.'
12+
dx = (1, -1, 0, 0)
13+
dy = (0, 0, 1, -1)
14+
15+
16+
def delete_block():
17+
for x, y in blocks:
18+
board[x][y] = EMPTY
19+
20+
21+
def update_board():
22+
for y in range(6):
23+
for t in range(10, -1, -1):
24+
for x in range(11, t, -1):
25+
if board[x][y] == EMPTY and board[t][y] != EMPTY:
26+
board[x][y], board[t][y] = board[t][y], EMPTY
27+
28+
29+
def is_in_area(x, y):
30+
return 0 <= x < 12 and 0 <= y < 6
31+
32+
33+
def bfs(x, y):
34+
queue = deque([(x, y)])
35+
visited[x][y] = True
36+
same_blocks = [(x, y)]
37+
while queue:
38+
x, y = queue.popleft()
39+
for d in range(4):
40+
nx = x + dx[d]
41+
ny = y + dy[d]
42+
if is_in_area(nx, ny) and not visited[nx][ny] and board[x][y] == board[nx][ny]:
43+
queue.append((nx, ny))
44+
visited[nx][ny] = True
45+
same_blocks.append((nx, ny))
46+
return same_blocks
47+
48+
49+
50+
board = [[*input().rstrip()] for _ in range(12)]
51+
flag = True
52+
res = 0
53+
while flag:
54+
flag = False
55+
visited = [[False] * 6 for _ in range(12)]
56+
for i in range(12):
57+
for j in range(6):
58+
if board[i][j] != EMPTY and not visited[i][j]:
59+
blocks = bfs(i, j)
60+
if len(blocks) >= 4:
61+
flag = True
62+
delete_block()
63+
if flag:
64+
update_board()
65+
res += 1
66+
print(res)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## 🚀5월 4주차 (5/26) 스터디 발제 주제: BFS + Implementation
2+
> 발제자: 조윤상 (@YoonYn9915)
3+
4+
> [!NOTE]
5+
> 주제: BFS + Implementation
6+
7+
### 🗂️ 스터디 자료
8+
- PDF: [바로가기](Study_BOJ_16509.pdf)
9+
10+
### 📖 문제
11+
- [백준 #16509. 장군](https://www.acmicpc.net/problem/16509): BFS + Implementation / 골드5
12+
- 정답 코드: [Study_BOJ_16509_장군.py](Study_BOJ_16509_장군.py)
13+
14+
### 💻 과제
15+
- [백준 #11559. Puyo Puyo](https://www.acmicpc.net/problem/11559): BFS + Implementation/ 골드4
16+
- 정답 코드: [Assignment_BOJ_11559_Puyo Puyo.py](Assignment_BOJ_11559_Puyo Puyo.py)
496 KB
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
BOJ #16509. 장군 (골드 5)
3+
https://www.acmicpc.net/problem/16509
4+
유형: BFS + Implementation
5+
"""
6+
7+
from collections import deque
8+
9+
# 입력 받기
10+
R1, C1 = map(int, input().split()) # 상의 시작 위치
11+
R2, C2 = map(int, input().split()) # 왕의 위치
12+
13+
# 방문 여부를 -1로 초기화 (10행 x 9열)
14+
visited = [[-1] * 9 for _ in range(10)]
15+
visited[R1][C1] = 0 # 시작 위치는 0으로 표시
16+
17+
# 상의 8가지 이동 방향 및 그에 따른 경유 좌표 정의
18+
# 각각: 경유1, 경유2, 최종 목적지 (총 3단계 이동)
19+
paths = [
20+
[(-1, 0), (-2, -1), (-3, -2)],
21+
[(-1, 0), (-2, 1), (-3, 2)],
22+
[(1, 0), (2, -1), (3, -2)],
23+
[(1, 0), (2, 1), (3, 2)],
24+
[(0, -1), (-1, -2), (-2, -3)],
25+
[(0, -1), (1, -2), (2, -3)],
26+
[(0, 1), (-1, 2), (-2, 3)],
27+
[(0, 1), (1, 2), (2, 3)],
28+
]
29+
30+
# 이동 경로에 왕이 있는지 확인
31+
def check(x, y, i):
32+
for dx, dy in paths[i][:2]: # 경유지 두 곳만 확인
33+
mx, my = x + dx, y + dy
34+
if mx == R2 and my == C2:
35+
return False
36+
return True
37+
38+
# BFS로 최소 이동 횟수 찾기
39+
def bfs():
40+
queue = deque([(R1, C1)])
41+
42+
while queue:
43+
x, y = queue.popleft()
44+
45+
# 왕의 위치에 도달한 경우
46+
if x == R2 and y == C2:
47+
print(visited[x][y])
48+
return
49+
50+
for i in range(8):
51+
nx = x + paths[i][2][0]
52+
ny = y + paths[i][2][1]
53+
54+
# 범위 안이고, 방문하지 않았으며, 경로에 왕이 없다면
55+
if 0 <= nx < 10 and 0 <= ny < 9 and visited[nx][ny] == -1 and check(x, y, i):
56+
visited[nx][ny] = visited[x][y] + 1
57+
queue.append((nx, ny))
58+
59+
# 도달할 수 없는 경우
60+
print(-1)
61+
62+
bfs()

0 commit comments

Comments
 (0)