[Algorithm] 백준_컨베이어 벨트 위의 로봇_20055번 (JAVA) 🤖

2025. 4. 10. 15:25·Algorithm

📝문제

https://www.acmicpc.net/problem/20055

티어: 골드5

 

💡풀이

문제 유형

구현, 시뮬레이션

 

걸린 시간

세번째 시돈데 60분 걸렸다

 

풀이 방법 도출(주석 참조)

import java.util.*;
import java.io.*;
class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int len = N*2;

        //1)벨트 배열, 로봇 위치 배열 선언
        int[] belt = new int[len];
        boolean[] isRobot = new boolean[len];

        st = new StringTokenizer(br.readLine());
        for(int i = 0; i < len; i++){
            belt[i] = Integer.parseInt(st.nextToken());
        }

        int level = 0;
        int count = 0;
        //9) 내구도가 0인 칸의 개수가 K개 이상이라면 종료
        while(count < K){
            level++;
            //2) 벨트가 회전한다.
            int tempBelt = belt[len-1];
            for(int i = len-1; i >= 1; i--){
                belt[i] = belt[i-1];
            }
            belt[0] = tempBelt;

            //3) 로봇이 회전한다.
            boolean tempRobot = isRobot[len-1];
            for(int i = len-1; i >= 1; i--){
                isRobot[i] = isRobot[i-1];
            }
            isRobot[0] = tempRobot;

            //4) 언제든지 로봇이 내리는 위치에 도달하면 그 즉시 내린다.
            isRobot[N-1] = false;

            //5) 로봇 이동 1 (현재 위치에 로봇이 있는 경우에만)
            // 다음칸 내구도가 0 이상이면서 다음칸에 로봇이 없는 경우 이동 가능
            // 현재 위치에 로봇이 있다면 패스
            // N-1번째에서 로봇이 내리기 때문에 N-2번째부터 검사를 한다.
            for(int i = N-2; i >= 0; i--){
                if(!isRobot[i]) continue;
                if(belt[i+1] > 0 && !isRobot[i+1]){
                    isRobot[i+1] = true;
                    belt[i+1]--;
                    isRobot[i] = false;
                }
            }
            
            //5-1) 로봇 이동 2
            for(int i = len-2; i>= N; i--){
                if(!isRobot[i]) continue;
                if(belt[i+1] > 0 && !isRobot[i+1]){
                    isRobot[i+1] = true;
                    belt[i+1]--;
                    isRobot[i] = false;
                }
            }

            //5-2) 로봇 이동 3
            if (isRobot[len-1] && belt[0] > 0 && !isRobot[0]) {
                isRobot[0] = true;
                belt[0]--;
                isRobot[len-1] = false;
            }

            //6) 언제든지 로봇이 내리는 위치에 도달하면 그 즉시 내림
            isRobot[N-1] = false;

            //7) 올리는 위치에 있는 칸의 내구도가 0이 아니면 올리는 위치에 로봇을 올린다.
            if(belt[0] > 0 && !isRobot[0]){
                belt[0]--;
                isRobot[0] = true;
            }

            //8) 내구도가 0인 칸의 개수 구하기
            count = 0;
            for(int b : belt){
                if(b == 0) count++;
            }

        }
        System.out.println(level);
    }
}

 

🤔Review

정말..이해가 안 됐던^^ 문제 

N번 칸이 있는 위치를 "내리는 위치"임을 놓치지말자

(난 2N-1번 칸에서 내리고 왜 틀린지 gpt한테 물어봤다 ^^ㅎㅎ babo)

'Algorithm' 카테고리의 다른 글

[Algorithm] 백준_뱀과 사다리 게임_16928번 (JAVA) 🐍  (0) 2025.04.14
[Algorithm] 백준_마법사 상어와 파이어볼_20056번 (JAVA) 🔥  (6) 2025.04.11
[Algorithm] 백준_마법사 상어와 비바라기_21610번 (JAVA)🪄  (1) 2025.04.09
[Algorithm] 백준_상어 초등학교_21608번 (JAVA) 🏫  (1) 2025.04.09
[Algorithm] 백준_청소년 상어_19236번 (JAVA) 🐟  (0) 2025.04.09
'Algorithm' 카테고리의 다른 글
  • [Algorithm] 백준_뱀과 사다리 게임_16928번 (JAVA) 🐍
  • [Algorithm] 백준_마법사 상어와 파이어볼_20056번 (JAVA) 🔥
  • [Algorithm] 백준_마법사 상어와 비바라기_21610번 (JAVA)🪄
  • [Algorithm] 백준_상어 초등학교_21608번 (JAVA) 🏫
dev_ajrqkq
dev_ajrqkq
알고리즘 천재가 될 거야
  • dev_ajrqkq
    기록이 자산이다
    dev_ajrqkq
  • 전체
    오늘
    어제
    • 분류 전체보기 (147)
      • Front-end (0)
      • Back-end (11)
        • Spring (1)
        • Java (8)
      • CS (9)
        • 데이터베이스 (5)
        • 네트워크 (4)
      • Algorithm (80)
      • 이것저것 (0)
      • 버그잡기 (1)
      • TIL (37)
      • 후기 (1)
      • 취준 (0)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      TypeScript
      오블완
      99클럽
      Til
      코딩테스트준비
      티스토리챌린지
      항해99
      개발자취업
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.2
    dev_ajrqkq
    [Algorithm] 백준_컨베이어 벨트 위의 로봇_20055번 (JAVA) 🤖
    상단으로

    티스토리툴바