Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- c++
- 투 포인터
- DFS
- Team Fortress 2
- 유니티
- 구현
- 브루트포스
- 유니온 파인드
- 누적 합
- 트리
- ue5
- 알고리즘
- 우선순위 큐
- 스택
- VR
- 수학
- 백트래킹
- 다이나믹 프로그래밍
- 재귀
- 정렬
- Unreal Engine 5
- 시뮬레이션
- XR Interaction Toolkit
- 다익스트라
- BFS
- 자료구조
- 그리디 알고리즘
- 그래프
- 백준
- 문자열
Archives
- Today
- Total
1일1알
백준 14891번 톱니바퀴 C++ 본문


톱니바퀴의 각 정보를 벡터로 저장하고, 왼쪽으로 회전한다면 첫번째 원소를 erase하고 맨 뒤에 다시 넣고, 오른쪽으로 회전한다면 마지막 원소를 pop하고 insert하는 방식으로 작동하는 Rotate 함수를 만들었다.
그리고 Solve 함수에서 맞닿아있는 극이 다르다면 재귀적으로 함수를 실행하도록 하였다.
rotDir을 회전 방향, dir을 진행 방향으로 설정하여 왼쪽으로 진행하는 것과 오른쪽으로 진행하는 것을 따로 시뮬레이션 하였다. 그리고 한쪽으로 진행했다면 처음 돌린 톱니바퀴는 돌아가있는 상태이기 때문에 반대쪽으로 한번 회전시킨 뒤 다른쪽으로 진행하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
vector<vector<int>> wheel(5);
void Rotate(int num, int rotDir) {
if (rotDir == -1) {
int tmp = wheel[num][0];
wheel[num].erase(wheel[num].begin());
wheel[num].push_back(tmp);
}
else {
int tmp = wheel[num][7];
wheel[num].pop_back();
wheel[num].insert(wheel[num].begin(), tmp);
}
}
void Solve(int num, int rotDir, int dir) {
if (num <= 0 || num > 4) return;
if (dir == -1) {
int lastLeft = wheel[num][6];
Rotate(num, rotDir);
if (num == 1) {
return;
}
if (wheel[num - 1][2] != lastLeft) {
Solve(num - 1, -rotDir, dir);
}
}
else {
int lastRight = wheel[num][2];
Rotate(num, rotDir);
if (num == 4) {
return;
}
if (wheel[num + 1][6] != lastRight) {
Solve(num + 1, -rotDir, dir);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string str;
for (int i = 1; i <= 4; i++) {
cin >> str;
for (int j = 0; j < 8; j++) {
wheel[i].push_back(str[j] - '0');
}
}
int k;
cin >> k;
while (k--) {
int num, rotDir;
cin >> num >> rotDir;
Solve(num, rotDir, -1);
Rotate(num, -rotDir);
Solve(num, rotDir, 1);
}
int ans = 0;
int score = 1;
for (int i = 1; i <= 4; i++) {
if (wheel[i][0] == 1) {
ans += score;
}
score *= 2;
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 2303번 숫자 게임 C++ (0) | 2022.01.08 |
---|---|
백준 15683번 감시 C++ (0) | 2022.01.07 |
백준 16234번 인구 이동 C++ (0) | 2022.01.03 |
백준 2225번 합분해 C++ (0) | 2022.01.02 |
백준 3190번 뱀 C++ (0) | 2022.01.01 |