728x90
백준 10026번 적록색약 문제풀이입니다. 문제 해설은 링크를 확인하면 된다. 문제 해설
해당 문제는 그래프 탐색 유형의 문제로 'G'를 'R'로 보는 적록색약인 사람이 보는 구역의 수와 적록색약이 아닌 사람이 보는 구역의 수를 구해야 한다. 해당 문제를 해결하기 위해서는 다음의 과정을 수행해야 한다. colorData는 입력받은 색 정보이다.
- colorData에서 BFS를 수행한다.
- 수행하면서 연산의 끝에 'G'를 'R'로 바꿔준다.
- 모든 'G'가 'R'로 변했다면 다시 BFS를 수행한다.
- 과정 1, 과정 3에서 수행한 결과를 출력해준다.
위의 과정을 코드로 작성하면 다음과 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
from collections import deque
def bfs(x, y, value):
queue = deque()
queue.append((x,y))
visited[x][y] = True
dx = [0, -1, 0, 1]
dy = [-1, 0, 1, 0]
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx >= 0 and nx < n and ny >= 0 and ny < n:
if not visited[nx][ny] and colorData[nx][ny] == value:
visited[nx][ny] = True
queue.append((nx,ny))
if colorData[x][y] == "G":
colorData[x][y] = "R"
n = int(input())
count = 0
visited = [[False] * n for _ in range(n)]
colorData = []
for i in range(n):
colorData.append(list(str(input())))
for i in range(n):
for j in range(n):
if not visited[i][j]:
count += 1
bfs(i, j, colorData[i][j])
print(count, end=' ')
count = 0
for i in range(n):
for j in range(n):
visited[i][j] = False
for i in range(n):
for j in range(n):
if not visited[i][j]:
count += 1
bfs(i, j, colorData[i][j])
print(count)
|
이번 문제는 BFS 탐색 중에 데이터의 값을 바꿔준다는 것이 다른 BFS 문제와 차별점이 있었던 것 같다.
728x90
'취업을 준비하며 정리하는 컴퓨터 지식 > Problem Solving' 카테고리의 다른 글
[Problem Solving] 백준 2212번 센서 문제풀이 (0) | 2021.02.15 |
---|---|
[ProblemSolving] 백준 1202 보석 도둑 문제풀이 (0) | 2021.02.09 |
[ProblemSolving] 백준 1744번 수 묶기 문제풀이 (0) | 2021.02.05 |
[ProblemSolving] 백준 7576번 토마토 문제풀이 (0) | 2021.02.04 |
[Problem Solving] 백준 1916번 최소비용 구하기 문제풀이 (0) | 2021.02.02 |