CodingTest/Java로 푼 코딩 테스트

[Java / 코딩테스트] 청소 문제

마볼링 2023. 10. 6. 18:33

 

1.  생각

  • 일단 k번 반복해야 한다. -> for(k)
    • 초기위치 (0,0)
    • 방향
      • 0 : 오른쪽
      • 1 : 아래
      • 2 : 왼쪽
      • 3 : 위
  • 조건에 따라 이동

 

2.  생각대로 코딩

[ 코드 ]

public int[] solution(int[][] board, int k){
        int[] answer = new int[2];
        //초기화
        answer[0] = 0;
        answer[1] = 0;
        int direction = 0;
        for (int i = 0; i < k; i++) {
            boolean change = false; //방향전화 변수
            // 넘어가면 방향 전환
            if (direction == 0 && answer[1] == board.length-1) {change = true;}
            if (direction == 1 && answer[0] == board.length-1) {change = true;}
            if (direction == 2 && answer[1] == 0) {change = true;}
            if (direction == 3 && answer[0] ==0 ) {change = true;}

            // 이동
            if(!change) {
                if (direction == 0 && answer[1] != board.length-1) {
                    if(board[answer[0]][answer[1]+1] == 0) {
                        answer[1]++;
                    } else {
                        change = true;
                    }
                }
                if (direction == 1 && answer[0] != board.length-1) {
                    if(board[answer[0]+1][answer[1]] == 0) {
                        answer[0]++;
                    } else {
                        change = true;
                    }
                }
                if (direction == 2 && answer[1] != 0) {
                    if(board[answer[0]][answer[1]-1] == 0) {
                        answer[1]--;
                    } else {
                        change = true;
                    }
                }
                if (direction == 3 && answer[0] !=0 ) {
                    if(board[answer[0]-1][answer[1]] == 0) {
                        answer[0]--;
                    } else {
                        change = true;
                    }
                }
            }

            // 방향전환
            if(change) {
                if(direction<=2) {
                    direction++;
                } else {
                    direction=0;
                }
            }
        }
        return answer;
    }
    
    public static void main(String[] args){
        Problem02 T = new Problem02();
        int[][] arr1 = {{0, 0, 0, 0, 0},
                {0, 1, 1, 0, 0},
                {0, 0, 0, 0, 0},
                {1, 0, 1, 0, 1},
                {0, 0, 0, 0, 0}};
        System.out.println(Arrays.toString(T.solution(arr1, 10)));
        int[][] arr2 = {{0, 0, 0, 1, 0, 1},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 1},
                {1, 1, 0, 0, 1, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0}};
        System.out.println(Arrays.toString(T.solution(arr2, 20)));
        int[][] arr3 = {{0, 0, 1, 0, 0},
                {0, 1, 0, 0, 0},
                {0, 0, 0, 0, 0},
                {1, 0, 0, 0, 1},
                {0, 0, 0, 0, 0}};
        System.out.println(Arrays.toString(T.solution(arr3, 25)));

    }

[ 결과 ]

[2, 2]
[4, 5]
[0, 1]

 

3. 강의

[ 코드 ]

public int[] solution2(int[][] board, int k){
        int[] answer = new int[2];
        int n = board.length;
        int[] dx = {-1, 0, 1, 0};
        int[] dy = {0, 1, 0, -1};
        int x = 0, y = 0, d = 1, count = 0;
        while(count < k){
            count++;
            int nx = x + dx[d];
            int ny = y + dy[d];
            if(nx < 0 || nx >= n || ny < 0 || ny >= n || board[nx][ny] == 1){
                d = (d + 1) % 4;
                continue;
            }
            x = nx;
            y = ny;
        }
        answer[0] = x;
        answer[1] = y;
        return answer;
    }

[ 차이 ]

  • 방향을 배열로 둠
  • 나머지를 활용한 방향전환