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 | 29 | 30 | 31 |
Tags
- 운영체제
- 그래프
- 분할정복
- 문자열
- 배열
- 안드로이드
- activity
- 자바
- 코틀린
- dfs
- 알고리즘
- 동적계획법
- 이분탐색
- 코딩테스트
- 스택
- 백준
- 다이나믹프로그래밍
- 생명주기
- 프로그래머스
- BFS
- 트리
- 완전탐색
- GIT
- 문자열다루기
- 세그먼트트리
- Android
- 카카오블라인드
- BOJ
- component
- 코딩
Archives
- Today
- Total
HS_development_log
백준 10026번 - 적록색약 / Java 본문
반응형
1. 문제
2. 접근
BFS문제다. 근데 구해야 하는 경우의 수가 2가지다. 적록색약일 때는 색구분을 2개로 하고, 적록색약이 아닐 때는 색구분을 3개로 해주면 된다. 적록색약 배열, 적록색약 아닐 때 배열 2가지를 만들어서 각각 처리해주면 된다.
3. 코드
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.nextLine();
String[] input = new String[n];
for(int i=0;i<n;i++){
input[i] = scan.nextLine();
}
char[][] map1 = new char[n][n];
char[][] map2 = new char[n][n];
boolean[][] check1 = new boolean[n][n];
boolean[][] check2 = new boolean[n][n];
int answer1 = 0;
int answer2 = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
char ch = input[i].charAt(j);
if(ch == 'R' || ch == 'G'){
map1[i][j] = 'R'; // 적록색약은 빨강으로 통합
map2[i][j] = ch;
} else {
map1[i][j] = 'B';
map2[i][j] = 'B';
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(!check1[i][j]){
check1[i][j] = true;
bfs(check1, map1, new Node(i,j), map1[i][j]);
answer1++;
}
if(!check2[i][j]){
check2[i][j] = true;
bfs(check2, map2, new Node(i,j), map2[i][j]);
answer2++;
}
}
}
System.out.print(answer2+" "+answer1);
}
public static void bfs(boolean check[][], char map[][], Node node, char color){
int[] di = {-1,0,1,0};
int[] dj = {0,1,0,-1};
Queue<Node> q = new LinkedList<>();
q.offer(node);
while(!q.isEmpty()){
Node n = q.poll();
int x = n.x;
int y = n.y;
for(int i=0;i<4;i++){
int dx = x+di[i];
int dy = y+dj[i];
if(dx>=0 && dy>=0 && dx<map.length && dy<map.length){
if(!check[dx][dy] && map[dx][dy] == color){
q.offer(new Node(dx,dy));
check[dx][dy] = true;
}
}
}
}
}
}
class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
4. 결과
끝.
반응형
'Algorithm-백준 > BFS,DFS' 카테고리의 다른 글
백준 4963번 - 섬의 개수 / Kotlin (0) | 2020.08.04 |
---|---|
백준 6603 - 로또 / Java (0) | 2020.07.28 |
백준 7562번 - 나이트의 이동 / Java (0) | 2020.02.05 |
백준 7576번 - 토마토 / Java (0) | 2020.02.05 |
백준 13023번 - ABCDE / Java (0) | 2020.02.05 |