공원 산책 (자바)

김재현·2023년 12월 12일
0

알고리즘 풀이

목록 보기
48/89

문제

오답 코드

    public int[] solution(String[] park, String[] routes) {
        int[] answer = {0,0};

        // 시작 지점을 숫자로
        int startX = 0;
        int startY = 0;

        findStartPoint:
        for (int i = 0; i < park.length; i++) {
            for (int j = 0; j < park[i].length(); j++) {
                if (park[i].charAt(j) == 'S') {
                    startY = i;
                    startX = j;
                    break findStartPoint;
                }
            }
        }

        // 이동 시키기
        move:
        for (String route : routes) {
            String direction = route.substring(0, 1);
            int moving = Integer.valueOf(route.substring(2));

            // E일 때
            if (direction.equals("E")) {
                if (startX + moving < park[startY].length()) {
                    if (park[startY].substring(startX,startX+moving).contains("X")) {
                        continue;
                    } else {
                        startX = startX+moving;
                    }
                }
            }

            // W일 때
            if (direction.equals("W")) {
                if (startX - moving >=0) {
                    if (park[startY].substring(startX-moving,startX).contains("X")) {
                        continue;
                    } else {
                        startX = startX-moving;
                    }
                }
            }

            // S일 때
            if (direction.equals("S")) {
                if (startY + moving < park.length) {
                    for (int i=startY;i<=startY+moving;i++) {
                        if (park[i].charAt(startX)=='X') continue move;
                    }
                    startY = startY+moving;
                }
            }

            // N일 때
            if (direction.equals("N")) {
                if (startY - moving >=0) {
                    for (int i=startY;i>=startY-moving;i--) {
                        if (park[i].charAt(startX)=='X') continue move;
                    }
                    startY = startY-moving;
                }
            }
        }

        answer[0]=startY;
        answer[1]=startX;

        return answer;
    }

처음 풀었던 코드이다. 로직 자체는 이상이 없지만 특정 부분에서 오답처리가 되는 것 같았다.
풀다보니 이것보다 효율적인 방법이 떠올라서 바꿔서 풀었다.

정답 풀이

class Solution {
    public int[] solution(String[] park, String[] routes) {
            int[] answer = {0,0};

            // 시작 지점을 숫자로
            int startX = 0;
            int startY = 0;

            findStartPoint:
            for (int i = 0; i < park.length; i++) {
                for (int j = 0; j < park[i].length(); j++) {
                    if (park[i].charAt(j) == 'S') {
                        startY = i;
                        startX = j;
                        break findStartPoint;
                    }
                }
            }

            // 이동 시키기
            move:
            for (String route : routes) {
                String direction = route.substring(0, 1);
                int moving = Integer.valueOf(route.substring(2));
				
                // 임시값
                int tempX=startX;
                int tempY=startY;

                for (int i =0;i<moving;i++) {
                    if (direction.equals("E")) tempX++;
                    if (direction.equals("W")) tempX--;
                    if (direction.equals("N")) tempY--;
                    if (direction.equals("S")) tempY++;
					
                    // 장애물 만나면 임시값 초기화
                    if (tempX>=0 && tempY>=0 && tempX<park[0].length() && tempY<park.length) {
                        if (park[tempY].charAt(tempX) == 'X') continue move;
                    }
                }
				
                // 장애물 없다면 임시값을 실제값에 대입
                if (tempX>=0 && tempY>=0 && tempX<park[0].length() && tempY<park.length) {
                    startX=tempX;
                    startY=tempY;
                }
            }

            answer[0]=startY;
            answer[1]=startX;

            return answer;
    }
}

코드와 로직이 더 간결해서 알아보기 쉬워졌다.

여기에 추가적으로 indexOf를 사용하여 시작값을 찾았다면 더 간결해질 수 있을 것이다.

profile
I live in Seoul, Korea, Handsome

0개의 댓글