Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
BOJ #11559. Puyo Puyo (골드 4)
https://www.acmicpc.net/problem/11559
유형: BFS + Implementation
"""

from collections import deque
from sys import stdin
input = stdin.readline

EMPTY = '.'
dx = (1, -1, 0, 0)
dy = (0, 0, 1, -1)


def delete_block():
for x, y in blocks:
board[x][y] = EMPTY


def update_board():
for y in range(6):
for t in range(10, -1, -1):
for x in range(11, t, -1):
if board[x][y] == EMPTY and board[t][y] != EMPTY:
board[x][y], board[t][y] = board[t][y], EMPTY


def is_in_area(x, y):
return 0 <= x < 12 and 0 <= y < 6


def bfs(x, y):
queue = deque([(x, y)])
visited[x][y] = True
same_blocks = [(x, y)]
while queue:
x, y = queue.popleft()
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if is_in_area(nx, ny) and not visited[nx][ny] and board[x][y] == board[nx][ny]:
queue.append((nx, ny))
visited[nx][ny] = True
same_blocks.append((nx, ny))
return same_blocks



board = [[*input().rstrip()] for _ in range(12)]
flag = True
res = 0
while flag:
flag = False
visited = [[False] * 6 for _ in range(12)]
for i in range(12):
for j in range(6):
if board[i][j] != EMPTY and not visited[i][j]:
blocks = bfs(i, j)
if len(blocks) >= 4:
flag = True
delete_block()
if flag:
update_board()
res += 1
print(res)
16 changes: 16 additions & 0 deletions _WeeklyChallenges/W24-[BFS+Implementation]/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 🚀5월 4주차 (5/26) 스터디 발제 주제: BFS + Implementation
> 발제자: 조윤상 (@YoonYn9915)

> [!NOTE]
> 주제: BFS + Implementation

### 🗂️ 스터디 자료
- PDF: [바로가기](Study_BOJ_16509.pdf)

### 📖 문제
- [백준 #16509. 장군](https://www.acmicpc.net/problem/16509): BFS + Implementation / 골드5
- 정답 코드: [Study_BOJ_16509_장군.py](Study_BOJ_16509_장군.py)

### 💻 과제
- [백준 #11559. Puyo Puyo](https://www.acmicpc.net/problem/11559): BFS + Implementation/ 골드4
- 정답 코드: [Assignment_BOJ_11559_Puyo Puyo.py](Assignment_BOJ_11559_Puyo Puyo.py)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
BOJ #16509. 장군 (골드 5)
https://www.acmicpc.net/problem/16509
유형: BFS + Implementation
"""

from collections import deque

# 입력 받기
R1, C1 = map(int, input().split()) # 상의 시작 위치
R2, C2 = map(int, input().split()) # 왕의 위치

# 방문 여부를 -1로 초기화 (10행 x 9열)
visited = [[-1] * 9 for _ in range(10)]
visited[R1][C1] = 0 # 시작 위치는 0으로 표시

# 상의 8가지 이동 방향 및 그에 따른 경유 좌표 정의
# 각각: 경유1, 경유2, 최종 목적지 (총 3단계 이동)
paths = [
[(-1, 0), (-2, -1), (-3, -2)],
[(-1, 0), (-2, 1), (-3, 2)],
[(1, 0), (2, -1), (3, -2)],
[(1, 0), (2, 1), (3, 2)],
[(0, -1), (-1, -2), (-2, -3)],
[(0, -1), (1, -2), (2, -3)],
[(0, 1), (-1, 2), (-2, 3)],
[(0, 1), (1, 2), (2, 3)],
]

# 이동 경로에 왕이 있는지 확인
def check(x, y, i):
for dx, dy in paths[i][:2]: # 경유지 두 곳만 확인
mx, my = x + dx, y + dy
if mx == R2 and my == C2:
return False
return True

# BFS로 최소 이동 횟수 찾기
def bfs():
queue = deque([(R1, C1)])

while queue:
x, y = queue.popleft()

# 왕의 위치에 도달한 경우
if x == R2 and y == C2:
print(visited[x][y])
return

for i in range(8):
nx = x + paths[i][2][0]
ny = y + paths[i][2][1]

# 범위 안이고, 방문하지 않았으며, 경로에 왕이 없다면
if 0 <= nx < 10 and 0 <= ny < 9 and visited[nx][ny] == -1 and check(x, y, i):
visited[nx][ny] = visited[x][y] + 1
queue.append((nx, ny))

# 도달할 수 없는 경우
print(-1)

bfs()