Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
이것이 취업을 위한 코딩테스트다 Ch05. DFS/BFS - 음료수 얼려 먹기
- Loading branch information
1 parent
4c5d9cb
commit 7141195
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |