https://www.acmicpc.net/problem/1388
๐ฉ๋ฌธ์
๐๋ฌธ์ ํด๊ฒฐ - BFS
FIFO - ๋จผ์ ์ฒ๋ฆฌ๋ ๋ ธ๋๋ถํฐ ์ฒ๋ฆฌ,
Queue์ฌ์ฉํ์์ ๊ฐ๋ก๋ก ์งํ, ๋จผ์ ํ์๋ ๋ ธ๋์ ์ธ์ ํ ๋ ธ๋๋ค์ ๋ชจ๋ ํ์ ํ ๋ค์ ํ์์์
1.bfsํจ์๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ ์์ ์ด๋ฏ๋ก์ ์ญ๋ณ์ : ๊ฐ๋ก, ์ธ๋ก์ด๋๋ฐฉํฅ ,๋ฐฉ๋ฌธ์ฌ๋ถ์ฒดํฌ ๋ฐฐ์ด , ์์น๊ฐ(x,y)๋ฅผ ๋จผ์ ์ ์ธํ๊ณ ์์ํ๋ค.
2. main์์๋2-1. ์ ๋ ฅ๊ฐ์ ๋ชจ๋ ๋ฐ์์ ์ ์ฅํ๊ณ ์ฃผ์ด์ง ๋ฐฐ์ด์ ํฌ๊ธฐ๊น์ง ์ง์ ํด์ค๋ค.2-2. for๋ฌธ์ ๋๋ฉด์ ๋ฐฉ๋ฌธํ์ง์์ ๊ณณ์ด๋ผ๋ฉด ํด๋น ์์น๊ฐ(x,y)๊ณผ ๋ฌธ์๊ฐ ์ ์ฅ๋ ๋ฐฐ์ด์ bfsํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๋๊ฒจ์ค๋ค.2-3. ๊ทธ๋ฆฌ๊ณ ๋๋ฌดํ์์ ๊ฐ์๋ฅผ +1ํด์ค๋ค(๋๋ฌดํ์๊ฐ ๊ฐ๋ก์ผ์๋ ์ธ๋ก์ผ์๋ ์์ง๋ง ๋ฐ๋ก ๊ตฌ๋ถํ์ง ์๊ณ ์ถ๋ ฅํ๊ธฐ ๋๋ฌธ์ ํด๋น if๋ฌธ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๋ฉด +1์ ํด์ฃผ๋ฉด๋จ)
3.bfsํจ์์์๋3-1. ๋จผ์ ๋ฐฉ๋ฌธํ ์์น์ ๋ณด๋ฅผ ์ ์ฅํ Deque๋ฅผ ์ ์ธํ๋ค.
3-2. Deque์ ์์น์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ ,ํด๋น ์์น์ ๋ณด๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ(true) ํด์ค๋ค.
3-3. ํ์ฌ ๋ฌธ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด์ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ๋ ๊ฐ๋ค์ ์ด์ฉํ์ฌ ํ์ฌ ์์น์ ๋ฌธ์๊ฐ์ ์ ์ฅํ๋ค.
3-4. while๋ฌธ์ ํตํด์ Deque๊ฐ ๋น๋๊น์ง ๋ฐ๋ณต๋ฌธ์ ๋๋ค.
while๋ฌธ ์์์์ ๋ก์ง
1. ํ์์ ๊ฐ์ ๊บผ๋ด ํ์ฌ์์น์ ๋ณด๋ฅผ ์ ์ฅํ๋ x,y๋ณ์์ ๊ฐ๊ฐ ๋ด๋๋ค.
2. ํ์ฌ ์์น์ ๋ฌธ์๊ฐ์ด'-'๋ฉด ๊ฐ๋กํ์(y์ถ์ด๋), '|'๋ฉด ์ธ๋ก ํ์(x์ถ์ด๋) ์กฐ๊ฑด๋ฌธ์ ๋ฐ๋ก ๋ง๋ค์ด์ค๋ค.
3. ์ ์ญ๋ณ์๋ก ์ ์ธํ ์ด๋๋ฐฉํฅ์ ๊ฐ์ง๊ณ ํ์์ ์์ํ๋ค.
4. ๋ชจ๋ ์์น๊ฐ์ 0์ด์์ด๊ณ , ์ฃผ์ด์ง ๋ฐฐ์ด๊ณผ ๊ฐ๊ฑฐ๋ ํฌ๋ฉด ์๋๋ค(์ธ๋ฑ์ค๊ฐ 0์ผ๋ก ์์ํ๊ธฐ ๋๋ฌธ์ ๊ฐ์๋ ์ค๋ฅ๋จ)
๋๋ ํด๋น๋ฌธ์๊ฐ ์๋๋๊น์ง ๋ฐ๋ณตํ๋ค. =>ํด๋น ์กฐ๊ฑด์ ๊ฑธ๋ฆฌ๋ฉด ๋ค์ ์์น๊ฐ์ผ๋ก ๋์ด๊ฐ๋ค.
5. ์กฐ๊ฑด์ ๊ฑธ๋ฆฌ์ง ์์ผ๋ฉด ํ์ ํด๋น ์์น๊ฐ์ ๋ฃ์ด์ฃผ๊ณ , ํด๋น ์์น์ ๋ณด๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ ํด์ค๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
/*
* ์
๋ ฅ
์ธ๋ก๊ธธ์ด n ๊ฐ๋ก๊ธธ์ด m
n๊ฐ์ ์ค์ ๊ฑธ์ณ ํ์ค์ m๊ฐ์ ๋ฌธ์๊ฐ ์ฃผ์ด์ง
* ์ถ๋ ฅ
ํ์ํ ๋๋ฌดํ์ ๊ฐ์ ์ถ๋ ฅ
* */
public class Main {
static int x, y;
static boolean[][] check;
static int[][] vecX = {{1, 0}, {-1, 0}}; // ์ธ๋ก ์ด๋ ๋ฐฉํฅ
static int[][] vecY = {{0, 1}, {0, -1}}; // ๊ฐ๋ก ์ด๋ ๋ฐฉํฅ
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split(" ");
x = Integer.parseInt(arr[0]);
y = Integer.parseInt(arr[1]);
char[][] bottom = new char[x][y]; // ๋ฌธ์ ์ ์ฅ
check = new boolean[x][y]; // ๋ฐฉ๋ฌธ ์ฒดํฌ ์ ์ฅ
for (int i = 0; i < x; i++) { // ๋ฌธ์ ์
๋ ฅ๋ฐ๊ณ ์ ์ฅ
String str = br.readLine();
for (int j = 0; j < y; j++) {
bottom[i][j] = str.charAt(j);
}
}
int cnt = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (!check[i][j]) {
bfs(i, j, bottom);
cnt++;
}
}
}
System.out.println(cnt);
}
//์ฌ๊ธฐ๊น์ง๋ dfs์ ๋๊ฐ๋ค
static void bfs(int i, int j, char[][] bottom) {
Deque<int[]> queue = new LinkedList<>();
queue.add(new int[]{i, j});
check[i][j] = true; // ์์ ์์น ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
char current = bottom[i][j];
while (!queue.isEmpty()) {
int[] currentPos = queue.poll();
int cx = currentPos[0];
int cy = currentPos[1];
// ๊ฐ๋กํ์ -
// y๋ 0๋ณด๋ค๊ฐ๊ฑฐ๋ ํฌ๊ณ ๊ธฐ์กด๊ฐ์ ๋์ด๊ฐ๋ฉด ์๋๋ค. & ์ด์ด๋ฐ์ ์์น๊ฐ ๋ฐฉ๋ฌธํ์ ์ด ์์ด์ผํ๋ค & ์ด์ด๋ฐ์ ๊ฐ์ด -๋ก ๊ฐ์์ผํ๋ค
if (current == '-') {
for (int[] v : vecY) {
int nx = cx;
int ny = cy + v[1];
if (ny >= 0 && ny < y && !check[nx][ny] && bottom[nx][ny] == '-') {
// DFS์ ๋ค๋ฅธ์ , ํ ์ฌ์ฉ
queue.add(new int[]{nx, ny});
check[nx][ny] = true;
}
}
}
// ์ธ๋กํ์ |
// x ๋ 0๋ณด๋ค๊ฐ๊ฑฐ๋ ํฌ๊ณ ๊ธฐ์กด๊ฐ์ ๋์ด๊ฐ๋ฉด ์๋๋ค. & ์ด์ด๋ฐ์ ์์น๊ฐ ๋ฐฉ๋ฌธํ์ ์ด ์์ด์ผํ๋ค & ์ด์ด๋ฐ์ ๊ฐ์ด |๋ก ๊ฐ์์ผํ๋ค
if (current == '|') {
for (int[] v : vecX) {
int nx = cx + v[0];
int ny = cy;
if (nx >= 0 && nx < x && !check[nx][ny] && bottom[nx][ny] == '|') {
// DFS์ ๋ค๋ฅธ์ , ํ ์ฌ์ฉ
queue.add(new int[]{nx, ny});
check[nx][ny] = true;
}
}
}
}
}
}
๐๋ฌธ์ ํด๊ฒฐ - DFS (์ฌ๊ท)
LIFO - ๊ฐ์ฅ ์ต๊ทผ์ ํ์ํ ๋ ธ๋๋ถํฐ ์ฒ๋ฆฌ, Stack ์ฌ์ฉ
ํ์์ ์ธ๋ก(์์ง)๋ก ์งํ, ๊ฒฝ๋ก์ ๋๊น์ง ํ์ํ ๋ค ๋์์ด
1.์ ์ญ๋ณ์๋ฅผ ์ ์ธํ๊ณ , main ํจ์์์์ ๋ก์ง์ BFS์ ๋๊ฐ๋ค.
2. DFSํจ์์์๋2-1. ์ ๋ฌ๋ฐ์ ์์น์ ๋ณด๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ(true)ํด์ค๋ค. //BFS์ ๊ฐ์
2-2. ํ์ฌ ๋ฌธ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด(current)์ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌ๋ ๊ฐ๋ค์ ์ด์ฉํ์ฌ ํ์ฌ ์์น์ ๋ฌธ์๊ฐ์ ์ ์ฅํ๋ค. // BFS์ ๊ฐ์2-3. ํ์ฌ ์์น์ ๋ฌธ์๊ฐ์ด '-'๋ฉด ๊ฐ๋ก ํ์(y์ถ ์ด๋), '|'๋ฉด ์ธ๋ก ํ์(x์ถ ์ด๋) ์กฐ๊ฑด๋ฌธ์ ๋ฐ๋ก ๋ง๋ค์ด์ค๋ค.
2-4. BFS์ while๋ฌธ ์์์ ๋ก์ง๊ณผ ๋๊ฐ๋ค.2-5. BFS์ ๋ค๋ฅธ์ ์ DFSํจ์๋ฅผ ๋ค์ ํธ์ถํ๋ค.
DFS์์๋ ๋ค์ ํจ์๋ฅผ ํธ์ถํ์ฌ 2-1๋ฒ๋ถํฐ ์์ํ๊ธฐ๋๋ฌธ์ ๋ฐ๋ก ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ if๋ฌธ์์์ ํด์คํ์๊ฐ ์๋ค.
(BFS์์๋ if๋ฌธ ์กฐ๊ฑด์ ํด๋นํ์ง ์์ผ๋ฉด ํ์ ์์น๊ฐ ์ ์ฅ & ์์น์ ๋ณด ๋ฐฉ๋ฌธ์ฒ๋ฆฌ)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* ์
๋ ฅ
์ธ๋ก๊ธธ์ด n ๊ฐ๋ก๊ธธ์ด m
n๊ฐ์ ์ค์ ๊ฑธ์ณ ํ์ค์ m๊ฐ์ ๋ฌธ์๊ฐ ์ฃผ์ด์ง
* ์ถ๋ ฅ
ํ์ํ ๋๋ฌดํ์ ๊ฐ์ ์ถ๋ ฅ
* */
public class Main {
static int x,y;
static boolean[][]check;
static int[][] vecX = {{1, 0}, {-1, 0}}; //์ธ๋ก์ด๋ ๋ฐฉํฅ
static int[][] vecY = {{0, 1}, {0, -1}}; //๊ฐ๋ก์ด๋๋ฐฉํฅ
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String [] arr = br.readLine().split(" ");
x = Integer.parseInt(arr[0]);
y = Integer.parseInt(arr[1]);
char [][] bottom = new char [x][y]; //๋ฌธ์์ ์ฅ
check = new boolean[x][y]; //๋ฐฉ๋ฌธ์ฒดํฌ ์ ์ฅ
for (int i = 0; i < x; i++) { //๋ฌธ์ ์
๋ ฅ๋ฐ๊ณ ์ ์ฅ
String str = br.readLine();
for (int j = 0; j < y; j++) {
bottom[i][j] = str.charAt(j);
}
}
int cnt =0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (!check[i][j]) {
dfs(i,j,bottom);
cnt++;
}
}
}
System.out.println(cnt);
}
static void dfs(int i, int j,char [][]bottom){
check[i][j] = true; //๋ฐฉ๋ฌธํจ
char current = bottom[i][j];
// ๊ฐ๋กํ์ -
// y๋ 0๋ณด๋ค๊ฐ๊ฑฐ๋ ํฌ๊ณ ๊ธฐ์กด๊ฐ์ ๋์ด๊ฐ๋ฉด ์๋๋ค. & ์ด์ด๋ฐ์ ์์น๊ฐ ๋ฐฉ๋ฌธํ์ ์ด ์์ด์ผํ๋ค & ์ด์ด๋ฐ์ ๊ฐ์ด -๋ก ๊ฐ์์ผํ๋ค
if(current=='-'){
for (int[] v:vecY){
int nx=i;
int ny= j+v[1];
if(ny >= 0 && ny < y && !check[nx][ny] && bottom[nx][ny] == '-'){
// BFS์ ๋ค๋ฅธ์ , ์ฌ๊ทํจ์ ์ฌ์ฉ
dfs(nx,ny,bottom);
}
}
}
// ์ธ๋กํ์ |
// x ๋ 0๋ณด๋ค๊ฐ๊ฑฐ๋ ํฌ๊ณ ๊ธฐ์กด๊ฐ์ ๋์ด๊ฐ๋ฉด ์๋๋ค. & ์ด์ด๋ฐ์ ์์น๊ฐ ๋ฐฉ๋ฌธํ์ ์ด ์์ด์ผํ๋ค & ์ด์ด๋ฐ์ ๊ฐ์ด |๋ก ๊ฐ์์ผํ๋ค
if(current=='|'){
for (int[] v:vecX){
int nx=i+v[0];
int ny= j;
if(nx >= 0 && nx < x && !check[nx][ny] && bottom[nx][ny] == '|'){
// BFS์ ๋ค๋ฅธ์ , ์ฌ๊ทํจ์ ์ฌ์ฉ
dfs(nx,ny,bottom);
}
}
}
}
}
๐๋ฌธ์ ํด๊ฒฐ - DFS (์คํ)
1.BFS์ ์ ๋ฐ์ ์ผ๋ก ๋ชจ๋ ๋๊ฐ๋ค.
๋ค๋ฅธ์ ์ BFS๋ FIFO๋ฐฉ์์ ์ฌ์ฉํ์ฌ Deque์ ์์น๊ฐ์ ๋ฃ๊ณ ๋นผ๊ณ ์์ ์ ํ์์ง๋ง
DFS๋ LIFO๋ฐฉ์์ ์ฌ์ฉํ์ฌ Stack์ ์์น๊ฐ์ ๋ฃ๊ณ ๋นผ๊ณ ์์ ์ ํ๋ค๋ ๊ฒ์ด๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/*
* ์
๋ ฅ
์ธ๋ก๊ธธ์ด n ๊ฐ๋ก๊ธธ์ด m
n๊ฐ์ ์ค์ ๊ฑธ์ณ ํ์ค์ m๊ฐ์ ๋ฌธ์๊ฐ ์ฃผ์ด์ง
* ์ถ๋ ฅ
ํ์ํ ๋๋ฌดํ์ ๊ฐ์ ์ถ๋ ฅ
* */
public class Main {
static int x, y;
static boolean[][] check;
static int[][] vecX = {{1, 0}, {-1, 0}}; // ์ธ๋ก ์ด๋ ๋ฐฉํฅ
static int[][] vecY = {{0, 1}, {0, -1}}; // ๊ฐ๋ก ์ด๋ ๋ฐฉํฅ
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split(" ");
x = Integer.parseInt(arr[0]);
y = Integer.parseInt(arr[1]);
char[][] bottom = new char[x][y]; // ๋ฌธ์ ์ ์ฅ
check = new boolean[x][y]; // ๋ฐฉ๋ฌธ ์ฒดํฌ ์ ์ฅ
for (int i = 0; i < x; i++) { // ๋ฌธ์ ์
๋ ฅ๋ฐ๊ณ ์ ์ฅ
String str = br.readLine();
for (int j = 0; j < y; j++) {
bottom[i][j] = str.charAt(j);
}
}
int cnt = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (!check[i][j]) {
dfs_stack(i, j, bottom);
cnt++;
}
}
}
System.out.println(cnt);
}
static void dfs_stack(int i, int j, char[][] bottom) {
Stack<int[]> stack = new Stack<>();
stack.push(new int[]{i, j});
check[i][j] = true; // ์์ ์์น ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
char current = bottom[i][j];
while (!stack.isEmpty()) {
int[] currentPos = stack.pop();
int cx = currentPos[0];
int cy = currentPos[1];
// ๊ฐ๋กํ์ -
// y๋ 0๋ณด๋ค๊ฐ๊ฑฐ๋ ํฌ๊ณ ๊ธฐ์กด๊ฐ์ ๋์ด๊ฐ๋ฉด ์๋๋ค. & ์ด์ด๋ฐ์ ์์น๊ฐ ๋ฐฉ๋ฌธํ์ ์ด ์์ด์ผํ๋ค & ์ด์ด๋ฐ์ ๊ฐ์ด -๋ก ๊ฐ์์ผํ๋ค
if (current == '-') {
for (int[] v : vecY) {
int nx = cx;
int ny = cy + v[1];
if (ny >= 0 && ny < y && !check[nx][ny] && bottom[nx][ny] == '-') {
// BFS์ ์ฌ๊ทํจ์์ ๋ค๋ฅธ์ , ์คํ์ฌ์ฉ
stack.push(new int[]{nx, ny});
check[nx][ny] = true;
}
}
}
// ์ธ๋กํ์ |
// x ๋ 0๋ณด๋ค๊ฐ๊ฑฐ๋ ํฌ๊ณ ๊ธฐ์กด๊ฐ์ ๋์ด๊ฐ๋ฉด ์๋๋ค. & ์ด์ด๋ฐ์ ์์น๊ฐ ๋ฐฉ๋ฌธํ์ ์ด ์์ด์ผํ๋ค & ์ด์ด๋ฐ์ ๊ฐ์ด |๋ก ๊ฐ์์ผํ๋ค
if (current == '|') {
for (int[] v : vecX) {
int nx = cx + v[0];
int ny = cy;
if (nx >= 0 && nx < x && !check[nx][ny] && bottom[nx][ny] == '|') {
// BFS์ ์ฌ๊ทํจ์์ ๋ค๋ฅธ์ , ์คํ์ฌ์ฉ
stack.push(new int[]{nx, ny});
check[nx][ny] = true;
}
}
}
}
}
}
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java]BOJ 1654 : ๋์ ์๋ฅด๊ธฐ (0) | 2025.01.15 |
---|---|
[Java]BOJ 2776 : ์๊ธฐ์ (0) | 2025.01.13 |
[Java]BOJ 1026 : ๋ณด๋ฌผ (0) | 2024.12.06 |
[Java]BOJ 16112 : 5์ฐจ ์ ์ง (0) | 2024.12.03 |
BOJ 1863 : ์ค์นด์ด๋ผ์ธ ์ฌ์ด๊ฑฐ (0) | 2024.12.03 |