[시뮬레이션 & 구현] 좌석번호

    [문제]

     

    [내가 푼 것]

    import java.util.*;
    class Solution4 {
        public int[] solution(int c, int r, int k){
            int[] answer = new int[2];
            int[][] seat = {};
    
            // 배열 초기화
            for(int i = 0; i < c; i++) {
                for(int j = 0; j < r; j++) {
                    seat[i][j] = 0;
                }
            }
    
            // 이동방향 정하는 배열
            int dx[] = {-1, 0, 1, 0};
            int dy[] = {0, 1, 0, -1};
    
            int d = 1;
    
            int x = 0, y = 0;
    
            int count = 0;
    
            while(count < k) {
                count++;
                int nx = 0, ny = 0;
    
                nx = x + dx[d];
                ny = y + dy[d];
    
    
                if(nx < 0 || nx >= seat.length - 1 || ny < 0 || ny >= seat.length || seat[nx][ny] != 0) {
                    d = (d + 1) % 4;
                    count = count - 1;
                }
                x = nx;
                y = ny;
            }
    
            answer[0] = x;
            answer[1] = y;
            return answer;
        }
    
        public static void main(String[] args){
            Solution4 T = new Solution4();
            System.out.println(Arrays.toString(T.solution(6, 5, 12)));
            System.out.println(Arrays.toString(T.solution(6, 5, 20)));
            System.out.println(Arrays.toString(T.solution(6, 5, 30)));
            System.out.println(Arrays.toString(T.solution(6, 5, 31)));
        }
    }

    빠졌다고 생각하는 부분은 지나간 자리의 배열 숫자를 안 바꿔 준 것 

     

    [정답 코드]

    import java.util.*;
    class Solution4 {
        public int[] solution(int c, int r, int k){
            int[] answer = new int[2];
    //        int[][] seat = {};
            if(k > c * r) return new int[] {0,0};
            // 90도 회전해서 생각했기 때문에 [c][r]
            int[][] seat = new int[c][r];
    
            // 배열 초기화
    //        for(int i = 0; i < c; i++) {
    //            for(int j = 0; j < r; j++) {
    //                seat[i][j] = 0;
    //            }
    //        }
    
            // 이동방향 정하는 배열
            int dx[] = {-1, 0, 1, 0};
            int dy[] = {0, 1, 0, -1};
    
            // 배열 인덱스 3시 방향
            int d = 1;
    
            int x = 0, y = 0;
    
            int count = 1;
    
            while(count < k) {
                // count++;
                int nx = x + dx[d];
                int ny = y + dy[d];
    
                // 좌석 밖이거나 이미 사람이 있는 좌석이면 회전하고 count는 세지 않음
                if(nx < 0 || nx >= c || ny < 0 || ny >= r || seat[nx][ny] > 0) {
                // if(nx < 0 || nx >= seat.length - 1 || ny < 0 || ny >= seat.length || seat[nx][ny] != 0) {
                    d = (d + 1) % 4;
                    // count = count - 1;
                    continue;
                }
                // 0이었던 좌석을 사람이 앉으면 숫자로 바꿔줌
                seat[x][y] = count;
                count++;
                x = nx;
                y = ny;
            }
    
            // 0,0으로 시작했기 때문에 +1
            answer[0] = x + 1;
            answer[1] = y + 1;
            return answer;
        }
    
        public static void main(String[] args){
            Solution4 T = new Solution4();
            System.out.println(Arrays.toString(T.solution(6, 5, 12)));
            System.out.println(Arrays.toString(T.solution(6, 5, 20)));
            System.out.println(Arrays.toString(T.solution(6, 5, 30)));
            System.out.println(Arrays.toString(T.solution(6, 5, 31)));
        }
    }

    생각 안 했던 것

     

    k번째 온 사람이 좌석 수보다 큰 경우

    사람 있는 좌석을 숫자로 안 바꿈

    댓글