-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExam22_Programers_level2_1.java
More file actions
68 lines (63 loc) · 1.75 KB
/
Copy pathExam22_Programers_level2_1.java
File metadata and controls
68 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package algorithmExam;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Exam22_Programers_level2_1 {
//문제링크
//https://programmers.co.kr/learn/courses/30/lessons/1829
static int m = 6;
static int n = 4;
static int[][] arr =
{{1, 1, 1, 0},
{1, 2, 2, 0},
{1, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 3},
{0, 0, 0, 3}};
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, 1, -1};
static int count;
static int max=0;
static int areaCnt=0;
public static void main(String[] args) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if(arr[i][j]!=0){
areaCnt++; //밑에 dfs가돌고나면 훑었던곳은 0이되므로 다시 arr[i][j]!=0이 만나는곳은 새로운 영역이므로 count
count=0; //한 영역의 갯수를 세어줌
showArr(arr);
System.out.println("=========");
dfs(i,j);
if(count>max){
max=count;
}
}
}
}
System.out.println(areaCnt+" "+max);
}
static void dfs(int x, int y) {
int currentNum = arr[x][y];
arr[x][y] = 0;
count++;
for(int i=0; i<4; i++) {
// 현위치에서 좌,우,위,아래에 접근하기 위함
int nx = x + dx[i];
int ny = y + dy[i];
//0과 n사이 범위에 속할때만
if(0 <= nx && nx < m && 0 <= ny && ny < n) {
if(arr[nx][ny] == currentNum)
dfs(nx, ny);
}
}
}
//배열출력 int[][]
public static void showArr(int[][] param){
for(int i=0;i<param.length;i++){
for (int j = 0; j < param[i].length; j++) {
System.out.print(param[i][j]+" ");
}
System.out.println();
}
}
}