슈뢰딩거의 고등어

14499 주사위굴리기 본문

알고리즘

14499 주사위굴리기

슈뢰딩거의 고등어 2021. 12. 19. 14:48

https://www.acmicpc.net/problem/14499

 

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

 

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

/*
주사위는 지도 위에 윗 면이 1이고, 동쪽을 바라보는 방향이 3인 상태로 놓여져 있으며, 
놓여져 있는 곳의 좌표는 (x, y) 이다. 가장 처음에 주사위에는 모든 면에 0이 적혀져 있다.
*/
int n, m;
int x, y;
int k;
int arr[21][21];
int Dice[7];

vector <int> cmd;

const int dy[4] = {1, -1, 0, 0};
const int dx[4] = {0, 0, -1, 1};


void roll_dice(int d) {

	int d1, d2, d3, d4, d5, d6;
    d1 = Dice[1], d2 = Dice[2], d3 = Dice[3];
    d4 = Dice[4], d5 = Dice[5], d6 = Dice[6];
 
    if (d == 0)
    {
        Dice[1] = d4;
        Dice[4] = d6;
        Dice[6] = d3;
        Dice[3] = d1;
    }
    else if (d == 1)
    {
        Dice[4] = d1;
        Dice[6] = d4;
        Dice[3] = d6;
        Dice[1] = d3;
    }
    else if (d == 2)
    {
        Dice[1] = d5;
        Dice[2] = d1;
        Dice[6] = d2;
        Dice[5] = d6;
    }
    else if (d == 3)
    {
        Dice[5] = d1;
        Dice[1] = d2;
        Dice[2] = d6;
        Dice[6] = d5;
    }
}

void solve() {

 	for(int i=0; i<cmd.size(); i++) {
		int ny = y + dy[cmd.at(i)];
		int nx = x + dx[cmd.at(i)];
		int d = cmd.at(i);

		if(ny < 0 || ny >= m || nx < 0 || nx >= n) continue;

		roll_dice(d);
        //  주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 
		if(arr[nx][ny] == 0)
			arr[nx][ny] = Dice[6]; // 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다.
		else {
			Dice[6] = arr[nx][ny]; // 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.
			arr[nx][ny] = 0;
		}

		printf("%d\n", Dice[1]);
		x = nx;
		y = ny;
	}

}

int main() {

	scanf("%d %d %d %d %d", &n, &m, &x, &y, &k);

	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			scanf("%d", &arr[i][j]);
		}
	}


	for(int i=0; i<k; i++) {
		int tmp;
		scanf("%d", &tmp);
		tmp--;
		cmd.push_back(tmp);
	}

	solve();

	return 0;

}

둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 

이것떄문에 arr[ny][nx] 가 아니라 arr[nx][ny]로 생각했어야했음./

 

'알고리즘' 카테고리의 다른 글

15685 드래곤 커브 (c++)  (0) 2021.12.21
오픈채팅방  (0) 2021.12.21
13460 구슬탈출2  (0) 2021.12.19
21609 상어중학교  (0) 2021.12.13
21608 상어초등학교  (0) 2021.12.11
Comments