1일1알

백준 2818번 숙제하기 싫을 때 C++ 본문

알고리즘

백준 2818번 숙제하기 싫을 때 C++

영춘권의달인 2022. 2. 1. 22:37

출처 : https://www.acmicpc.net/problem/2818

 

주사위는 마주 보는 면의 합이 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