Little Jay

[C++] 백준 20055번 - 컨베이어 벨트 위의 로봇 본문

알고리즘/BOJ

[C++] 백준 20055번 - 컨베이어 벨트 위의 로봇

Jay, Lee 2022. 7. 18. 16:56

쉬웠던 구현문제였지만 오타 하나를 못봐서 30분간 맞왜틀하고 있었던 문제였다.

처음에는 이차원 배열을 통해서 구현해야하나 싶었는데, 생각해보면 deque를 사용하면 좋겠다라는 생각을 했다.

컨베이어벨트를 회전시킬 때 단순히 push_front, pop_back을 해주면 컨테이너가 잘 회전이 된다.

생각없이 check 컨테이너에 push_back 써놓은거를 못봐서 애먹었다. 

 

벨트 회전 - 로봇 움직이기 - 로봇 배치 - 내구도 체크 순서대로 진행하면 된다.

 

특히 로봇은 0~n-1에서 놀고 n ~ 2n -1에 절대로 가지 않는다. 어차피 n-1에서 다 떨어져야하기 때문이다.

 

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int n, k;
deque<int> durability;
deque<bool> check;

int find() {
	int durable = 0;
	for (int i = 0; i < durability.size(); i++) {
		if (durability[i] == 0) durable++;
	}
	return durable;
}

void move_robot() {
	for (int i = n - 2; i >= 0; i--) {
		if (check[i] == true && check[i+1] == false && durability[i+1] > 0) {
			durability[i + 1]--; 
			check[i + 1] = true;
			check[i] = false;
		}
	}
	check[n - 1] = false;
}

void place_robot() {
	if (durability[0] > 0 && check[0] == false) {
		durability[0]--;
		check[0] = true;
	}
}

void rotate() {
	durability.push_front(durability.back());
	durability.pop_back();
	check.push_front(check.back());
	check.pop_back();
	check[n - 1] = false;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;

	for (int i = 0; i < 2*n; i++) {
		int x; cin >> x;
		durability.push_back(x);
		check.push_back(false);
	}
	int ans = 0;
	while (1) {
		ans++;
		rotate();
		move_robot();
		place_robot();

		if (find() >= k) {
			cout << ans << endl;
			break;
		}
	}

	return 0;
}
Comments