하다보니

20055번-컨베이어 벨트 위의 로봇 본문

알고리즘 풀이/백준

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

claire 2022. 3. 27. 19:00
#include<iostream>
#include<deque>
using namespace std;

int n, k;

deque<int> belt;	//컨베이어벨트 내구도
deque<bool> check;	//로봇 체크


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

	cin >> n >> k;

	for (int i = 0; i < 2 * n; i++) {
		int x;
		cin >> x;
		belt.push_back(x);
		check.push_back(false);
	}
	int tot = 0;
	while (1) {
		int cnt = 0;
		for (int i = 0; i < 2 * n; i++) {
			if (belt[i] == 0)cnt++;
		}
		if (cnt >= k) {
			cout << tot;
			return 0;
		}
		//컨베이어벨트 회전
		tot++;
		belt.push_front(belt.back());
		belt.pop_back();

		//로봇도 회전
		check.push_front(check.back());
		check.pop_back();

		//n-1번째에 로봇이 있으면 내려주기
		if (check[n - 1] == true) {
			check[n - 1] = false;
		}

		//로봇 이동
		for (int i = n - 2; i >= 0; i--) {
			if (check[i] && !check[i + 1] && belt[i + 1] > 0) {
				check[i] = false;
				belt[i+1] -= 1;
				if (i == n - 2)continue;
				check[i + 1] = true;
			}
		}
		//로봇 0인덱스에 올리기
		if (!check[0] && belt[0] > 0) {
			check[0] = true;
			belt[0]--;
		}

	}

}

'알고리즘 풀이 > 백준' 카테고리의 다른 글

14502번-연구소  (0) 2022.03.30
12100번-2048(Easy)  (0) 2022.03.30
2206번-벽 부수고 이동하기  (0) 2022.03.17
2579번-계단 오르기  (0) 2022.03.12
9095번-1,2,3 더하기  (0) 2022.03.12