Skip to content

Commit 893e9ee

Browse files
committed
[Gold V] Title: 적록색약, Time: 148 ms, Memory: 115152 KB -BaekjoonHub
1 parent 6b769a6 commit 893e9ee

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

백준/Gold/10026. 적록색약/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 115528 KB, 시간: 156 ms
7+
메모리: 115152 KB, 시간: 148 ms
88

99
### 분류
1010

1111
너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
1212

1313
### 제출 일자
1414

15-
2025년 3월 20일 12:05:07
15+
2025년 3월 29일 12:08:57
1616

1717
### 문제 설명
1818

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
11
from collections import deque
22

3-
N = int(input())
4-
RGB = [list(map(str, input())) for _ in range(N)]
5-
6-
dx = [-1, 1, 0, 0]
7-
dy = [0, 0, -1, 1]
8-
9-
visited = [[False] * N for _ in range(N)]
103

11-
def bfs1(i, j):
4+
def bfs(i, j, A):
125
q = deque([(i, j)])
136

147
while q:
158
x, y = q.popleft()
16-
for d in range(4):
17-
nx = x + dx[d]
18-
ny = y + dy[d]
19-
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny] and RGB[nx][ny] == RGB[x][y]:
9+
for i in range(4):
10+
nx, ny = x + dx[i], y + dy[i]
11+
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny] and A[nx][ny] == A[x][y]:
2012
visited[nx][ny] = True
21-
q.append((nx, ny))
13+
q.append((nx, ny))
2214

23-
def bfs2(i, j):
24-
q = deque([(i, j)])
15+
N = int(input())
2516

26-
while q:
27-
x, y = q.popleft()
28-
visited[x][y] = True
29-
for d in range(4):
30-
nx = x + dx[d]
31-
ny = y + dy[d]
32-
if 0 <= nx < N and 0 <= ny < N and not visited[nx][ny]:
33-
if RGB[x][y] != 'B' and RGB[nx][ny] != 'B':
34-
visited[nx][ny] = True
35-
q.append((nx, ny))
36-
elif RGB[x][y] == 'B' and RGB[nx][ny] == 'B':
37-
visited[nx][ny] = True
38-
q.append((nx, ny))
39-
40-
ans1 = 0
41-
ans2 = 0
17+
change_normal = {'R': 1, 'G': 2, 'B': 3}
18+
change_blind = {'R': 1, 'G': 1, 'B': 2}
4219

20+
first = [list(map(str, input())) for _ in range(N)]
21+
22+
normal = [[0] * N for _ in range(N)]
23+
blind = [[0] * N for _ in range(N)]
4324
for i in range(N):
4425
for j in range(N):
45-
if not visited[i][j]:
46-
bfs1(i, j)
47-
ans1 += 1
26+
normal[i][j] = change_normal[first[i][j]]
27+
blind[i][j] = change_blind[first[i][j]]
4828

4929
visited = [[False] * N for _ in range(N)]
30+
dx = [-1, 1, 0, 0]
31+
dy = [0, 0, -1, 1]
32+
33+
ans = [0, 0]
5034

5135
for i in range(N):
5236
for j in range(N):
5337
if not visited[i][j]:
54-
bfs2(i, j)
55-
ans2 += 1
38+
bfs(i, j, normal)
39+
ans[0] += 1
40+
41+
visited = [[False] * N for _ in range(N)]
42+
for i in range(N):
43+
for j in range(N):
44+
if not visited[i][j]:
45+
bfs(i, j, blind)
46+
ans[1] += 1
5647

57-
print(ans1, ans2)
48+
print(*ans)

0 commit comments

Comments
 (0)