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

Memo


어려웠던 점

로봇이 움직이는 건지, 컨베이어 벨트가 움직이는 건지 좀 헷갈리는 문제였고, 문제 자체가 좀 이해하기 힘들었습니다. 로봇이 어떤 칸에 올라가거나 이동하면 그 칸의 내구도는 즉시 1만큼 감소한다 라는 말이 잘 이해가 안가서 코드를 작성하는데 오래 걸렸던 것 같고, 회전하는 배열에 대한 구현이 조금은 모자라다고 생각이 들었습니다.

해결 아이디어

문제 평가

삼성 기출중 난이도가 solved.ac 티어 기준으로 낮은 편에 속하는 문제였지만, 문제를 이해하는데 힘이 들었던 문제였습니다. 또한 down 다음 부분부터는 로봇이 존재하지 않는다는 것을 생각하지 못했습니다. 코드를 작성하기 전 충분한 고민이 필요하다는 것을 다시 한 번 느낀 문제였습니다.

Code


제출 날짜

2021/02/26

메모리

2016 KB

시간

72 ms

#include <iostream>
#include <vector>
#define endl "\\n"

int N, K;
std::vector<int> conveyor;
std::vector<int> robots;
int stage;
int down;
int up;

void input_faster()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

void input()
{
	input_faster();
	std::cin >> N >> K;
	conveyor.resize(N * 2 + 1);
	for (size_t i = 1 ; i <= N * 2 ; i++)
		std::cin >> conveyor[i];
	robots.resize(N * 2 + 1);
	stage = 0;
	down = N;
	up = 1;
}

int cal_all()
{
	int rnt = 0;

	for (size_t i = 1 ; i <= N * 2; i++)
		if (conveyor[i] == 0)
			rnt++;
	return (rnt);
}

void rotate_step_one()
{
	if (down == 1)
		down = 2 * N;
	else
		down--;
	if (up == 1)
		up = 2 * N;
	else
		up--;
	robots[down] = 0;
}

void rotate_step_two()
{
	int i = 0;
	int robot_index;

	while (i < N)
	{
		robot_index = down - i < 1 ? 2 * N + down - i : down - i;
		if (robot_index + 1 > 2 * N)
		{
			if (robots[robot_index] && !robots[1] && conveyor[1] > 0)
			{
				robots[1] = robots[robot_index];
				robots[robot_index] = 0;
				conveyor[1]--;
				if (down == 1)
					robots[1] = 0;
			}
		}
		else
		{
			if (robots[robot_index] && !robots[robot_index + 1] && conveyor[robot_index + 1] > 0)
			{
				robots[robot_index + 1] = robots[robot_index];
				robots[robot_index] = 0;
				conveyor[robot_index + 1]--;
				if (down == robot_index + 1)
					robots[down] = 0;
			}
		}
		i++;
	}
}

void step_three()
{
	if (!robots[up] && conveyor[up] > 0)
	{
		robots[up] = 1;
		conveyor[up]--;
	}
}

void solve()
{
	while (cal_all() < K)
	{
		stage++;
		rotate_step_one();
		rotate_step_two();
		step_three();
	}
	std::cout << stage;
}
int main()
{
	input();
	solve();
	return (0);
}