(Java)프로그래머스 - 공원 산책

윤준혁·2024년 3월 12일

나의 풀이

class Solution {
    public int[] solution(String[] park, String[] routes) {
        int[] answer = new int[2]; // 1
        int x = 0;
        int y = 0;

        for (int i = 0; i < park.length; i++) { // 2
            for (int j = 0; j < park[i].length(); j++) {
                if (park[i].charAt(j) == 'S') {
                    x = j;
                    y = i;
                    break;
                }
            }
        }

        for (int i = 0; i < routes.length; i++) { // 3
            String[] temp = routes[i].split(" ");
            String op = temp[0];
            int n =  Integer.parseInt(temp[1]);
            int curr_x = x;
            int curr_y = y;

            if (op.equals("N")) { // 4
                boolean tf = true;
                for (int j = 1; j <= n; j++) {
                    curr_y--;
                    if (curr_y  < 0 || park[curr_y].charAt(curr_x) == 'X') {
                        tf = false;
                        break;
                    }
                }
                if (tf) y = curr_y;
            } else if (op.equals("S")) {
                boolean tf = true;
                for (int j = 1; j <= n; j++) {
                    curr_y++;
                    if (curr_y >= park.length || park[curr_y].charAt(curr_x) == 'X') {
                        tf = false;
                        break;
                    }
                }
                if (tf) y = curr_y;
            } else if (op.equals("W")) {
                boolean tf = true;
                for (int j = 1; j <= n; j++) {
                    curr_x--;
                    if (curr_x < 0 || park[curr_y].charAt(curr_x) == 'X') {
                        tf = false;
                        break;
                    }
                }
                if (tf) x = curr_x;
            } else if (op.equals("E")) {
                boolean tf = true;
                for (int j = 1; j <= n; j++) {
                    curr_x++;
                    if (curr_x >= park[0].length() || park[curr_y].charAt(curr_x) == 'X') {
                        tf = false;
                        break;
                    }
                }
                if (tf) x = curr_x;
            }
        }

        answer[0] = y;
        answer[1] = x;
        
        return answer;
    }
}

과정

  1. answer은 y와 x를 리턴하니까 크기를 2로 지정하고, x와 y를 선언
  2. String배열 park를 순회하며 시작점이 되는 'S'를 찾고, x, y에 해당하는 j, i를 넣는다
  3. String배열 routes에서 가야할 방향 op와 가야할 거리 n을 찾고, x와 y를 움직여볼 현재 위치 curr_x와 curr_y를 선언
  4. 동, 서, 남, 북에 해당하는 E, W, S, N중 해당하는 것을 찾고, 조건이 맞는지 검사해줄 boolean tf를 선언. 해당하는 방향으로 n만큼 반복하며 curr_y와 curr_x를 증감시켜준다. 만약 증감시킨 값이 공원을 벗어나거나 위치한 값이 'X'이면 tf를 false. 반복문이 끝났는데 tf가 true라면 curr_y, curr_x를 각각 y, x에 대입해준다

다른 사람 풀이

class Solution {
    public static int[] solution(String[] park, String[] routes) {
        int x = 0;
        int y = 0;
        int[][] arr = {{-1,0},{1,0},{0,-1},{0,1}};
        boolean start = false;
        for(int i=0; i< park.length;i++){
            if(start){
                break;
            }
            for(int j=0; j<park[i].length();j++){
                if(park[i].charAt(j) == 'S'){
                    x = i;
                    y = j;
                    start = true;
                    break;
                }
            }
        }

        for (String route : routes) {
            String[] routeArr = route.split(" ");
            String direction = routeArr[0];
            int distance = Integer.parseInt(routeArr[1]);

            int index = getDirectionIndex(direction);

            if(isWalk(park, x, y, distance, arr[index])){
                x += distance*arr[index][0];
                y += distance*arr[index][1];
            }
        }

        return new int[]{x,y};
    }

    private static boolean isWalk(String[] park, int x, int y, int distance, int[] arr) {
        for(int i = 0; i< distance; i++){
            x+= arr[0];
            y+= arr[1];

            if(y<0 || y> park[0].length()-1 || x<0 || x> park.length-1 || park[x].charAt(y)=='X'){
                return false;
            }
        }
        return true;
    }

    private static int getDirectionIndex(String direction) {
        int index = 0;
        switch (direction){
            case "N":
                break;
            case "S":
                index = 1;
                break;
            case "W":
                index = 2;
                break;
            case "E":
                index = 3;
                break;
        }
        return index;
    }
}
  • 코드를 메소드화 시켜서 긴 코드보단 가독성이 좋아보일수도 있다

0개의 댓글