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 |
Tags
- 그래프
- 알고리즘
- c++
- 수학
- 자료구조
- 누적 합
- ue5
- 브루트포스
- 그리디 알고리즘
- 트리
- 투 포인터
- Unreal Engine 5
- 다이나믹 프로그래밍
- 백준
- 재귀
- DFS
- VR
- Team Fortress 2
- 스택
- XR Interaction Toolkit
- 정렬
- 유니티
- BFS
- 시뮬레이션
- 백트래킹
- 유니온 파인드
- 구현
- 우선순위 큐
- 다익스트라
- 문자열
Archives
- Today
- Total
1일1알
백준 2818번 숙제하기 싫을 때 C++ 본문
주사위는 마주 보는 면의 합이 7이기 때문에 세 면의 정보만 알고 있으면 어디로 구르든 다음 위에 오는 숫자의 정보를 알 수 있다.
그리고 4번 구르면 원래 상태와 같아지기 때문에 많이 가야하는 경우는 이것을 이용해 한번에 이동할 수 있다.
그리고 행*열*주사위 최고 수 = 100000*100000*6 은 int 범위를 넘어가기 때문에 주의해야 한다.
#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;
using ll = long long;
int dRow[4] = { 0,1,0,1 };
int dCol[4] = { 1,0,-1,0 };
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int r, c;
cin >> r >> c;
int up = 1;
int front = 2;
int right = 3;
int row = 0;
int col = 0;
int dir = 0;
ll sum = 1;
while (true) {
int nextRow = row + dRow[dir];
int nextCol = col + dCol[dir];
if (nextRow < 0 || nextRow >= r || nextCol < 0 || nextCol >= c) {
if (dir == 1 || dir == 3) break;
dir = (dir + 1) % 4;
continue;
}
int tmp_right = right;
int tmp_front = front;
int tmp_up = up;
if (dir == 0) {
int jump = (c - 1 - col) / 4;
if (jump > 0) {
sum += jump * 14;
col += jump * 4;
continue;
}
right = tmp_up;
up = 7 - tmp_right;
}
else if (dir == 1 || dir == 3) {
front = tmp_up;
up = 7 - tmp_front;
dir = (dir + 1) % 4;
}
else if (dir == 2) {
int jump = col / 4;
if (jump > 0) {
sum += jump * 14;
col -= jump * 4;
continue;
}
up = tmp_right;
right = 7 - tmp_up;
}
row = nextRow;
col = nextCol;
sum += up;
}
cout << sum;
};
'알고리즘' 카테고리의 다른 글
백준 10836번 여왕벌 C++ (0) | 2022.02.03 |
---|---|
백준 17259번 선물이 넘쳐흘러 C++ (0) | 2022.02.02 |
백준 1680번 쓰레기 수거 C++ (0) | 2022.02.01 |
백준 2016번 미팅 주선하기 C++ (0) | 2022.02.01 |
백준 3987번 보이저 1호 C++ (0) | 2022.02.01 |