※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 5957번 문제인 Cleaning the Dishes이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/5957 

 

5957번: Cleaning the Dishes

Bessie and Canmuu are teaming up to wash the massive pile of N (1 <= N <= 10,000) dirty dishes left over after the CowMoose Festival. Bessie is washing the dishes; Canmuu will dry them. Each dish has a unique serial number in the range 1..N. At the beginni

www.acmicpc.net

접시가 쌓여있는 상태는 스택을 이용해 나타낼 수 있다.

 

설거지 전 접시, 젖은 접시, 마른 접시 스택을 각각 준비하고, 문제에서 주어지는 설거지 순서에 따라 시뮬레이션을 돌려 문제를 해결하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <stack>
using namespace std;

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

	stack<int> stk1;
	stack<int> stk2;
	stack<int> stk3;

	int N; cin >> N;
	for (int i = N; i > 0; i--) {
		stk1.push(i);
	}

	int p, x;
	while (cin >> p >> x) {
		if (p == 1) {
			while (x--) {
				stk2.push(stk1.top());
				stk1.pop();
			}
		}
		else {
			while (x--) {
				stk3.push(stk2.top());
				stk2.pop();
			}
		}
	}
	while (!stk3.empty()) {
		cout << stk3.top() << '\n';
		stk3.pop();
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 2365 // C++] 숫자판 만들기  (0) 2022.08.01
[BOJ 5956 // C++] Symmetry  (0) 2022.07.31
[BOJ 17295 // C++] 엔드게임 스포일러  (0) 2022.07.31
[BOJ 2302 // C++] 극장 좌석  (0) 2022.07.31
[BOJ 5502 // C++] 팰린드롬  (0) 2022.07.31

+ Recent posts