728x90
https://www.acmicpc.net/problem/20055
시뮬레이션 + 구현문제이다. 약간 삼성틱한 문제인데 역량 기출문제는 아니었다. 별다른 알고리즘이 필요한 것은 아니고 그냥 구현이 피곤한 문제였다.
아이디어는 빠르게 떠올렸는데 두 부분에서 해맸다.
1. 2차원배열의 위치 변경
2. 문제를 조금 잘 못 읽음
다른 사람들은 1차원 배열로 만들어서 풀이했는데 이것도 좋은 방법이다. 그러나 난 문제가 시키는대로 풀이했다.
이번에는 별다른 고찰이나 생각을 할 필요까지는 없었고..
아이디어와 구현 계획은 빠르게 짰으나 자잘한 실수가 많았다. 이런 문제는 40분 안에 끝낼 수 있어야할 것이다.
#include <iostream>
using namespace std;
int belt[2][101];
bool robot[2][101] = { false, };
int N, M;
void rotation() {
int tmp = belt[0][N - 1];
bool temp = robot[0][N - 1];
for (int i = N - 1; i > 0; i--) {
belt[0][i] = belt[0][i - 1];
robot[0][i] = robot[0][i - 1];
}
belt[0][0] = belt[1][0];
robot[0][0] = robot[1][0];
for (int i = 0; i < N - 1; i++) {
belt[1][i] = belt[1][i + 1];
robot[1][i] = robot[1][i + 1];
}
belt[1][N - 1] = tmp;
robot[1][N - 1] = temp;
}
void move_robot() {
for (int i = N - 2; i > -1; i--) {
if (robot[0][i] == true) {
if (robot[0][i + 1] == false && belt[0][i + 1] > 0) {
robot[0][i] = false;
robot[0][i + 1] = true;
belt[0][i + 1]--;
if (i + 1 == N - 1) robot[0][i + 1] = false;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int j = 0; j < N; j++)
cin >> belt[0][j];
for (int j = N - 1; j > -1; j--)
cin >> belt[1][j];
int answer = 0;
while (true) {
answer++;
rotation();
robot[0][N - 1] = false;
move_robot();
if (belt[0][0] > 0) {
robot[0][0] = true;
belt[0][0]--;
}
int cnt = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < N; j++) {
if (belt[i][j] <= 0) {
cnt++;
}
}
}
/*
cout << endl;
cout << answer << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < N; j++) {
cout << belt[i][j] << " ";
}
cout << endl;
}*/
if (cnt >= M) break;
}
cout << answer << '\n';
}
'백준 문제 풀이' 카테고리의 다른 글
백준 2458번 - 키 순서 (C++) (2) | 2023.12.26 |
---|---|
백준 12919번 - A와 B 2(C++) (2) | 2023.12.23 |
백준 7579번 앱 (C++) (0) | 2023.12.15 |
백준 11729번 하노이 탑 이동 순서 (C++) (0) | 2023.12.15 |
백준 29792번 규칙적인 보스돌이 (C++) (0) | 2023.12.14 |