1. 카카오 프렌즈 컬러링북
https://programmers.co.kr/learn/courses/30/lessons/1829
(1) BFS 또는 DFS 로 풀 것인지 결정
(2) 두 가지를 고려 ( 영역의 개수, 영역의 최대 너비 )
(3) 탐색의 진입점 고려
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import java.io.*; import java.util.*; public class Main { static OutputStreamWriter osw; static BufferedWriter bw; public static void main(String[] args) throws IOException { bw = new BufferedWriter(osw); int m = 6; int n = 4; int[][]picture = new int[][]{{1, 1, 1, 0}, {1, 2, 2, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 3}, {0, 0, 0, 3}}; int[]result = solution(m, n, picture); bw.write(result[0] + " " + result[1] + "\n"); bw.flush(); bw.close(); } public static int[] solution(int m, int n, int[][] picture) { // 방문배열 int visited[][] = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ visited[i][j] = (picture[i][j] == 0)? -1 : 0; } } int[]move1 = {1, -1, 0, 0}; int[]move2 = {0, 0, 1, -1}; int areaCount = 0; int maxArea = 0; for(int i = 0; i < picture.length; i++){ for(int j = 0; j < picture[i].length; j++){ // 색칠이 된 상태 && 미방문 if(picture[i][j] != 0 && visited[i][j] == 0){ areaCount += 1; int areaSize = 0; visited[i][j] = picture[i][j]; Queue<Point> q = new LinkedList<Point>(); q.add(new Point(i, j)); // BFS while(!q.isEmpty()){ Point p = q.poll(); areaSize += 1; for(int mv = 0; mv < 4; mv++){ int my = p.y + move1[mv]; int mx = p.x + move2[mv]; if(my < 0 || my >= m || mx < 0 || mx >= n) continue; if(visited[my][mx] != 0 || picture[my][mx] == 0) continue; // 값에 따라 구역을 나눔. if(picture[my][mx] != visited[p.y][p.x]) continue; q.add(new Point(my, mx)); visited[my][mx] = visited[p.y][p.x]; } } maxArea = Math.max(maxArea, areaSize); }// 색칠 방문 } // for } // for // console. // for(int i = 0; i < m; i++){ // for(int j = 0; j < n; j++){ // System.out.print(visited[i][j] + " "); // } // System.out.println(); // } return new int[]{areaCount, maxArea}; } } class Point{ int y; int x; public Point(int y, int x){ this.y = y; this.x = x; } } | cs |
'Problem Solving & Algorithm > Contest' 카테고리의 다른 글
20190105 Hello 2019 (0) | 2019.01.05 |
---|---|
20180904 2017 KAKAO BLIND RECRUITMENT [1차] (0) | 2018.09.04 |
20180812 2018 카카오코드 예선 (0) | 2018.08.12 |
20180811 2017 카카오코드 예선 [캠핑] (0) | 2018.08.11 |
20180807 2017 카카오코드 예선 [보행자 천국] (0) | 2018.08.07 |