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();
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 15653 구슬 탈출 4(JAVA) (0) | 2020.10.17 |
---|---|
백준 13334 철로(JAVA) (0) | 2020.10.12 |
백준 1938 통나무 옮기기 (0) | 2020.10.08 |
백준 1670 정상회담 2 (0) | 2020.10.05 |
백준 1219 오민식의 고민(Java) (0) | 2020.10.03 |