https://www.acmicpc.net/problem/10026
1. ๋ฌธ์ ์์ฝ
์ฃผ์ด์ง map์ ๋ํ์ฌ, ์ ๋ก์์ฝ์ด ์๋ ์ฌ๋์ R,G,B๋ฅผ ๊ฐ๊ฐ ๋ค๋ฅธ์์ผ๋ก ๋ณด๊ณ ์ ๋ก์์ฝ์ธ ์ฌ๋์ R,G๋ ๊ฐ์ ์์ผ๋ก ๋ณธ๋ค.
์ด ๋, ๊ฐ์ ์๋ผ๋ฆฌ์ ๊ตฌ์ญ์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
2. ํ์ด
DFS๋ก ์ํํ ๊ตฌ์ญ์ ๊ฐ์๋ฅผ ์ธ์ฃผ๋ ๋ฌธ์ ์ด๋ค. DFS ํจ์๋ ์ ํ์ ์ธ ํ์ ์ฌ์ฉํ์๋ค.
์ฃผ์ํ ์ ์ ์ ๋ก์์ฝ์ธ ๊ฒฝ์ฐ continue ์กฐ๊ฑด์ด๋ค.
๋๋ ์ด ์กฐ๊ฑด์ ์๋์ ๊ฐ์ด ์ฃผ์๋ค.
์์์ ์ด R๋๋ G์ธ ๊ฒฝ์ฐ, B๋ฅผ ๋ง๋๋ฉด ๋์ด๊ฐ๊ฒ ๋ค (R,G๋ฅผ ๊ฐ์ ์์ผ๋ก ๋ฌถ์)
์์์ ์ด B์ธ ๊ฒฝ์ฐ, B๊ฐ ์๋ ๋ค๋ฅธ ์์ ๋ง๋๋ฉด ๋์ด๊ฐ๊ฒ ๋ค
1. map์์ ๋ชจ๋ ์ ์ ์ํํ๋ฉฐ ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ๋ํด์ DFS๋ฅผ ์ํํ๋ค.
2. DFS๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์๋ผ๋ฆฌ์ ๊ตฌ์ญ์ ๋ชจ๋ ๋ฐฉ๋ฌธํ๋ค, ๋ฐฉ๋ฌธํ์ผ๋ฉด visited๋ฅผ true๋ก ๊ฐฑ์ ํด์ค๋ค.
3. ์ ๋ก์์ฝ์ด ์๋ ๊ฒฝ์ฐ dfs1(), visited1 / ์ ๋ก์์ฝ์ธ ๊ฒฝ์ฐ dfs2(), visited2๋ฅผ ์ฌ์ฉํ๋ค.
3. ๋ฌธ์ ์
1. ์ค๋ณต๋๋ ์ฝ๋๊ฐ ๋ง๋ค.
- dfs ํจ์์ ํ์ ๊ฑฐ์ ๊ฐ๊ณ , ์กฐ๊ฑด ํ ๋์ค๋ง ๋ค๋ฅด๋ค.
2. ๋ฐฐ์ด์ด ๋ง๋ค.
- ์ด์คfor๋ฌธ ๋๋ฆด ๋ ํ๋ฒ์ 2๊ฐ์ dfs๋ฅผ ๋๋ฆฌ๋ ค๊ณ ํ๊ธฐ ๋๋ฌธ์ dfsํจ์ 2๊ฐ, visited ๋ฐฐ์ด 2๊ฐ๊ฐ ํ์ํ๋ค.
๋ฐ๋ณต๋ฌธ์ ์ฌ๋ฌ๋ฒ ๋๋ฆฌ๋ ๊ฒ๋ณด๋ค ๋ฐฐ์ด์ ํ๋ ๋ ๋ง๋๋๊ฒ ๋ซ์ง์์๊น? ํ๋ ์๊ฐ์ผ๋ก ๋ง๋ค์๋ค.
4. ์ฝ๋
๋ฐฉ๋ฒ1. dfs1(), dfs2() ๊ตฌ๋ถ
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
static int[] dy = {-1, 1, 0, 0};
static int[] dx = {0, 0, -1, 1};
static int N, cnt1, cnt2;
static char color;
static char[][] map;
static boolean[][] visited1, visited2;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new char[N][N];
visited1 = new boolean[N][N];
visited2 = new boolean[N][N];
for (int i = 0; i < N; i++) {
map[i] = br.readLine().toCharArray();
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
color = map[i][j];
if (!visited1[i][j]) {
dfs1(i, j);
cnt1++;
}
if (!visited2[i][j]) {
dfs2(i, j);
cnt2++;
}
}
}
System.out.println(cnt1 + " " + cnt2);
}
//์ ๋ก์์ฝ ์๋
static void dfs1(int y, int x) {
visited1[y][x] = true;
for (int d = 0; d < 4; d++) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 || nx < 0 || ny >= N || nx >= N||visited1[ny][nx]) continue;
if (map[ny][nx] != color) continue;
visited1[ny][nx] = true;
dfs1(ny, nx);
}
}
// ์ ๋ก์์ฝ
static void dfs2(int y, int x) {
visited2[y][x] = true;
for (int d = 0; d < 4; d++) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 || nx < 0 || ny >= N || nx >= N || visited2[ny][nx]) continue;
if ((color == 'R' || color == 'G') && map[ny][nx] == 'B') continue;
else if (color == 'B' && map[ny][nx] != 'B') continue;
visited2[ny][nx] = true;
dfs2(ny, nx);
}
}
}
๋ฐฉ๋ฒ2. dfs ๊ฐ์ด ์
* visited ๋ฐฐ์ด์ ์ฌํ์ฉํ๊ธฐ ์ํด ์ค๊ฐ์ ์ด๊ธฐํ๋ฅผ ํด์ค์ผ ํ๋ค.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BOJ_10026_์ ๋ก์์ฝ {
static int[] dy = {-1, 1, 0, 0};
static int[] dx = {0, 0, -1, 1};
static int N, cnt1, cnt2;
static char color;
static char[][] map;
static boolean[][] visited;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new char[N][N];
visited = new boolean[N][N];
//์
๋ ฅ๋ฐ๋๋ฐฉ๋ฒ1. ํ์ค์ ํต์งธ๋ก ๋ฃ์ด์ค
for (int i = 0; i < N; i++) {
map[i] = br.readLine().toCharArray();
}
//์
๋ ฅ๋ฐ๋๋ฐฉ๋ฒ2. ๋ฌธ์ ํ๋ํ๋๋ฅผ ๋ฃ์ด์ค -> ์
๋ ฅ๋ฐ์ ๋ ์กฐ๊ฑด์ ์ฒ๋ฆฌํด์ค ์ ์์
/*
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
map[i][j] = br.readLine().charAt(j);
}
}
*/
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
color = map[i][j];
if (!visited[i][j]) {
dfs1(i, j);
cnt1++;
}
}
}
//visited ์ด๊ธฐํ ๋ฐฉ๋ฒ1 - Arrays.fill(๋ฐฐ์ด, false);
for (int i = 0; i < N; i++) {
Arrays.fill(visited[i], false);
}
//visited ์ด๊ธฐํ ๋ฐฉ๋ฒ2 - ๊ฐ์ฒด ์๋ก ์์ฑ
//visited = new boolean[N][N];
//map์ R->G๋ก ๋ฐ๊ฟ์ค
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] == 'R') {
map[i][j] = 'G';
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
color = map[i][j];
if (!visited[i][j]) {
dfs1(i, j);
cnt2++;
}
}
}
System.out.println(cnt1 + " " + cnt2);
}
//์ ๋ก์์ฝ ์๋
static void dfs1(int y, int x) {
visited[y][x] = true;
for (int d = 0; d < 4; d++) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 || nx < 0 || ny >= N || nx >= N||visited[ny][nx]) continue;
if (map[ny][nx] != color) continue;
visited[ny][nx] = true;
dfs1(ny, nx);
}
}
/*
// ์ ๋ก์์ฝ
static void dfs2(int y, int x) {
visited2[y][x] = true;
for (int d = 0; d < 4; d++) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 || nx < 0 || ny >= N || nx >= N || visited2[ny][nx]) continue;
if ((color == 'R' || color == 'G') && map[ny][nx] == 'B') continue;
else if (color == 'B' && map[ny][nx] != 'B') continue;
visited2[ny][nx] = true;
dfs2(ny, nx);
}
}
*/
}
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 15686 ์นํจ๋ฐฐ๋ฌ [Java] (0) | 2022.08.29 |
---|---|
[BOJ] 16637 ๊ดํธ์ถ๊ฐํ๊ธฐ [Java] (0) | 2022.08.28 |
[BOJ] 1697 ์จ๋ฐ๊ผญ์ง [Java] (0) | 2022.08.25 |
[BOJ] 2606 ๋ฐ์ด๋ฌ์ค [Java] (0) | 2022.08.25 |
[BOJ] 1759 ์ํธ ๋ง๋ค๊ธฐ [Java] (0) | 2022.08.24 |