20055번: 컨베이어 벨트 위의 로봇 (acmicpc.net)
20055번: 컨베이어 벨트 위의 로봇
길이가 N인 컨베이어 벨트가 있고, 길이가 2N인 벨트가 이 컨베이어 벨트를 위아래로 감싸며 돌고 있다. 벨트는 길이 1 간격으로 2N개의 칸으로 나뉘어져 있으며, 각 칸에는 아래 그림과 같이 1부
www.acmicpc.net
처음에 로봇의 회전을 큐로 처리하려다가 실패
컨케이어 벨트가 움직일 때마다, 로봇의 위치와, 벨트 배열 값을 한칸씩 뒤로 보내기
import java.util.*;
class Solution {
static int N,K;
static int[] d;
static int[] robot;
static int count=0;
public static void main(String args[]) {
int answer=0;
Scanner sc=new Scanner(System.in);
N=sc.nextInt();
K=sc.nextInt();
robot=new int[N];
d=new int[2*N];
for(int i=0;i<2*N;i++) {
d[i]=sc.nextInt();
}
while(true) {
//벨트의 회전
rotate();
rotateRobot();
//로봇이 이동한다
for(int i=N-2;i>=0;i--) {
if(robot[i]==1) {
if(d[i+1]>0 && robot[i+1]==0) {
robot[i+1]=1;
robot[i]=0;
d[i+1]--;
}
}
}
//로봇 올려두기
put();
answer++;
if(countZero()>=K) break;
}
System.out.println(answer);
}
static void rotate() {
int temp=d[2*N-1];
for(int i=2*N-1;i>0;i--) {
d[i]=d[i-1];
}
d[0]=temp;
}
//로봇의 회전
static void rotateRobot() {
for(int i=N-1;i>0;i--) {
robot[i]=robot[i-1];
}
robot[0]=0;
robot[N-1]=0;
}
//로봇 올려두기
static void put() {
if(d[0]>0) {
d[0]--;
robot[0]=1;
}
}
static int countZero() {
int count=0;
for(int i=0;i<2*N;i++) {
if(d[i]==0) count++;
}
return count;
}
}
'코테 공부' 카테고리의 다른 글
[SWEXPERT] 1961. 숫자 배열 회전(자바) (0) | 2022.12.31 |
---|---|
[DP]조 짜기(백준 2229번, 자바)☆☆☆☆☆ (0) | 2022.12.20 |
주사위 굴리기(백준 14499번), 자바(☆) (0) | 2022.12.19 |
[SWEA]최장 증가 부분 수열(3307번, 자바), DP (0) | 2022.11.19 |
[SWEA]진기의 최고급 붕어빵(1860번, 자바)★ (0) | 2022.11.18 |