본문 바로가기
프로그래머스/카카오 코딩테스트

[프로그래머스] 카카오 프렌즈 컬러링북 (java)(1829)

by 유줘니 2019. 5. 28.

원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/1829

문제 참고 : https://lkhlkh23.tistory.com/48

 

class Solution {
    
    public static boolean[][] visited;
	public static int[][] image;

    
  	public static int[] solution(int m, int n, int[][] picture) {
	      int[] answer = new int[2];
	      visited = new boolean[m][n];
	      image = picture.clone();
	      
	      for(int i = 0; i < m ; i++) {
	    	  for(int j = 0; j < n ; j++) {
	    		  if(image[i][j]>0) {
	    			  int count = getEqualSpaceCount(i,j,image[i][j]);
	    			  answer[0] = count > 0 ? answer[0]+1 : answer[0];
	    			  answer[1] = Math.max(count, answer[1]);
	    		  }
	    	  }
	      }
	    
	      return answer;
	}
	
	public static int getEqualSpaceCount(int i, int j,  int value) {
		if(	   i<0 
			|| i >= image.length 
			|| j < 0 
			|| j >= image[0].length 
			|| visited[i][j]==true 
			|| value != image[i][j])
			return 0;
		else {
			visited[i][j] = true;
			return 1 +
					getEqualSpaceCount(i+1,j,value) +
					getEqualSpaceCount(i-1,j,value) +
					getEqualSpaceCount(i,j+1,value) +
					getEqualSpaceCount(i,j-1,value) ;
		}
	}
}

댓글