티스토리 뷰

1. 문제

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

 

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

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

www.acmicpc.net


 

2. 문제 풀이 겸 회고

문제 이해가 제일 문제임... ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ

나는 진짜 바보같이 2N위치에서 로봇을 내려주고 있었음..ㅎ...

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 진짜.... 로직은 맞았는데 답이 계속 안 나오길래.. 열이 너무 받았지만 결국은 solve.. ㅎ

로직은 생각보다 간단!!

난 삼성문제가 좋더라... ㅠㅠㅠㅠ 그냥 그대로 구현하면 되니깐 (?)...

또한, 리스트랑 배열이랑 둘 다 구현해봤음

편하기는 List이지만 시간 측면은 배열 승

그리고 삼성 문제는 특히, 메서드로 나눠서 구현해주면 디버깅도 금방 할 수 있고 안 헷갈린다.

 

조건대로 그대로 구현해줬음

  1. 벨트가 각 칸 위에 있는 로봇과 함께 한 칸 회전한다. 
  2. 가장 먼저 벨트에 올라간 로봇부터, 벨트가 회전하는 방향으로 한 칸 이동할 수 있다면 이동한다. 만약 이동할 수 없다면 가만히 있는다.
    1. 로봇이 이동하기 위해서는 이동하려는 칸에 로봇이 없으며, 그 칸의 내구도가 1 이상 남아 있어야 한다.
  3. 올리는 위치에 있는 칸의 내구도가 0이 아니면 올리는 위치에 로봇을 올린다.
  4. 내구도가 0인 칸의 개수가 K개 이상이라면 과정을 종료한다. 그렇지 않다면 1번으로 돌아간다.

 

로봇은 boolean으로 관리, 내구도는 int로 관리해줌

-주의사항-

1. 내구도 잘 깎아주기 (깎아주는거 까먹지 말긔)

2. (1), (2) 과정에서 N-1 위치에 로봇이 있으면 내려주기 (index가 0 기준으로)

 

 

3. 코드

(1) 배열

package net.acmicpc.samsung;

import java.io.*;
import java.util.*;


// 배열로 푸는게 더 빠름.
public class Solution_20055_컨베이어벨트위의로봇_배열ver {

	static int[] duArr;
	static boolean[] robot;

	static int N, K;
	static int answer;

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

		N = Integer.parseInt(st.nextToken()); // 벨트의 총길이
		K = Integer.parseInt(st.nextToken()); // 내구도가 0인가 K개 이상일경우 종료

		duArr = new int[N * 2]; // 내구도
		robot = new boolean[N]; // 로봇
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < 2 * N; i++) {

			duArr[i] = Integer.parseInt(st.nextToken());

		}

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

	// 내리는 위치는 2N이 아니라 N이야...

	private static int simulation() {
		int answer = 0;
		e: while (true) {
			++answer;

			move();
			moveRobot();
			putRobot();

			int idx = 0;
			for (int i = 0; i < N * 2; i++) {
				if (duArr[i] == 0) {
					idx++;
				}
				if (idx == K)
					break e;
			}
		}
		
		return answer;
	}

	private static void move() {
		int tmp = duArr[(2 * N) - 1];
		for (int i = (N * 2) - 1; i > 0; i--) {
			duArr[i] = duArr[i - 1];
		}
		
		duArr[0] = tmp;
		for (int i = (N - 1); i > 0; i--) {
			robot[i] = robot[i - 1];
		}

		robot[0] = false;
		robot[N - 1] = false;
	}

	private static void moveRobot() {
		for (int i = N - 2; i >= 0; i--) {
			if (robot[i] && !robot[i + 1] && duArr[i + 1] >= 1) {
				robot[i + 1] = true;
				robot[i] = false;
				duArr[i + 1] -= 1;
			}
		}

		if (robot[N - 1]) {
			robot[N - 1] = false;
		}
	}

	private static void putRobot() {
		if (duArr[0] >= 1) {
			robot[0] = true;
			duArr[0] -= 1;
		}
	}
	
}

 

 

(2) 리스트

package net.acmicpc.samsung;

import java.io.*;
import java.util.*;

public class Solution_20055_컨베이어벨트위의로봇 {

	static List<Integer> duList;
	static List<Boolean> robot;

	static int N, K;
	static int answer;

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

		N = Integer.parseInt(st.nextToken()); // 벨트의 총길이
		K = Integer.parseInt(st.nextToken()); // 내구도가 0인가 K개 이상일경우 종료

		duList = new ArrayList<>(); // 내구도
		robot = new ArrayList<>(); // 로봇
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < 2 * N; i++) {
			if (i < N) {
				robot.add(false);
			}
			duList.add(Integer.parseInt(st.nextToken()));
		}
		System.out.println(simulation());
	}

	// 내리는 위치는 2N이 아니라 N이야...

	private static int simulation() {
		int answer = 0;
		e: while (true) {
			++answer;

			move();
			moveRobot();
			putRobot();

			int idx = 0;
			for (int i = 0; i < duList.size(); i++) {
				if (duList.get(i) == 0) {
					idx++;
				}
				if (idx == K)
					break e;
			}
		}
		
		return answer;
	}

	private static void move() {
		duList.add(0, duList.remove(duList.size() - 1));
		robot.remove(robot.size() - 1);
		robot.add(0, false);
		robot.set(N - 1, false);

	}

	private static void moveRobot() {
		for (int i = N - 2; i >= 0; i--) {
			if (robot.get(i) && !robot.get(i + 1) && duList.get(i + 1) >= 1) {
				duList.set(i + 1, duList.get(i + 1) - 1);
				robot.set(i + 1, true);
				robot.set(i, false);
			}
		}
		if (robot.get(N - 1)) {
			robot.set(N - 1, false); // 로봇 내리기
		}
	}

	private static void putRobot() {
		if (duList.get(0) >= 1) {
			robot.set(0, true);
			duList.set(0, duList.get(0) - 1);
		}
	}
	
}

 

 

피드백은 언제나 환영입니다 :) 🌳

'algo > 백준' 카테고리의 다른 글

[백준] 11052_카드 구매하기 (JAVA)  (0) 2022.09.20
[백준] 1912_연속합 (JAVA)  (0) 2022.09.19
[백준] 19948_음유시인 영재 (JAVA)  (0) 2022.07.08
[백준] 20300_서강근육맨 💪 (JAVA)  (0) 2022.06.30
[백준] 12933_오리 (JAVA)  (0) 2022.06.27
댓글