알고리즘/백준
백준 20055 컨베이어 벨트 위의 로봇(Java)
우리로
2020. 10. 20. 01:35
https://www.acmicpc.net/problem/20055
삼성 기출문제다.(2021년 상반기용)
주어진 조건대로 구현하면 된다!
import java.io.*;
import java.lang.*;
import java.util.*;
public class boj20055 {
static int N, K, turn;
static int[] conveyor;
static ArrayList<Integer> robots = new ArrayList<>();
static int rotate() {
int last = conveyor[2 * N];
System.arraycopy(conveyor, 1, conveyor, 2, 2 * N - 1);
conveyor[1] = last;
int exit = -1, zero = 0;
for (int i = 0; i < robots.size(); i++) {
int next = (robots.get(i) == 2 * N ? 1 : robots.get(i) + 1);
robots.set(i, next);
if (next == N)
exit = i;
}
if (exit != -1)
robots.remove(exit);
return zero;
}
static int robotMove() {
int exit = -1, zero = 0;
for (int i = 0; i < robots.size(); i++) {
int next = (robots.get(i) == 2 * N ? 1 : robots.get(i) + 1);
if (conveyor[next] > 0 && robots.stream().noneMatch(n -> n == next)) {
robots.set(i, next);
conveyor[next]--;
if (conveyor[next] == 0)
zero++;
}
if (next == N)
exit = i;
}
if (exit != -1)
robots.remove(exit);
return zero;
}
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
conveyor = new int[2 * N + 1];
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= 2 * N; i++) conveyor[i] = Integer.parseInt(st.nextToken());
int zeroCount = 0;
while (zeroCount < K) {
turn++;
// rotate belt
zeroCount += rotate();
// robot move
zeroCount += robotMove();
boolean isFirst = robots.stream().anyMatch(n -> n == 1);
if (!isFirst && conveyor[1] > 0) {
robots.add(1);
conveyor[1]--;
if(conveyor[1] == 0){
zeroCount++;
}
}
}
bw.write(turn + "\n");
bw.flush();
}
}