개발자 톡

연습문제 톡 장애물 인식 프로그램

장애물 인식 프로그램 TC9 런타임 에러 질문

등록일
2022-08-23 15:10:10
조회수
794
작성자
aaadddggg

처음 지도를 배열에 초기화한 다음 (r, c) 기준에서 위 아래 좌 우로 재귀적으로 파고드는 형식으로 체크하도록 하였습니다.

나머지 케이스는 모두 통과하였는데 TC9에서 런타임 에러가 발생하는데 이유가 뭔지 모르겠습니다.


제 추측에는 재귀적으로 파고드는 방식이다 보니 호출 스택의 최대 허용치를 벗어난건가 싶긴한데 지도 최대크기인 25에서 몇가지 케이스로 돌려보아도 런타임 에러를 재현할 수가 없었습니다.


고수분들의 조언 부탁드립니다 ㅠ.ㅠ




import java.util.*;

import java.io.*;

public class Main
{
    private static int mapSize;
    private static int[][] map;

    public static void main(String args[]) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        mapSize = Integer.parseInt(br.readLine());

        // 맵 초기화
        map = new int[mapSize][mapSize];
        for(int r = 0; r < mapSize; r++){
            char[] line = br.readLine().toCharArray();
            for(int c = 0; c < mapSize; c++){
                if (line[c] == '1') map[r][c] = '-';
            }
        }
        
        List blockList = new ArrayList<>();
        for(int r = 0; r < mapSize; r++){
            for(int c = 0; c < mapSize; c++){
                if (map[r][c] == '-'){
                    int count = search(r, c, blockList.size() + 1);
                    blockList.add(count);
                }
            }
        }

        Collections.sort(blockList);

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("%d
", blockList.size()));

        for(int i = 0; i < blockList.size(); i++){
            sb.append(String.format("%d
", blockList.get(i)));
        }

        System.out.println(sb.toString());
    }

    private static int search(int r, int c, int typeIndex){
        if (map[r][c] != '-') return 0;

        int count = 1;

        map[r][c] = typeIndex;

        if (r > 0) count += search(r - 1, c, typeIndex);
        if (r < mapSize - 1) count += search(r + 1, c, typeIndex);
        if (c > 0) count += search(r, c - 1, typeIndex);
        if (c < mapSize - 1) count += search(r, c + 1, typeIndex);

        return count;
    }
}


#장애물_인식_프로그램
#java
#장애물_인식

이 카테고리의 톡 더보기