Problem Solving

C++) 백준 20055 컨베이어 벨트 위의 로봇

소년조 2022. 8. 6. 19:18
 

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

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

www.acmicpc.net


코드

 

#include <algorithm>
#include <iostream>
#include <deque>

using namespace std;

#define HP first
#define R second

int n, k, cnt = 0, zeros = 0;
deque<pair<int,bool>> c;

void step1() {
	int tmp = c.back().HP; c.pop_back();
	c.push_front({ tmp, false });
	c[n - 1].R = false;
}

int step2() {
	int zeros = 0;
	for (int i = n - 1; i > 0; i--) {
		if (c[i - 1].R && !c[i].R && c[i].HP > 0) {
			c[i].HP--;
			c[i - 1].R = false; c[i].R = true;
			if (!c[i].HP) zeros++;
		}
	}
	c[n - 1].R = false;
	return zeros;
}

int step3() {
	if (!c.front().HP) return 0;
	c.front().R = true;
	c.front().HP--;
	return !c.front().HP ? 1 : 0;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n >> k;

	for (int i = 0; i < 2 * n; i++) {
		int tmp; cin >> tmp; c.push_back({ tmp, false });
	}

	while (k > zeros) {
		cnt++;
		step1();
		zeros += step2();
		zeros += step3();
	}
	cout << cnt;
}

 

 

풀이

 

deque에서 마지막 값을 빼서 처음 위치에 넣는 방식으로 컨베이어 회전 구현

deque는 <내구도, 로봇 유무> 로 설정