Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 재귀
- 큐
- 우선순위 큐
- 세그먼트 트리
- Knapsack
- 메모리풀
- 크루스칼
- 순열
- 피보나치 수
- SSAFY
- 중복 순열
- BeautifulSoup
- 문자열
- 링크드리스트
- 백트래킹
- 스택
- DP
- 이분 탐색
- 분할 정복
- 빠른 입출력
- 시뮬레이션
- dfs
- 조합
- BFS
- 클래스
- MST
- 비트마스크
- lis
- 완전 탐색
- 그리디
Archives
- Today
- Total
작심 24/7
[백준] 10026번 적록색약 (C++, JAVA) 본문
10026번: 적록색약
문제 적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(
www.acmicpc.net
처음엔 두 가지의 버전의 BFS를 만들었다가
애초에 입력받을 때 R과 G를 통일을 해주면 BFS함수 하나만으로도 풀 수 있다는 걸 깨달았다.
이차원 배열 두 가지를 만들어 하나는 정상적으로, 하나는 R과 G를 R로 통일해준다.
BFS함수는 늘 짜던 그 방식으로 해주면 된다.
< C++ 코드 >
#include <iostream>
#include <queue>
using namespace std;
int daltonism = 0, non_daltonism = 0, x, y;
void Bfs(int i, int j, int N, char arr[][101]) {
queue <pair<int, int>> q;
q.push(pair<int, int>(i, j));
char section = arr[i][j];
arr[i][j] = '0';
while (!q.empty()) {
x = q.front().first, y = q.front().second;
if (x - 1 >= 0 && arr[x - 1][y] == section) { //상
q.push(pair<int, int>(x - 1, y));
arr[x - 1][y] = '0';
}
if (x + 1 < N && arr[x + 1][y] == section) { //하
q.push(pair<int, int>(x + 1, y));
arr[x + 1][y] = '0';
}
if (y - 1 >= 0 && arr[x][y - 1] == section) { //좌
q.push(pair<int, int>(x, y - 1));
arr[x][y - 1] = '0';
}
if (y + 1 < N && arr[x][y + 1] == section) { //우
q.push(pair<int, int>(x, y + 1));
arr[x][y + 1] = '0';
}
q.pop();
}
}
int main() {
int N;
cin >> N;
char arr[101][101] = { 0 }, arr2[101][101] = { 0 };
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
cin >> arr[i][j];
if (arr[i][j] == 'G') arr2[i][j] = 'R';
else arr2[i][j] = arr[i][j];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (arr[i][j] != '0') {
Bfs(i, j, N, arr);
non_daltonism++;
}
if (arr2[i][j] != '0') {
Bfs(i, j, N, arr2);
daltonism++;
}
}
}
cout << non_daltonism << " " << daltonism;
return 0;
}
< JAVA 코드 >
import java.util.Scanner;
public class BOJ_10026_적록색약 {
private static int N;
private static int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
public static void dfs(int X, int Y, char ch, char arr[][], boolean visited[][]) {
visited[X][Y] = true;
for(int i = 0; i < 4; i++) {
int x = X + dx[i], y = Y + dy[i];
if(x < 0 || x >= N || y < 0 || y >= N || arr[x][y] != ch || visited[x][y]) continue;
visited[x][y] = true;
dfs(x, y, ch, arr, visited);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
int res = 0, res2 = 0;
char arr[][] = new char[N][N], arr2[][] = new char[N][N];
boolean visited[][] = new boolean[N][N], visited2[][] = new boolean[N][N];
for(int i = 0; i < N; i++) {
String st = sc.next();
for(int j = 0; j < N; j++) {
arr[i][j] = st.charAt(j);
if(arr[i][j] =='G') arr2[i][j] = 'R';
else arr2[i][j] = arr[i][j]; // arr2는 R과 B만으로 이루어져 있다
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(!visited[i][j]) {
dfs(i, j, arr[i][j], arr, visited);
res++;
}
if(!visited2[i][j]) {
dfs(i, j, arr2[i][j], arr2, visited2);
res2++;
}
}
}
System.out.println(res + " " + res2);
}
}
'백준' 카테고리의 다른 글
[백준] 9029번 정육면체 (0) | 2020.06.13 |
---|---|
[백준] 2468번 안전 영역 (C++, JAVA) (0) | 2020.06.10 |
[백준] 11403번 경로 찾기 (C++, JAVA) (0) | 2020.06.10 |
[백준] 2667번 단지 번호 붙이기 (C++, JAVA) (0) | 2020.06.10 |
[백준] 1743번 음식물 피하기 (0) | 2020.06.09 |
Comments