Skip to content

Commit cada402

Browse files
authored
Merge pull request #36 from YoonYn9915/main
YoonYn9915 / 6월 5주차 / 2문제
2 parents c4e5313 + 8fbc3c3 commit cada402

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from collections import deque
2+
3+
# 2차원 배열을 이용한 bfs 구현
4+
def bfs(x, y):
5+
6+
# 2차원 배열에서 현재 노드의 상하좌우를 검사하기 위한 dx와 dy
7+
dx = [0, -1, 0, 1]
8+
dy = [-1, 0, 1, 0]
9+
10+
# queue를 이용한 bfs 그래프 탐색
11+
queue.append((x, y))
12+
visited[x][y] = 1
13+
14+
15+
while queue:
16+
(x, y) = queue.popleft()
17+
18+
# queue에서 pop한 현재 노드를 기준으로 상하좌우 탐색
19+
for k in range(4):
20+
row = x + dx[k]
21+
col = y + dy[k]
22+
23+
# 탐색 조건
24+
# 1. 현재 노드가 배열의 인덱스 범위 안인지 0 <= x < n and 0 <= y < n
25+
# 2. 같은 색인지
26+
# 3. 방문하지 않았는지
27+
if 0 <= row < n and 0 <= col < n:
28+
if arr[x][y] == arr[row][col] and visited[row][col] == 0:
29+
queue.append((row, col))
30+
visited[row][col] = 1
31+
32+
33+
# 초기값 세팅
34+
n = int(input())
35+
visited = [[0] * n for _ in range(n)]
36+
arr = [list(input()) for _ in range(n)]
37+
queue = deque()
38+
39+
# 적록색약이 아닌 경우의 답
40+
answerForNormal = 0
41+
42+
# 적록색약인 경우의 답
43+
answerForColorBlindness = 0
44+
45+
46+
# 적록색약이 아닌 경우
47+
for i in range(n):
48+
for j in range(n):
49+
if visited[i][j] == 0:
50+
bfs(i, j)
51+
answerForNormal += 1
52+
53+
54+
# 적록색약인 경우 R과 G는 같으므로
55+
56+
for i in range(n):
57+
for j in range(n):
58+
if arr[i][j] == 'G':
59+
arr[i][j] = 'R'
60+
61+
visited = [[0] * n for _ in range(n)]
62+
for i in range(n):
63+
for j in range(n):
64+
if not visited[i][j]:
65+
bfs(i,j)
66+
answerForColorBlindness += 1
67+
68+
print(answerForNormal, answerForColorBlindness)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from collections import deque
2+
3+
# 지도(2차원 배열)에서 섬을 탐색하기 위한 bfs
4+
def bfs(x, y):
5+
6+
# 탐색범위가 현재 노드 기준 가로, 세로, 대각선이므로 총 8가지
7+
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
8+
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
9+
10+
queue.append((x, y))
11+
visited[x][y] = 1
12+
13+
# queue가 존재하면
14+
while queue:
15+
16+
# queue에서 pop해서
17+
x, y = queue.popleft()
18+
19+
#가로, 세로 대각선에 있는 노드가 다음 세가지 조건을 만족하는지 검사
20+
# 탐색 조건
21+
# 1. 현재 노드가 배열의 인덱스 범위 안인지 0 <= row < h and 0 <= col < w
22+
# 2. 땅인지 (arr값이 1인지)
23+
# 3. 방문하지 않았는지
24+
for k in range(8):
25+
row = x + dx[k]
26+
col = y + dy[k]
27+
28+
if 0 <= row < h and 0 <= col < w and arr[row][col] == 1 and visited[row][col] == 0:
29+
queue.append((row, col))
30+
visited[row][col] = 1
31+
32+
33+
answer = deque()
34+
queue = deque()
35+
36+
while True:
37+
w, h = map(int, input().split())
38+
if h == 0 and w == 0:
39+
break
40+
41+
visited = [[0] * w for _ in range(h)]
42+
arr = [list(map(int, input().split())) for _ in range(h)]
43+
ans = 0
44+
45+
for i in range(h):
46+
for j in range(w):
47+
if arr[i][j] == 1 and visited[i][j] == 0:
48+
bfs(i, j)
49+
ans += 1
50+
51+
answer.append(ans)
52+
53+
for ans in answer:
54+
print(ans)

0 commit comments

Comments
 (0)