티스토리 뷰

728x90
반응형
문제 링크

https://www.acmicpc.net/problem/20055

 

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

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

www.acmicpc.net

문제 풀이

시뮬레이션 문제이다. 시뮬레이션 문제는 요구하는 바를 정확히 이해하고, 중요한 조건을 캐치해서 그대로 구현해내기만 하면되는 문제이다. (근데 엄한데서 조건을 빼먹어서 한참 찾았다. ㅠㅠ)

소스 코드
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class P20055 {
	static Queue<Integer> robots;
	static int [] belt ;
	static int N,K;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		K = sc.nextInt();
		belt = new int [N*2];
		
		for (int i = 0; i < belt.length; i++) {
			belt[i] = sc.nextInt();
		}
		
		//로봇위치는 큐
		robots = new LinkedList<>();
		int cnt =0;
		while(true) {
			cnt++;
			//회전
			rotate();
			//이동
			move();
			//로봇 올림
			put();
			//내구도 체크
			if(checkEnd()) break;
		}
		System.out.println(cnt);
		
		
	}
	
	public static void rotate() {
		// belt 회전
		int temp = belt[belt.length-1];
		for (int i = belt.length-1; i >0; i--) {
			belt[i] = belt[i-1];
		}
		belt[0] = temp;
		
		//로봇 회전
		int size = robots.size();
		
		while(size-->0) {
			int robot = robots.poll()+1;
			if(robot != N-1) robots.add(robot);
		}
	}
	
	
	public static void move() {
		
		//로봇 무브
		int size = robots.size();
		
		while(size-->0) {
			//뽑기
			int robot = robots.poll();
			
			if(isExistRobot(robot+1) || belt[robot+1]<=0) {
				robots.add(robot);
				
			}else {
				belt[robot+1]--;
				if(robot+1 != N-1) robots.add(robot+1);
			}
		}
	}
	
	public static boolean isExistRobot(int robot) {
		for (int i : robots) {
			if(robot == i) return true;
		}
		return false;
	}
	
	
	public static void put() {
		if(belt[0]>=1) {
			
			robots.add(0);
			belt[0]--;
		}
		
		
	}
	
	
	public static boolean checkEnd() {
		int cnt = 0;
		
		for (int i : belt) {
			if(i<=0) cnt++;
		} 
		return cnt>=K ? true: false;
		
	}
	
	
}
728x90
반응형
250x250
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함