Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
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
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 10026. 적록색약 본문

acmicpc

[BAEKJOON] 10026. 적록색약

코드와이 2021. 5. 10. 20:08

 

문제링크

www.acmicpc.net/problem/10026

 

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net

 

package acmicpc.Gold5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class 적록색약 {

	static int n, ans, ans2;
	static char map[][], map2[][];
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		n = Integer.parseInt(br.readLine());
		
		map = new char[n][n];
		map2 = new char[n][n];
		char[] tmp = new char[n];
		for(int i = 0 ; i < n ; i++) {
			tmp = br.readLine().toCharArray();
			for(int j = 0 ; j < n ; j++) {
				map[i][j] = tmp[j];
				map2[i][j] = tmp[j];
				if(map2[i][j] == 'G')map2[i][j] = 'R';
			}
		}
		
		ans = 0;
		ans2 = 0;
		for(int i = 0 ; i < n ; i++) {
			for(int j = 0 ; j < n ; j++) {
				if(map[i][j] != 'a') {
					ans++;
					dfs(i,j,map[i][j], map);
				}
				if(map2[i][j] != 'a') {
					ans2++;
					dfs(i,j,map2[i][j], map2);
				}
			}
		}
		System.out.println(ans + " " + ans2);
	}
	static int[] dr = {-1, 1, 0, 0};
	static int[] dc = {0, 0, 1, -1};
	public static void dfs(int r, int c, char rgb, char[][] map) {
		map[r][c] = 'a';
		for(int d = 0 ; d < 4 ; d++) {
			int nr = r + dr[d];
			int nc = c + dc[d];
			if(nr < 0 || nc < 0 || nr >= n || nc >= n) continue;
			if(map[nr][nc] != rgb || map[nr][nc] == 'a') continue;
			dfs(nr, nc, rgb, map);
		}
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 17143. 낚시왕  (0) 2021.05.12
[BAEKJOON] 15686. 치킨 배달  (0) 2021.05.10
[BAEKJOON] 14500. 테트로미노  (0) 2021.05.09
[BAEKJOON] 14503. 로봇 청소기  (0) 2021.05.09
[BAEKJOON] 11054. 가장 긴 바이토닉 부분 수열  (0) 2021.05.06