코테 공부

컨베이어 벨트 위의 로봇(백준 20055번), 자바(☆)

DaEun_ 2022. 12. 20. 17:53

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;
	}

	
	
}