Skip to content

Commit 5e66806

Browse files
authored
Merge pull request #217 from AlgorithmStudy-Allumbus/YoonYn9915
feat: 5์›” 4์ฃผ์ฐจ ๊ณผ์ œ ๋ฌธ์ œ ํ’€์ด ์—…๋กœ๋“œ
2 parents cbb3f67 + acdbc45 commit 5e66806

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)
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)