[Java] 백준 20055번 컨베이어 벨트 위의 로봇

2022. 4. 25. 02:03
728x90

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

 

20055번: 컨베이어 벨트 위의 로봇

길이가 N인 컨베이어 벨트가 있고, 길이가 2N인 벨트가 이 컨베이어 벨트를 위아래로 감싸며 돌고 있다. 벨트는 길이 1 간격으로 2N개의 칸으로 나뉘어져 있으며, 각 칸에는 아래 그림과 같이 1부

www.acmicpc.net

문제


 

코드 리뷰


이번 문제는 시뮬레이션 문제입니다.

저는 개인적으로 정말 화나는 문제였습니다. (물론 저의 실수가 크지만 ㅋㅋ)

 

문제 이해가 너무 헷갈립니다.

올리고 내리고를 개인적으로 추가, 삭제 느낌으로 만들었다면 어떨까 싶네요 그림이 저렇게 2층으로 구성되있는 느낌이다 보니 올리고 내리고 이부분에서 정말 헷갈렸습니다. 헛짓거리도 좀 하구요ㅠㅠ

그래도.. 이해 못한 제 잘못이기때문에 어쩔 수 없죠

 

그 다음 저는 처음에 LinkedList로 풀었습니다. 하지만 시간 초과~~~~~ 멘탈이 더 나가더군요.. 아마 값 수정에서 시간을 많이 잡아 먹은게 아닌가 싶었습니다.

이유는 모릅니다. 실력이 부족합니다.

 

아무튼 이번 문제는 문제에서 답을 다 알려줍니다. 내릴 곳에 내리고 올릴 곳에 올리고 벨트 이동할 때 로봇과 같이 이동시켜주고 로봇만 따로 이동시켜주는데 내구도 체크하면서 이동시켜주면 풀리는 문제입니다.

 

이번에는 코드 2개를 올리겠습니다.

LinkedList 시간 왜케 잡아먹냐 이말입니다. ㅠㅠ

 

LinkedList(시간 초과)

package BJ;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class BJ_컨베이어벨트위의로봇_linkedlist {
	static int N, K;
	static LinkedList<Node> belt;
	static int index;
	static int check;

	static class Node {
		int durability;
		boolean robot;

		public Node(int durability, boolean robot) {
			super();
			this.durability = durability;
			this.robot = robot;
		}		
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(br.readLine());

		N = Integer.parseInt(st.nextToken());
		K = Integer.parseInt(st.nextToken());
		
		belt = new LinkedList<>();
		st = new StringTokenizer(br.readLine());

		for (int i = 0; i < 2 * N; i++) {
			int a = Integer.parseInt(st.nextToken());
			belt.add(new Node(a, false));
		}
		bw.write(conveyor_belt() + "\n");
		
		br.close();
		bw.flush();
		bw.close();
	}

	private static int conveyor_belt() {
		while(isOk()) {
			rotate(); // 벨트 회전
			move(); // 로봇이 움직임
			add_robot(); // 로봇 올림
			index++;
		}
		
		return index;
	}

	private static boolean isOk() { // 내구도 0 개수 체크
		check = 0;
		for (int i = 0; i < belt.size(); i++) {
			if(belt.get(i).durability == 0) {
				check++;
			}
		}
		
		if(check >= K) {
			return false;
		}
		return true;
	}

	private static void move() {
//		Node node;
		for (int i = N - 1; i > 0; i--) { // n-1 위치에서 로봇은 내려진다. 먼저 벨트위에 올라간 로봇은 N - 1 부터 찾았을 때 가장 먼저 나온 로봇이다.
			if(belt.get(i - 1).robot && belt.get(i).durability > 0 && !belt.get(i).robot) {
//				node = new Node(belt.get(i).durability - 1, true);
				belt.set(i, new Node(belt.get(i).durability - 1, true)); // 로봇이 움직임
				
//				node = new Node(belt.get(i - 1).durability, false);
				belt.set(i - 1, new Node(belt.get(i - 1).durability, false)); // 로봇이 이전에 있던 곳 수정
				
			}
		}
		if(belt.get(N - 1).robot) { // 로봇 내리기
//			node = new Node(belt.get(N - 1).durability, false);
			belt.set(N - 1, new Node(belt.get(N - 1).durability, false));
		}
	}

	private static void add_robot() {
		if (belt.get(0).durability > 0) { // 로봇 추가
			belt.set(0, new Node(belt.get(0).durability - 1, true));
		}
	}

	private static void rotate() {
		Node node;
		
		node = belt.pollLast(); // 벨트의 마지막 값을
		belt.addFirst(node); // 벨트의 첫번째 값으로 가지고 옴
		
		if(belt.get(N - 1).robot) { // 로봇 내리기
//			node = new Node(belt.get(N - 1).durability, false);
			belt.set(N - 1, new Node(belt.get(N - 1).durability, false));
		}
		
	}

}

 

array (정답)

package BJ;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class BJ_컨베이어벨트위의로봇_array {
	public static int N;
    public static int K;
    public static int[] A;
    public static boolean[] robot;
    
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        A = new int[2 * N];
        robot = new boolean[N];

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

        bw.write(conveyor_belt(0) + "\n");

        br.close();
        bw.flush();
        bw.close();

	}

	private static int conveyor_belt(int cnt) {
		while (isOK()) {
            int temp = A[A.length - 1]; // 1. 벨트 한 칸 회전
            for (int i = A.length - 1; i > 0; i--) {
                A[i] = A[i - 1];
            }
            A[0] = temp;

            for (int i = robot.length - 1; i > 0; i--) {    // 로봇도 벨트와 같이 회전
                robot[i] = robot[i - 1];
            }
            robot[0] = false;

            robot[N - 1] = false;
            for (int i = N - 1; i > 0; i--) {   // 2. 로봇 이동가능하면 이동
                if (robot[i - 1] && !robot[i] && A[i] >= 1) {
                    robot[i] = true;
                    robot[i - 1] = false;
                    A[i]--;
                }
            }

            if (A[0] > 0) {     // 3. 올라가는 위치에 로봇 올리기
                robot[0] = true;
                A[0]--;
            }
            
            cnt++;
        }

        return cnt;
	}
	 public static boolean isOK() {
	        int cnt = 0;

	        for (int i = 0; i < A.length; i++) {
	            if (A[i] == 0) {
	                cnt++;
	            }
	            if (cnt >= K) {
	                return false;
	            }
	        }
	        return true;
	    }

}

 

Ysik Github : https://github.com/Y1sik/Algorithm/blob/main/BJ/BJ_%EC%BB%A8%EB%B2%A0%EC%9D%B4%EC%96%B4%EB%B2%A8%ED%8A%B8%EC%9C%84%EC%9D%98%EB%A1%9C%EB%B4%87_array.java

반응형

BELATED ARTICLES

more