반응형

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

 

23288번: 주사위 굴리기 2

크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다. 가장 왼

www.acmicpc.net

 

#include <bits/stdc++.h>

using namespace std;

int N, M, K, Map[21][21], pointMap[21][21], ans;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int moveDirection = 0;
//dice는 문제의 나온 전개도랑 다릅니다. 제가 문제에 제시된걸 안보고 풀어서 그렇습니다..
int dice[6] = {6, 5, 1, 2, 4, 3};
int floorX = 1, floorY = 1;

void drawPointMap() {
    queue<pair<int, int> > Q;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            if (pointMap[i][j] == 0) {
                vector<pair<int, int> > v;
                Q.push({i, j});
                //방문체크
                pointMap[i][j] = -1;
                v.push_back({i, j});

                while (!Q.empty()) {
                    int x = Q.front().first;
                    int y = Q.front().second;
                    Q.pop();

                    for (int k = 0; k < 4; k++) {
                        int nx = x + dx[k];
                        int ny = y + dy[k];
                        if (nx <= 0 || nx > N || ny <= 0 || ny > M) continue;
                        if (Map[nx][ny] == Map[i][j] && pointMap[nx][ny] != -1) {
                            Q.push({nx, ny});
                            v.push_back({nx, ny});
                            pointMap[nx][ny] = -1;
                        }
                    }
                }
                for (int k = 0; k < v.size(); k++) {
                    pointMap[v[k].first][v[k].second] = Map[i][j] * v.size();
                }
            }
        }
    }
}

void turnMoveDirection() {
    if (dice[0] > Map[floorX][floorY]) {
        moveDirection = (moveDirection + 1) % 4;
    } else if (dice[0] < Map[floorX][floorY]) {
        moveDirection = (moveDirection + 3) % 4;
    }
}

void diceToRight() {
    int tmp = dice[0];
    dice[0] = dice[5];
    dice[5] = dice[2];
    dice[2] = dice[4];
    dice[4] = tmp;
}

void diceToDown() {
    int tmp = dice[3];
    dice[3] = dice[0];
    dice[0] = dice[1];
    dice[1] = dice[2];
    dice[2] = tmp;
}


void checkAndMove() {
    // 아랫면 좌표 이동
    int x = floorX + dx[moveDirection];
    int y = floorY + dy[moveDirection];
    if (x <= 0 || x > N || y <= 0 || y > M) {
        moveDirection = (moveDirection + 2) % 4;
        x = floorX + dx[moveDirection];
        y = floorY + dy[moveDirection];
    }
    floorX = x;
    floorY = y;


    //주사위 이동
    if (moveDirection == 0) {
        diceToRight();
    } else if (moveDirection == 2) {
        for (int i = 0; i < 3; i++) diceToRight();
    } else if (moveDirection == 1) {
        diceToDown();
    } else {
        for (int i = 0; i < 3; i++) diceToDown();
    }
    
    ans += pointMap[floorX][floorY];
    turnMoveDirection();
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> M >> K;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            cin >> Map[i][j];
        }
    }


    drawPointMap();

    for (int i = 1; i <= K; i++) checkAndMove();


    cout << ans << '\n';

    return 0;
}

 

반응형

+ Recent posts