Post

[2025 프로그래머스 코드챌린지 1차 예선] 지게차와 크레인

[2025 프로그래머스 코드챌린지 1차 예선] 지게차와 크레인

🙋‍♂️ 들어가며

이번 문제는 크레인과 지게차의 진입 방식을 이해하면 충분히 잘 풀 수 있는 문제였다.

크레인의 경우 완전탐색을 통해 요청 작업을 처리하면 된다.

지게차의 경우 먼저 테두리에서 들어갈 공간이 있나 없나 탐색한 이후 빈공간을 탐색할 q와 제거할 공간의 lst를 만들어주면 되겠다.

이렇게 생각하면 되겠네

1
2
3
4
5
6
7
8
/*
-------------fork_lift-------------
접근 가능한 외부에서 테두리를 먼저 탐색하고 bfs 진행
q(이동 가능한 통로), to_remove(삭제할 컨테이너)

-------------crane-------------
전부 탐색
*/

코드는 다음과 같다



✅ 정답

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 테두리에서 시작하여 이동 가능한 통로들
import java.util.Queue;
import java.util.LinkedList;

// 제거할 컨테이너들
import java.util.List;
import java.util.ArrayList;



class Solution {
    static int[] dr = {-1,1,0,0};
    static int[] dc = {0,0,-1,1};
    static char[][] arr;
    static boolean[][] visited;
    static int row, col;
    
    
    public int solution(String[] storage, String[] requests) {
        row = storage.length;
        col = storage[0].length();
        arr = new char[row][col];
        
        // 1. 초기 격자 생성
        for (int r = 0; r < row; r++) {
            arr[r] = storage[r].toCharArray();
        }
        
        
        // 2. 작업을 요청 순서에 따라 진행
        int i = 0;
        int N = requests.length;
        while (i < N) {
            String cur_req = requests[i];
            
            // 2-1. 길이가 2라면 crane
            if (cur_req.length() >= 2) {
                remove_with_crane(cur_req.charAt(0));
            }
            // 2-2. 길이가 1이면 -> fork_lift
            else if (cur_req.length() < 2) {
                remove_with_fork_lift(cur_req.charAt(0));
            }
            
            // 다음으로 전환
            i++;
        }
        
        
        // 3. 제거된 컨테이너 제외하고 합산
        int cnt = 0;
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                if (arr[r][c] != '.') cnt++;
            }
        }
        
        return cnt;
    }
    
    
    
    
    // crane
    static void remove_with_crane(char target) {
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                if (arr[r][c] == target) {
                    arr[r][c] = '.';
                }
            }
        }
    }
    
    
    // fork_lift
    static void remove_with_fork_lift(char target) {
        visited = new boolean[row][col];
        
        Queue<int[]> q = new LinkedList<>();
        List<int[]> to_remove = new ArrayList<>();
        
        // a. 외부에서 접근하려면 테두리에서 먼저 찾자
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                if (r == 0 || r == row-1 || c == 0 || c == col-1) {
                    // a-1. 이동 가능한 빈 공간이라면
                    if (arr[r][c] == '.') {
                        q.add(new int[] {r, c});
                        visited[r][c] = true;
                    }
                    // a-2. 그렇지 않고 현재 작업중인 컨테이너와 일치시
                    else if (arr[r][c] == target) {
                        to_remove.add(new int[] {r, c});
                        visited[r][c] = true;
                    }
                }
            }
        }
        
        // b. bfs
        bfs(target, q, to_remove);
        
        // c. 삭제할 컨테이너 처리
        for (int[] pos : to_remove) {
            int r = pos[0];
            int c = pos[1];
            arr[r][c] = '.';
        }
        
    }
    
    
    
    
    // bfs
    static void bfs(char target, Queue<int[]> q, List<int[]> to_remove) {
        while (!q.isEmpty()) {
            int[] cur_pos = q.poll();
            int cr = cur_pos[0];
            int cc = cur_pos[1];
            for (int d = 0; d < 4; d++) {
                int nr = cr + dr[d];
                int nc = cc + dc[d];
                
                // 만약 범위 밖이면 skip
                if (nr < 0 || nr >= row || nc < 0 || nc >= col) continue;
                
                // 방문했다면 skip
                if (visited[nr][nc]) continue;
                
                // 이동 가능한 빈 공간이라면
                if (arr[nr][nc] == '.') {
                    visited[nr][nc] = true;
                    q.add(new int[] {nr, nc});
                }
                // 현재 제거 중인 컨테이너와 일치한다면
                else if (arr[nr][nc] == target) {
                    visited[nr][nc] = true;
                    to_remove.add(new int[] {nr, nc});
                }
                
            }
        }

    }
    
    
}
This post is licensed under CC BY 4.0 by the author.
3D GIF

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?