풀이는 아래와 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
vector<tuple<int,int>> order;
int move(int num){
if(num == 1){
return 1;
}
return 2*move(num-1)+1;
}
void solve(int num, int a, int b){
if(num == 1){
order.push_back(make_tuple(a,b));
return;
}
solve(num-1,a,6-a-b);
order.push_back(make_tuple(a,b));
solve(num-1,6-a-b,b);
}
int main()
{
int n;
cin >> n;
cout << move(n) << '\n';
solve(n,1,3);
vector<tuple<int,int>>::iterator iter;
for(iter = order.begin(); iter != order.end(); iter++){
cout << get<0>(*iter) << ' ' << get<1>(*iter) << '\n';
}
return 0;
}
|
cs |
생각보다 간단하게 풀린다. 근데 이런식으로 굳이 vector와 tuple을 쓸 필요없이 바로 출력하면 되긴 하는데
이런 식으로 여러가지 자료구조를 사용하는 방법을 익혀놓으면 더 어려운 문제를 풀때 나중에 도움이 되지 않을까 싶다.
반응형
'Baekjoon(C++)' 카테고리의 다른 글
[C++]백준 알고리즘 9020번 (0) | 2021.07.27 |
---|---|
[C++]백준 알고리즘 1011번 (0) | 2021.07.27 |
[C++]백준 알고리즘 2447번 (1) | 2021.07.18 |
[C++] 띄어쓰기 포함 여러개 문자열을 입력받을때 (cin, getline에 대하여) (1) | 2021.07.15 |
[C++]백준 알고리즘 1316번 (0) | 2021.07.14 |