Skip to content

Commit

Permalink
이것이 취업을 위한 코딩테스트다 Ch05. DFS/BFS - 음료수 얼려 먹기
Browse files Browse the repository at this point in the history
  • Loading branch information
allrightDJ0108 committed Jul 14, 2023
1 parent 4c5d9cb commit 7141195
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
Binary file added CodingTestStudy1.0/bin/ThisIsCT/ch05_01.class
Binary file not shown.
24 changes: 24 additions & 0 deletions CodingTestStudy1.0/bin/ThisIsCT/example/ch05_01.txt
@@ -0,0 +1,24 @@
4 5
00110
00011
11111
00000



15 14
00000111100000
11111101111110
11011101101110
11011101100000
11011111111111
11011111111100
11000000011111
01111111111111
00000000011111
01111111111000
00011111111000
00000001111000
11111111110011
11100011111111
11100011111111
61 changes: 61 additions & 0 deletions CodingTestStudy1.0/src/ThisIsCT/ch05_01.java
@@ -0,0 +1,61 @@
package ThisIsCT;

import java.io.*;

public class ch05_01 {

//ch.05 DFS/BFS
//음료수 얼려 먹기

static int N, M;
static int[][] arr;
static int[][] visited;
static int[][] dir = {{0,1}, {0,-1}, {1,0}, {-1,0}};
static int cnt = 0;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] tempStr = br.readLine().split(" ");
N = Integer.parseInt(tempStr[0]);
M = Integer.parseInt(tempStr[1]);
arr = new int[N][M];
visited = new int[N][M];

for (int i=0; i<N; i++) {
tempStr = br.readLine().split("");
for (int j=0; j<M; j++) {
arr[i][j] = Integer.parseInt(tempStr[j]);
}
}

for (int i=0; i<N; i++) {
for (int j=0; j<M; j++) {
if (arr[i][j] == 0 && visited[i][j] == 0) {
//구멍이 뚫려있으면서 아직 방문하지 않은 구역
cnt++;
dfsFn(i,j);
}
}
}

System.out.println(cnt);

}

static void dfsFn(int x, int y) {

for (int i=0; i<4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];

if (nx >= 0 && ny >= 0 && nx < N && ny < M) {
if (arr[nx][ny] == 0 && visited[nx][ny] == 0) {
visited[nx][ny] = cnt;
dfsFn(nx,ny);
}
}

}

}
}
24 changes: 24 additions & 0 deletions CodingTestStudy1.0/src/ThisIsCT/example/ch05_01.txt
@@ -0,0 +1,24 @@
4 5
00110
00011
11111
00000



15 14
00000111100000
11111101111110
11011101101110
11011101100000
11011111111111
11011111111100
11000000011111
01111111111111
00000000011111
01111111111000
00011111111000
00000001111000
11111111110011
11100011111111
11100011111111

0 comments on commit 7141195

Please sign in to comment.