알고리즘/그래프
[java 프로그래머스] 카카오프렌즈 컬러링북
Meaning_
2022. 5. 21. 06:49
728x90
반응형
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
|
import java.util.*;
class Solution {
public static class Node{
int x,y;
public Node(int x,int y){
this.x=x;
this.y=y;
}
}
public static boolean [][]visit;
public static int dirX[]={-1,1,0,0};
public static int dirY[]={0,0,-1,1};
public static int cnt=0;
public static void BFS(int m,int n,int [][]picture,int i,int j){
Queue<Node>queue=new LinkedList<Node>();
queue.add(new Node(i,j));
visit[i][j]=true;
while(!queue.isEmpty()){
Node node=queue.poll();
for(int k=0;k<4;k++){
int nx=node.x+dirX[k];
int ny=node.y+dirY[k];
if(nx>=0&&nx<m&&ny>=0&&ny<n){
if(picture[node.x][node.y]==picture[nx][ny]&&!visit[nx][ny]){
cnt++;
visit[nx][ny]=true;
BFS(m,n,picture,nx,ny);
}
}
}
}
}
public int[] solution(int m, int n, int[][] picture) {
visit=new boolean[m+1][n+1];
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(picture[i][j]!=0&&!visit[i][j]){
cnt=1;
numberOfArea++;
BFS(m,n,picture,i,j);
if(cnt>maxSizeOfOneArea){
maxSizeOfOneArea=cnt;
}
}
}
}
int[] answer = new int[2];
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
}
|
cs |
그냥 기존에 백준에서 하는대로 하면 됐다. BFS 풀듯이
문제에 있는 어피치랑 입력 예제랑 달라서 이게 뭐지..? 싶었지만 결국 코드는 늘 보던 유형이였다.
728x90
반응형