mojo's Blog

[백준 14499] 주사위 굴리기 본문

백준/simulation

[백준 14499] 주사위 굴리기

_mojo_ 2021. 8. 29. 19:50

문제 링크 => 14499번: 주사위 굴리기 (acmicpc.net)

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지도

www.acmicpc.net


삼성 SW 역량 테스트 문제이다.

 

지도에 (x, y) 위치에 배치된 주사위에 대해서 다음과 같은 규칙을 만족해야 한다.

 

주사위가 지도의 다음 칸으로 이동하는 경우에 대해서

 

(1) 해당 칸의 값이 0 인 경우 => 주사위 바닥면에 쓰인 숫자가 칸에 복사된다.

 

(2) 해당 칸의 값이 0 이 아닌 경우 => 해당 칸에 쓰여진 숫자가 주사위 바닥면으로 복사되며 해당 칸의 숫자는 0 이 된다.

 

(3) 지도 바깥으로 이동하는 경우 =>  이동하지 않음 

 

(1), (2) 을 만족하는 경우 이동한 주사위의 윗면의 숫자를 출력해야 하고 (3) 인 경우에 출력하지 않도록 한다.

 

초기 주사위의 전개도가 다음과 같다고 하자. (1을 아랫면으로 기준을 둠 => location[1] = 1)

 

초기 주사위의 전개도

2

4  1  3

5

6

 

(1) 초기 주사위에서 동쪽으로 주사위가 이동할 경우의 전개도

 

주사위의 전개도

2

1  3  6

5

4

 

이때 1, 3, 4, 6 의 위치가 변경되었다. 

즉, location[1] <= location[3], location[3] <= location[6],

     location[4] <= location[1], location[6] <= location[4] 와 같이 변경되어야 한다.

 

(2) 초기 주사위에서 서쪽으로 주사위가 이동할 경우의 전개도

 

주사위의 전개도

2

6  4  1

5

3

 

이때 1, 3, 4, 6 의 위치가 변경되었다. 

즉, location[1] <= location[4], location[3] <= location[1],

     location[4] <= location[6], location[6] <= location[3] 와 같이 변경되어야 한다.

 

(3) 초기 주사위에서 북쪽으로 주사위가 이동할 경우의 전개도

 

주사위의 전개도

6

4  2  3

1

5

 

이때 1, 2, 5, 6 의 위치가 변경되었다. 

즉, location[1] <= location[2], location[2] <= location[6],

     location[5] <= location[1], location[6] <= location[5] 와 같이 변경되어야 한다.

 

(4) 초기 주사위에서 남쪽으로 주사위가 이동할 경우의 전개도

 

주사위의 전개도

1

4  5  3

6

2

 

이때 1, 2, 5, 6 의 위치가 변경되었다. 

즉, location[1] <= location[5], location[2] <= location[1],

     location[5] <= location[6], location[6] <= location[2] 와 같이 변경되어야 한다.

 

위 방식으로 전개도의 위치만 변경해주고 아랫면의 위치는 location[1] 값을 통해서 가져올 수 있다.

 

풀이 Code

 

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define INF 100000000
#define endl '\n'
#define ll long long

using namespace std;

int x, y, N, M, K;
int Map[20][20];
int dx[5] = { 0,0,0,-1,1 };
int dy[5] = { 0,1,-1,0,0 };
int Dice[7], location[7];
queue<int> commandQ;

void moveDice(int direction) {

	if (direction == 1) {
		int a = location[1], b = location[3], c = location[4], d = location[6];
		location[1] = b, location[3] = d, location[4] = a, location[6] = c;
	}
	else if (direction == 2) {
		int a = location[1], b = location[3], c = location[4], d = location[6];
		location[1] = c, location[3] = a, location[4] = d, location[6] = b;
	}
	else if (direction == 3) {
		int a = location[1], b = location[2], c = location[5], d = location[6];
		location[1] = b, location[2] = d, location[5] = a, location[6] = c;
	}
	else if (direction == 4) {
		int a = location[1], b = location[2], c = location[5], d = location[6];
		location[1] = c, location[2] = a, location[5] = d, location[6] = b;
	}

}

int getUpSide(int cur) {

	if (cur == 1) return 6;
	else if (cur == 2) return 5;
	else if (cur == 3) return 4;
	else if (cur == 4) return 3;
	else if (cur == 5) return 2;
	else if (cur == 6) return 1;

}

void input() {

	cin >> N >> M >> x >> y >> K;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> Map[i][j];
		}
	}
	
	for (int i = 0; i < K; i++) {
		int command;
		cin >> command;
		commandQ.push(command);
	}
}


void solve() {

	for (int i = 1; i <= 6; i++)
		location[i] = i;

	while (!commandQ.empty()) {
		int command = commandQ.front();
		commandQ.pop();

		int nextX = x + dx[command], nextY = y + dy[command];
		if (nextX >= 0 && nextX < N && nextY >= 0 && nextY < M) {
			x = nextX, y = nextY;
			moveDice(command);
			if (Map[x][y] == 0) {
				Map[x][y] = Dice[location[1]];
			}
			else {
				Dice[location[1]] = Map[x][y];
				Map[x][y] = 0;
			}
			cout << Dice[getUpSide(location[1])] << endl;
		}
	}

}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	input();
	solve();

	return 0;
}

 

'백준 > simulation' 카테고리의 다른 글

[백준 19236] 청소년 상어  (0) 2022.03.31
[백준 15685] 드래곤 커브  (0) 2021.12.29
[백준 15683] 감시  (0) 2021.08.31
[백준 14891] 톱니바퀴  (0) 2021.08.30
[백준 14890] 경사로  (0) 2021.08.29
Comments