Skip to content

Commit 264472b

Browse files
committed
feat: 3월 4주차 발제 자료 업로드
1 parent 5adb823 commit 264472b

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
BOJ #4963. 섬의 개수 (실버2)
3+
https://www.acmicpc.net/problem/4963
4+
유형: BFS/DFS
5+
'''
6+
7+
import sys
8+
input = sys.stdin.readline
9+
from collections import deque
10+
11+
# 8방향 (상, 하, 좌, 우, 대각선 4개)
12+
dx = [-1, 1, 0, 0, -1, -1, 1, 1]
13+
dy = [0, 0, -1, 1, -1, 1, -1, 1]
14+
15+
def bfs(x, y, graph, w, h):
16+
queue = deque([(x, y)])
17+
graph[y][x] = 0 # 방문 처리 (1 → 0 변경)
18+
19+
while queue: # 큐가 빌 때까지 반복
20+
cx, cy = queue.popleft() # 현재 좌표 꺼내기
21+
for i in range(8): # 8방향 탐색
22+
nx, ny = cx + dx[i], cy + dy[i] # 새로운 좌표 계산
23+
if 0 <= nx < w and 0 <= ny < h and graph[ny][nx] == 1:
24+
graph[ny][nx] = 0 # 방문 처리
25+
queue.append((nx, ny)) # 큐에 추가 (다음에 탐색할 곳)
26+
27+
while True:
28+
w, h = map(int, input().split())
29+
if w == 0 and h == 0:
30+
break # 종료 조건
31+
island = [list(map(int, input().split())) for _ in range(h)]
32+
33+
count = 0 # 섬의 개수
34+
for y in range(h):
35+
for x in range(w):
36+
if island[y][x] == 1: # 새로운 섬 발견
37+
count += 1
38+
bfs(x, y, island, w, h) # BFS 실행
39+
40+
print(count)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 🚀 1월 4주차 (03/24) 스터디 발제 주제: DFS/BFS
2+
> 발제자: 정수미
3+
4+
### 🗂️ 스터디 자료
5+
- PDF: [바로가기](./Study_BOJ_10026.pdf)
6+
![image](https://github.com/user-attachments/assets/41909fa7-14ed-4678-9725-9ab0721b31ff)
7+
8+
9+
### 📖 문제
10+
- [백준 #10026. 적록색약](https://www.acmicpc.net/problem/10026): DFS/BFS / 골드5
11+
- 정답 코드: [Study_BOJ_10026_적록색약.py](./Study_BOJ_10026_적록색약.py)
12+
13+
### 💻 과제
14+
- [백준 #4963. 섬의 개수](https://www.acmicpc.net/problem/4963): DFS/BFS, 실버2
15+
- 정답 코드: [Assignment_BOJ_4963_섬의개수.py](./Assignment_BOJ_4963_섬의개수.py)
607 KB
Binary file not shown.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
BOJ #10026. 적록색약 (골드5)
3+
https://www.acmicpc.net/problem/10026
4+
유형: BFS/DFS
5+
'''
6+
7+
import sys
8+
input = sys.stdin.readline
9+
from collections import deque
10+
11+
N = int(input().strip())
12+
grid = [list(input().strip()) for _ in range(N)]
13+
14+
# 방향 벡터
15+
dx = [-1, 1, 0, 0]
16+
dy = [0, 0, -1, 1]
17+
18+
def bfs(x, y, color, visited, grid):
19+
queue = deque([(x, y)])
20+
visited[x][y] = True
21+
22+
while queue:
23+
x, y = queue.popleft()
24+
for i in range(4):
25+
nx, ny = x + dx[i], y + dy[i]
26+
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
27+
if grid[nx][ny] == color:
28+
visited[nx][ny] = True
29+
queue.append((nx, ny))
30+
31+
def count_regions(grid, is_color_blind):
32+
visited = [[False] * N for _ in range(N)]
33+
count = 0
34+
35+
for i in range(N):
36+
for j in range(N):
37+
if not visited[i][j]: # 방문하지 않은 경우 BFS 탐색 시작
38+
count += 1
39+
color = grid[i][j]
40+
41+
# 적록색약 모드라면 R과 G를 동일하게 처리
42+
if is_color_blind and color in "RG":
43+
bfs(i, j, "R", visited, grid)
44+
else:
45+
bfs(i, j, color, visited, grid)
46+
47+
return count
48+
49+
# 적록색약이 아닌 경우
50+
normal_count = count_regions(grid, is_color_blind=False)
51+
52+
# 적록색약인 경우 (R과 G를 동일하게 처리한 grid 생성)
53+
for i in range(N):
54+
for j in range(N):
55+
if grid[i][j] == 'G':
56+
grid[i][j] = 'R'
57+
58+
color_blind_count = count_regions(grid, is_color_blind=True)
59+
60+
print(normal_count, color_blind_count)

0 commit comments

Comments
 (0)