250x250
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
- 파티션 크기 조정
- 볼륨 만들기
- BFS
- 주사위굴리기2
- 중복조합
- 알고리즘개념
- 백준15652
- D드라이브생성
- 알고리즘
- 에라토스테네스의채
- 백준13458
- 완탐
- 코테
- 전화번호속의암호
- 삼성역테
- 재귀함수
- 23288
- N과M
- 완전탐색
- 자바
- Bfs와DFS
- java
- 자바 코테
- 중복순열
- 백준2251
- 정보처리기사
- 코테준비
- 백준
- 순열
- 정올 1620
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 10026번 적록색약 본문
728x90
* 출처
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
문제 풀이
dfs로 풀면 된다.
public class Main {
static int N;
static char[][] matrix;
static boolean[][] visited;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(in.readLine());
matrix = new char[N][N];
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
String s = in.readLine();
for (int j = 0; j < N; j++) {
matrix[i][j] = s.charAt(j);
}
}
int total = 0;
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
if(!visited[i][j]) {
dfs(i,j);
total++;
}
}
}
System.out.print(total+ " ");
total = 0;
visited = new boolean[N][N];
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
if(matrix[i][j] == 'G') matrix[i][j]= 'R';
}
}
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
if(!visited[i][j]) {
dfs(i,j);
total++;
}
}
}
System.out.println(total);
}
static int dx[] = { -1, 1, 0, 0 };
static int dy[] = { 0, 0, -1, 1 };
static void dfs(int x, int y) {
visited[x][y] = true;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {
if (matrix[x][y] == matrix[nx][ny]) {
dfs(nx, ny);
}
}
}
}
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 1149번 RGB 거리 (0) | 2021.03.06 |
---|---|
[문제] 백준 1260번 BFS와 DFS (0) | 2021.03.04 |
[문제] 백준 2491번 수열 (0) | 2021.02.18 |
[문제] 백준 15686번 치킨 배달 (0) | 2021.02.17 |
[문제] 백준 11279번 최대 힙, 1927번 최소 힙 (0) | 2021.02.16 |
Comments