이것도 알아야 하네?

[C++] 백준 14499번 주사위 굴리기 (삼성 SW 역량 테스트 기출 문제) 본문

프로그래밍/알고리즘

[C++] 백준 14499번 주사위 굴리기 (삼성 SW 역량 테스트 기출 문제)

아직 갈 길이 먼 사람 2021. 11. 12. 21:35
728x90
#include <iostream>

using namespace std;

int cube[7] = {0};
int x, y;

int offset_x[] = {0, 0, -1, 1}; // east west north south
int offset_y[] = {1, -1, 0, 0};

int WithinRange(int x, int y, int N, int M) {
    if (x >= 0 && x < N && y >= 0 && y < M) return 1;
    else return 0;
}

void Solve(int * graph, int dir, int N, int M) {
    int next_x, next_y;
    switch(dir) {
    case 0: // east;
        next_x = x + offset_x[dir];
        next_y = y + offset_y[dir];
        if (WithinRange(next_x, next_y, N, M)) {
            int temp = cube[1];
            cube[1] = cube[4]; cube[4] = cube[6];
            cube[6] = cube[3]; cube[3] = temp;
            if (graph[next_x * M + next_y] == 0) {
                graph[next_x * M + next_y] = cube[6];
            } else {
                cube[6] = graph[next_x * M + next_y];
                graph[next_x * M + next_y] = 0;
            }
            x = next_x; y = next_y;
            cout << cube[1] << endl;
        }
        break;
    case 1: // west;
        next_x = x + offset_x[dir];
        next_y = y + offset_y[dir];
        if (WithinRange(next_x, next_y, N, M)) {
            int temp = cube[1];
            cube[1] = cube[3]; cube[3] = cube[6];
            cube[6] = cube[4]; cube[4] = temp;
            if (graph[next_x * M + next_y] == 0) {
                graph[next_x * M + next_y] = cube[6];
            } else {
                cube[6] = graph[next_x * M + next_y];
                graph[next_x * M + next_y] = 0;
            }
            x = next_x; y = next_y;
            cout << cube[1] << endl;
        }
        break;
    case 2: // north;
        next_x = x + offset_x[dir];
        next_y = y + offset_y[dir];
        if (WithinRange(next_x, next_y, N, M)) {
            int temp = cube[1];
            cube[1] = cube[5]; cube[5] = cube[6];
            cube[6] = cube[2]; cube[2] = temp;
            if (graph[next_x * M + next_y] == 0) {
                graph[next_x * M + next_y] = cube[6];
            } else {
                cube[6] = graph[next_x * M + next_y];
                graph[next_x * M + next_y] = 0;
            }
            x = next_x; y = next_y;
            cout << cube[1] << endl;
        }
        break;
    case 3: // south;
        next_x = x + offset_x[dir];
        next_y = y + offset_y[dir];
        if (WithinRange(next_x, next_y, N, M)) {
            int temp = cube[1];
            cube[1] = cube[2]; cube[2] = cube[6];
            cube[6] = cube[5]; cube[5] = temp;
            if (graph[next_x * M + next_y] == 0) {
                graph[next_x * M + next_y] = cube[6];
            } else {
                cube[6] = graph[next_x * M + next_y];
                graph[next_x * M + next_y] = 0;
            }
            x = next_x; y = next_y;
            cout << cube[1] << endl;
        }
        break;
    default:
        break;
    }
}

int main() {
    int N, M, K;
    cin >> N >> M >> x >> y >> K; 
    
    int *graph = new int[N * M];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> graph[i * M + j];
        }
    }
    int dir;
    for (int k = 0; k < K; k++) {
        cin >> dir;
        
        //dir - 1;
        Solve(graph, dir - 1, N, M);
        
    }
    delete[] graph;
}
728x90
Comments