본문 바로가기

Algorithms/Baekjoon

[Java] 백준 알고리즘 20055번 문제 : 삼성 SW 역량 테스트 기출 문제 - 컨베이어 벨트 위의 로봇 (Simulation, 구현)

--- 문제 ---

 

 

 

--- 코드 ---

 

 

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.LinkedList;

public class Bj20055 {
	static int N,K;
	static int result, zero_cnt;
	static int[] belt;
	static boolean[] robot;
	
	static int stoi(String s) {
		return Integer.parseInt(s);
	}
	
	static void move() {
		
		// final down
		robot[N-1] = false;
				
		// belt move 1 step
		int last = belt[2*N-1];
		for(int i=2*N-1; i>0; --i) {
			belt[i] = belt[i-1];
		}
		belt[0] = last;
		
		// robot move 1 step to final
		if(robot[N-2]) {
			// after down
			robot[N-2] = false;
			
		}
		// move other robots
		for(int i=N-3; i>=0; --i) {
			if(robot[i]) {
				// if can step one more
				if(!robot[i+2]&& belt[i+2]>0) {
					// if not final turn true;
					if(i!=N-3) {
						robot[i+2]=true;
					}
					belt[i+2]--;
					if(belt[i+2]==0) {
						++zero_cnt;
					//robot[i] = false;
					}
				}
				else {
					robot[i+1] = true;
				}
				
				// turn false
				robot[i] = false;
			}
		
			
		}
		
		// put new one it can
		if(belt[0]>0) {
			robot[0] = true;
			belt[0]--;
			if(belt[0]==0) {
				++zero_cnt;
			}
		}
		
	}
	
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = stoi(st.nextToken());
		K = stoi(st.nextToken());
		
		belt = new int[2*N];
		robot = new boolean[N];
		result = 0;
		zero_cnt = 0;
		
		st = new StringTokenizer(br.readLine());
		for(int i=0; i<2*N; ++i) {
			belt[i] = stoi(st.nextToken());
		}
		
		while(zero_cnt<K) {
			++result;
			
			move();
			
		}
		
		System.out.println(result);
	}
}

 

 

--- 출처 ---

 

www.acmicpc.net/problem/20055

 

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

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

www.acmicpc.net

 

반응형