Skip to content

지게차와 크레인 - #84

Open
yangyeeeun wants to merge 1 commit into
mainfrom
yee/forklift_crane
Open

지게차와 크레인#84
yangyeeeun wants to merge 1 commit into
mainfrom
yee/forklift_crane

Conversation

@yangyeeeun

Copy link
Copy Markdown
Collaborator

🍪 문제 이름

Resolve: 지게차와 크레인


🍊 문제 정의

input

  • participant : String[] - 참가자 이름 목록
  • completion : String[] - 완주자 이름 목록

output

  • String - 완주하지 못한 선수의 이름

[입출력 예시]

  1. participant = ["leo", "kiki", "eden"]
    completion = ["eden", "kiki"]
    return = "leo"

  2. participant = ["marina", "josipa", "nikola", "vinko", "filipa"]
    completion = ["josipa", "filipa", "marina", "nikola"]
    return = "vinko"

  3. participant = ["mislav", "stanko", "mislav", "ana"]
    completion = ["stanko", "ana", "mislav"]
    return = "mislav"


🍑 알고리즘 설계

풀이 과정에서 본인이 생각한 내용을 작성해 주세요.
지게차일 때만 bfs를 사용해서 방문여부를 체크하고 빈공간이 0인 부분으로 퍼져나가도록 설계했습니다.

# 특히 리뷰받고 싶은 코드 일부를 여기에 작성해 주세요.
import java.util.*;

class Solution {
    char[][] grid;
    boolean[][] visited;
    int n, m;
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    public int solution(String[] storage, String[] requests) {
        n = storage.length;
        m = storage[0].length();
        // 여백 포함 grid 초기화 (n+2, m+2), 테두리는 '0'
        grid = new char[n + 2][m + 2];
        for (char[] row : grid) Arrays.fill(row, '0');
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                grid[i+1][j+1]=storage[i].charAt(j);
            }
        }

        for (String req : requests) {
            if (req.length() == 1) {
                forklift(req.charAt(0));
            } else {
                crane(req.charAt(0));
            }
        }

        int answer = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (grid[i][j] != '0') answer++;

        return answer;
    }

    public void bfs() {
        visited = new boolean[n + 2][m + 2];
        Queue<int[]>queue = new ArrayDeque<>();
        queue.add(new int[]{0,0}); //외부취급 모서리
        visited[0][0] = true;
        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int x = cur[0], y = cur[1];

            for (int dir = 0; dir < 4; dir++) {
                int nx = x + dx[dir];
                int ny = y + dy[dir];

                // 범위 체크, 이미 방문했는지 체크
                if (nx < 0 || nx >= n+2 || ny < 0 || ny >= m+2)
                    continue;
                if (visited[nx][ny])
                    continue;

                // 빈 공간('0')만 타고 퍼져나감
                if (grid[nx][ny] != '0')
                    continue;

                visited[nx][ny] = true;
                queue.add(new int[]{nx, ny});
            }
        }
    }

    public void forklift(char type) {
        bfs();// 먼저 외부 연결 상태 갱신
         List<int[]> toRemove = new ArrayList<>();

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(grid[i][j]!=type)
                    continue;
                for (int dir = 0; dir < 4; dir++) {
                    int nx = i + dx[dir];
                    int ny = j + dy[dir];
                    if (visited[nx][ny]) {
                        toRemove.add(new int[]{i, j});
                        break;
                    }
                }
            }
        }
        // 모아둔 위치들 한꺼번에 제거
        for (int[] pos : toRemove) {
            grid[pos[0]][pos[1]] = '0';
        }
    }

    public void crane(char type) {
        //grid를 훑으면서 type인 칸을 전부 '0'으로 변경
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(grid[i][j]==type){
                    grid[i][j]='0';
                }
            }
        }
    }
}

🥝 최악 수행 시간 복잡도

  • O()

🍰 특이 사항 (Optional)

참고 자료나 추가로 설명하고 싶은 내용을 작성해 주세요.

@yangyeeeun yangyeeeun self-assigned this Jul 14, 2026
@yangyeeeun yangyeeeun linked an issue Jul 14, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PGS] 지게차와 크레인 / level2

1 participant