[시뮬레이션 & 구현] 청소

    [문제]

     

     

    [처음 생각한 것]

    import java.util.*;
    class Solution {
        public int[] solution(int[][] board, int k){
            int[] answer = new int[2];
    
            for(int i = 0 ; i < k ; i++) {
                for(int[] line : board) {
                    for(int k : line) {
                    	if(answer[i] == 1 || answer[i] == board.length) {
                        
    					}
                    }
                }
            }
    
            return answer;
        }
    
        public static void main(String[] args){
            Solution T = new Solution();
            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)));
    
        }
    }

    일단 k초 만큼 주어지기 때문에 k값안에서 for문이 동작해야하고

    board 배열의 각 가로줄에서 마지막 원소이거나 원소의 값이 1인 경우 회전해야 한다는 것을 생각하였다...

    그리고 격자 밖이거나 장애물인 경우를 생각하기 위해 if 문을 넣었다

    너무너무 어렵다 ㅜㅜ

     

    [강의를 듣고 풀어본 것]

    import java.util.*;
    class Solution {
        public int[] solution(int[][] board, int k){
            int[] answer = new int[2];
            int count = 0;
    
            // 이동방향을 정하기 위한 배열
            int[] dx = {-1, 0, 1, 0};
            int[] dy = {0, 1, 0, -1};
    
            // 위의 배열의 인덱스
            int d = 1;
    
            // 청소 로봇의 위치 저장 (행, 열)
            int x = 0, y = 0;
    
           while(count < k) {
               int nx = 0, ny = 0;
    
               nx = x + dx[d];
               ny = y + dy[d];
    
               if(nx == board.length - 1 || ny == board.length - 1 || Arrays.asList(board).contains(1)) {
                    d = (d+1)%4;
                    continue;
               }
               x = nx;
               y = ny;
    
               answer[0] = x;
               answer[1] = y;
           }
    
    
            return answer;
        }
    
        public static void main(String[] args){
            Solution T = new Solution();
            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)));
    
        }
    }

    강의를 듣고 풀어봤는데도 실행이 되지 않았다

    음 내 생각에는 격자 밖이거나 장애물이 있는 경우를 찾아내는 if문이 잘못된 것 같다.

     

    [정답 코드]

    import java.util.*;
    class Solution {
        public int[] solution(int[][] board, int k){
            int[] answer = new int[2];
            int count = 0;
            
            // 추가코드
            int n = board.length;
            
            // 이동방향을 정하기 위한 배열
            int[] dx = {-1, 0, 1, 0};
            int[] dy = {0, 1, 0, -1};
    
            // 위의 배열의 인덱스
            int d = 1;
    
            // 청소 로봇의 위치 저장 (행, 열)
            int x = 0, y = 0;
    
           while(count < k) {
               // 추가 코드
               count++;
               int nx = 0, ny = 0;
    
               nx = x + dx[d];
               ny = y + dy[d];
    
               //if(nx == board.length - 1 || ny == board.length - 1 || Arrays.asList(board).contains(1)) {
               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;
           }
           answer[0] = x;
           answer[1] = y;
           return answer;
        }
    
        public static void main(String[] args){
            Solution T = new Solution();
            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)));
    
        }
    }

    nx, ny 의 값이 0보다 작은 경우는 생각하지 않았었고,

    count의 값을 증가시켜주지 않아서 무한 루프가 돌았을 것 같다

    비슷하지만 틀려서 아쉽다

    처음엔 어렵다는 생각 뿐이었는데 재미도 있는 것 같다.

    꾸준히 연습해야겠다

    댓글