끄적끄적 코딩
article thumbnail
728x90

2차원 배열에 색이(R, G, B) 들어있습니다.
인접한(상, 하, 좌,우) 색이 있는 곳들은 같은 구역입니다.
(R, G, B)로 나누었을 때 구역의 수와 ((R, G), B)로 나누었을 때 구역의 수를 구하는 문제입니다.

DFS로 탐색을 해서 같은 구역을 찾아주었습니다.
적록색약((R, G), B)는 R, G를 같은 색으로 계산하도록 해서 구역을 찾았습니다.

위의 과정을 통해 나온 결과들을 출력해주었습니다.

 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
	static int ans = 0;
	static int n;
	static int[][] map;
	static int[][] visit;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		n = Integer.parseInt(br.readLine());
		map = new int[n][n];
		visit = new int[n][n];
		int cnt1 = 0;
		int cnt2 = 0;
		
		for(int i=0; i<n; ++i) {
			String st = br.readLine();
			for(int j=0; j<n; ++j) {
				map[i][j] = st.charAt(j) == 'R' ? 1 : st.charAt(j) == 'G' ? 2 : 4; 
			}
		}
		
		for(int i=0; i<n; ++i) {
			for(int j=0; j<n; ++j) {
				if(visit[i][j] == 1) {
					continue;
				}
				DFS(i, j, map[i][j]);
				++cnt1;
			}
		}
		
		for(int i=0; i<n; ++i) {
			for(int j=0; j<n; ++j) {
				visit[i][j] = 0;
			}
		}
		
		for(int i=0; i<n; ++i) {
			for(int j=0; j<n; ++j) {
				if(visit[i][j] == 1) {
					continue;
				}
				DFS2(i, j, map[i][j] == 4 ? 4 : 3);
				++cnt2;
			}
		}
		
		bw.write(cnt1 + " " + cnt2 + "\n");
		bw.close();
	}
	
	public static void DFS(int y, int x, int find) {
		int[] dy = {0, 1, 0, -1};
		int[] dx = {1, 0, -1, 0};
		for(int i=0; i<4; ++i) {
			int nextY = y + dy[i];
			int nextX = x + dx[i];
			if(nextY < 0 || nextX < 0 || nextY >= n || nextX >= n || visit[nextY][nextX] == 1) {
				continue;
			}
			if(map[nextY][nextX] == find) {
				visit[nextY][nextX] = 1;
				DFS(nextY, nextX, find);
			}
		}
	}
	public static void DFS2(int y, int x, int find) {
		int[] dy = {0, 1, 0, -1};
		int[] dx = {1, 0, -1, 0};
		for(int i=0; i<4; ++i) {
			int nextY = y + dy[i];
			int nextX = x + dx[i];
			if(nextY < 0 || nextX < 0 || nextY >= n || nextX >= n || visit[nextY][nextX] == 1) {
				continue;
			}
			if((map[nextY][nextX] & find) != 0) {
				visit[nextY][nextX] = 1;
				DFS2(nextY, nextX, find);
			}
		}
	}
}

검색 태그