강의로 돌아가기
형태희

어느부분이 잘못됬는지 못찾겠어요

테스트 케이스를 여러 개 만들어봤는데 틀린 부분을 못찾겠어요.
문제이해를 잘못한걸까요

import java.util.ArrayList;

public class CallingBook_FillFlood {
public static void main(String[] args) {

        Solution sol = new Solution();

        /*int[][] picture = { { 1, 1, 1, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 }, { 3, 3, 3, 1 },
                { 3, 3, 3, 1 } };
        sol.solution(6, 4, picture);*/

        /*int[][] picture = { { 0, 0 }, { 0, 0 } };
        sol.solution(2, 2, picture);*/

        /*int[][] picture = { { 1, 1, 1, 1 }, { 1, 4, 1, 1 }, { 1, 3, 2, 1 }, { 1, 1, 1, 1 } };
        sol.solution(4, 4, picture);*/

        /*int[][] picture = { { 1, 1, 1, 0 }, { 1, 2, 2, 0 }, { 1, 0, 0, 1 }, { 0, 0, 0, 1 }, { 0, 0, 0, 3 },
                { 0, 0, 0, 3 } };
        sol.solution(6, 4, picture);*/

        /*int[][] picture = { { 1, 1, 1, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 }, { 0, 0, 0, 1 },
                { 0, 0, 0, 1 } };
        sol.solution(6, 4, picture);*/
        /*int[][] picture= {{1,1,1,0,0,0,0,1,1,1},
             {1,1,1,1,0,0,1,1,1,1},
             {1,1,1,1,1,1,1,1,1,1},
             {1,1,1,2,1,1,1,1,2,1},
             {1,1,2,1,2,1,1,2,1,2}};
        sol.solution(5, 10, picture);*/
        int[][] picture = {{1,2},{1,1}};
        sol.solution(2, 2, picture);
    }
}

class Solution {
    int[] result;
    boolean[][] checkPicture;
    ArrayList<Integer> findColor;

    int[] solution(int m, int n, int[][] picture) {
        result = new int[2];
        checkPicture = new boolean[m][n];
        findColor = new ArrayList<Integer>();

        int max_Size_of_one_area = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int color = picture[i][j];
                if (picture[i][j] == 0) {
                    continue;
                }
                int resionSize = floodfill(i, j, m, n, picture, color);
                if (resionSize > max_Size_of_one_area) {
                    max_Size_of_one_area = resionSize;
                }
            }
        }


        result[0] = findColor.size();
        result[1] = max_Size_of_one_area;
        if(findColor.size()==0) {
            result[0] =1;
        }
        System.out.println("result[0]" + result[0] + " result[1]" + result[1]);
        return result;
    }// End Solution

    public int floodfill(int i, int j, int m, int n, int[][] picture, int beforeColor) {

        if ((i >= m) || (j >= n) || (i < 0) || (j < 0) || (checkPicture[i][j] == true)) {
            return 0;
        }

        int color = picture[i][j];

        if (!findColor.contains(color)) {
            findColor.add(color);
        }
        if (beforeColor != color) {
            return 0;
        }
        checkPicture[i][j] = true;
        int resionSize = 1;
        resionSize += floodfill(i + 1, j, m, n, picture, color);
        resionSize += floodfill(i - 1, j, m, n, picture, color);
        resionSize += floodfill(i, j + 1, m, n, picture, color);
        resionSize += floodfill(i, j - 1, m, n, picture, color);

        return resionSize;
    }// end floodfill

}// End class Solution
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.