개발자 톡

연습문제 톡 [HSAT 1회 정기 코딩 인증평가 기출] 로봇이 지나간 경로

자바, 런타임 에러

등록일
2024-06-30 20:15:00
조회수
30
작성자
ppssyyy95
import java.io.*;
import java.util.*;

public class Main {

    static int h,w;
    static char[][] map;

    //로봇
    static int sx=-1, sy=-1, sDir=-1;

    static int[] dx = {-1,0,1,0}; //+1;R 북동남서 -1:L
    static int[] dy = {0,1,0,-1};
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        h = sc.nextInt();
        w = sc.nextInt();

        map = new char[h][w];
        for(int i=0; i<h; i++) {
            String s = sc.next();
            for(int j=0; j<w; j++) {
                //#:로봇
                map[i][j] = s.charAt(j);
            }
        }
        // for(int i=0; i<h; i++) {
        //     System.out.println(Arrays.toString(map[i]));
        // }
        //입력 끝

        findStart(); //첫 시작점과 처음 시작 방향 찾기
        System.out.println( (sx+1) +" "+ (sy+1) );
        System.out.println( printDir(sDir) );

        moveRobot(); //로봇 움직이고, 명령 프린트
    }

    private static void moveRobot() {
        int cx = sx;
        int cy = sy;
        int prevDir = sDir;
        int cDir = sDir;
        // findDirection(i,j)
        map[sx][sy] = '.';

        while(true) {
            //현재 위치에서 이어진 길 찾기
            cDir = findDirection(cx, cy);

            if(cDir == -2) { //끝
                return;
            }
            
            else if(prevDir == cDir) { //아까랑 방향이 같으니까 A출력, visit처리
                System.out.print('A');
                map[ cx+dx[cDir] ][ cy+dy[cDir] ] = '.';
                map[ cx+2*dx[cDir] ][ cy+2*dy[cDir] ] = '.';
                cx += 2*dx[cDir];
                cy += 2*dy[cDir];
                // System.out.print("["+cx+" "+cy+"]");
            }
            else { //방향이 바뀜. 
                if( cDir-prevDir==1 || prevDir-cDir==3 ) { //오른쪽으로 90도 회전
                    System.out.print('R');
                }
                else {
                    System.out.print('L');
                }
                prevDir = cDir;
            }
        }
    }

    private static int findDirection(int x, int y) {
        int linkedCnt = 0;
        int dir = -1;
        for(int d=0; d<4; d++) {
            int nx = x + dx[d];
            int ny = y + dy[d];

            if(nx<0 || ny<0 || nx>=h || ny>=w) continue;
            if(map[nx][ny] == '#') {
                linkedCnt ++ ;
                dir = d;
            }
        }

        if (linkedCnt > 1) return -1; //2개 이상으로 이어져 있음
        else if(linkedCnt == 0) return -2; //이어진 게 없음 == 끝점
        else return dir; //하나만 이어짐(현재에서 끝점이기 때문에 갈 곳이 d로 정해짐)
    }

    private static void findStart() {
        for(int i=0; i<h; i++) {
            for(int j=0; j<w; j++) {
                if(map[i][j] == '#') {
                    int dir = findDirection(i, j);
                    if( dir != 0) {
                        sx = i;
                        sy = j;
                        sDir = dir;
                        return;
                    }
                }
            }
        }
        
    }

    private static char printDir(int d) { //북동남서
        if(d==0) return '^';
        else if(d==1) return '>';
        else if(d==2) return 'v';
        else return '<';
    }
}



주어진 예시 두 개는 답이 잘 나오는 걸 확인했습니다.


제출했을 때 subTask1에 있는 예제는 다 통과되었고

subTask2에 있는 예제 중 절반 정도가 "런타임 에러"가 뜨던데 이유가 무엇일까요??

#[HSAT_1회_정기_코딩_인증평가_기출]_로봇이_지나간_경로

이 카테고리의 톡 더보기