[BOJ] 14499๋ฒˆ | ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ (C++)

2024. 3. 17. 16:35ใ†Coding Test/BOJ

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

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

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

์ผ์ข…์˜ ๊ณต๊ฐ„์ง€๊ฐ๋Šฅ๋ ฅ(?)์„ ํ•„์š”๋กœ ํ•˜๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ดˆ๊ธฐ์— ์ฃผ์–ด์ง„ ์ฃผ์‚ฌ์œ„ ์ƒํƒœ์—์„œ ๋™, ์„œ, ๋‚จ, ๋ถ ๋ฐฉํ–ฅ์œผ๋กœ ๊ตฌ๋ฅด๋ฉด ๊ทธ์— ๋”ฐ๋ผ ์ฃผ์‚ฌ์œ„ ๋ฉด์— ์ ํžŒ ์ˆซ์ž๋ฅผ ์ž˜ ์ •๋ฆฌํ•˜๋Š” ์ ˆ์ฐจ๊ฐ€ ํ•„์š”ํ•˜์ฃ . ๊ทธ๋ž˜์„œ ์ €๋Š” 6๊ฐœ์˜ ์›์†Œ๋ฅผ ๊ฐ€์ง€๋Š” 1์ฐจ์› ๋ฐฐ์—ด์—๋‹ค๊ฐ€ ์ฃผ์‚ฌ์œ„์˜ ๊ฐ ๋ฉด์— ์ ํžŒ ์ˆซ์ž ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

enum DIRECTION
{
    TOP,
    RIGHT,
    LEFT,
    FORWARD,
    BACK,
    DOWN,
};

vector<int> SurfaceOfDice(6);

 

์ด์ œ ์ฃผ์–ด์ง„ ๋ช…๋ น์–ด ์ด๋™ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์ฃผ์‚ฌ์œ„์˜ ๋‹ค์Œ ์œ„์น˜๋ฅผ ๊ตฌํ•ด์ฃผ๊ณ , ํ•ด๋‹น ์œ„์น˜๊ฐ€ ๋งต ์•ˆ์ผ ๊ฒฝ์šฐ์—๋งŒ ๋‹ค์Œ์ฒ˜๋Ÿผ ์ฃผ์‚ฌ์œ„ ๋ฉด์˜ ์ˆซ์ž ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค๋‹ˆ๋‹ค.

  • ๋™(RIGHT) : 1)์˜ค๋ฅธ๋ฉด์ด ์•„๋žซ๋ฉด, 2)์œ— ๋ฉด์ด ์˜ค๋ฅธ๋ฉด, 3) ์™ผ๋ฉด์ด ์œ— ๋ฉด, 4) ์•„๋žซ๋ฉด์ด ์™ผ๋ฉด
  • ์„œ(LEFT) : 1)์™ผ๋ฉด์ด ์•„๋žซ๋ฉด, 2)์œ— ๋ฉด์ด ์™ผ๋ฉด, 3)์˜ค๋ฅธ๋ฉด์ด ์œ— ๋ฉด, 4) ์•„๋žซ๋ฉด์ด ์˜ค๋ฅธ๋ฉด
  • ๋ถ(FORWARD) : 1)์ •๋ฉด์ด ์•„๋žซ๋ฉด, 2) ์œ— ๋ฉด์ด ์ •๋ฉด, 3) ํ›„๋ฉด์ด ์œ— ๋ฉด, 4) ์•„๋žซ๋ฉด์ด ํ›„๋ฉด
  • ๋‚จ(BACK) : 1)ํ›„๋ฉด์ด ์•„๋žซ๋ฉด, 2) ์œ— ๋ฉด์ด ํ›„๋ฉด, 3)์ •๋ฉด์ด ์œ— ๋ฉด, 4)์•„๋žซ๋ฉด์ด ์ •๋ฉด
bool Move(Point& dicePosition, const int directionIndex, const int maxRow, const int maxColumn)
{
    int nextDiceRow = dicePosition.first + Directions[directionIndex].first;
    int nextDiceColumn = dicePosition.second + Directions[directionIndex].second;

    if (nextDiceRow < 0 or nextDiceRow >= maxRow or nextDiceColumn < 0 or nextDiceColumn >= maxColumn)
        return false;

    switch(directionIndex)
    {
        case RIGHT:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[RIGHT];
            SurfaceOfDice[RIGHT] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[LEFT];
            SurfaceOfDice[LEFT] = temp;
            break;
        }

        case LEFT:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[LEFT];
            SurfaceOfDice[LEFT] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[RIGHT];
            SurfaceOfDice[RIGHT] = temp;
            break;
        }

        case FORWARD:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[FORWARD];
            SurfaceOfDice[FORWARD] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[BACK];
            SurfaceOfDice[BACK] = temp;
            break;
        }

        case BACK:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[BACK];
            SurfaceOfDice[BACK] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[FORWARD];
            SurfaceOfDice[FORWARD] = temp;
            break;
        }
    }
 
    dicePosition.first = nextDiceRow;
    dicePosition.second = nextDiceColumn;
    return true;
}

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋™์„ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด, ํ•ด๋‹น ๋งต ๋ฐ”๋‹ฅ์— ์ ํžŒ ์ˆซ์ž์— ๋”ฐ๋ผ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.

  • ์ด๋™ํ•œ ๋งต ์นธ์ด 0์ผ ๊ฒฝ์šฐ : ์ฃผ์‚ฌ์œ„์˜ ์•„๋žซ๋ฉด ์ˆซ์ž๋ฅผ ์ด๋™ํ•œ ๋งต ์นธ์— ๋ณต์‚ฌ
  • ์ด๋™ํ•œ ๋งต ์นธ์ด 0์ด ์•„๋‹ ๊ฒฝ์šฐ : ๋งต ์นธ์˜ ์ˆซ์ž๋ฅผ ์ฃผ์‚ฌ์œ„์˜ ์•„๋žซ๋ฉด์— ๋ณต์‚ฌํ•˜๊ณ , ํ•ด๋‹น ๋งต ์นธ์€ 0์œผ๋กœ ์„ค์ •
void Copy(vector<vector<int>>& map, int row, int column)
{
    if (map[row][column] == 0)
    {
        map[row][column] = SurfaceOfDice[DOWN];
        return;
    }

    SurfaceOfDice[DOWN] = map[row][column];
    map[row][column] = 0;
}

 

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ, ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>

using namespace std;
using Point = pair<int, int>;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);

vector<Point> Directions { {}, {0, 1}, {0, -1}, {-1, 0}, {1, 0} };  // DUMMY, ๋™, ์„œ, ๋ถ, ๋‚จ
vector<int> SurfaceOfDice(6);

enum DIRECTION
{
    TOP,
    RIGHT,
    LEFT,
    FORWARD,
    BACK,
    DOWN,
};

bool Move(Point& dicePosition, const int directionIndex, const int maxRow, const int maxColumn)
{
    int nextDiceRow = dicePosition.first + Directions[directionIndex].first;
    int nextDiceColumn = dicePosition.second + Directions[directionIndex].second;

    if (nextDiceRow < 0 or nextDiceRow >= maxRow or nextDiceColumn < 0 or nextDiceColumn >= maxColumn)
        return false;

    switch(directionIndex)
    {
        case RIGHT:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[RIGHT];
            SurfaceOfDice[RIGHT] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[LEFT];
            SurfaceOfDice[LEFT] = temp;
            break;
        }

        case LEFT:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[LEFT];
            SurfaceOfDice[LEFT] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[RIGHT];
            SurfaceOfDice[RIGHT] = temp;
            break;
        }

        case FORWARD:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[FORWARD];
            SurfaceOfDice[FORWARD] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[BACK];
            SurfaceOfDice[BACK] = temp;
            break;
        }

        case BACK:
        {
            int temp = SurfaceOfDice[DOWN];
            SurfaceOfDice[DOWN] = SurfaceOfDice[BACK];
            SurfaceOfDice[BACK] = SurfaceOfDice[TOP];
            SurfaceOfDice[TOP] = SurfaceOfDice[FORWARD];
            SurfaceOfDice[FORWARD] = temp;
            break;
        }
    }
 
    dicePosition.first = nextDiceRow;
    dicePosition.second = nextDiceColumn;
    return true;
}

void Copy(vector<vector<int>>& map, int row, int column)
{
    if (map[row][column] == 0)
    {
        map[row][column] = SurfaceOfDice[DOWN];
        return;
    }

    SurfaceOfDice[DOWN] = map[row][column];
    map[row][column] = 0;
}

int main()
{
    FAST_IO

    // 1. ์ž…๋ ฅ ๋ฐ›๊ธฐ
    int maxRow, maxColumn, numOfInstruction, directionIndex;
    Point dicePosition;

    cin >> maxRow >> maxColumn >> dicePosition.first >> dicePosition.second >> numOfInstruction;

    vector<vector<int>> map(maxRow, vector<int>(maxColumn));
    for (int row = 0; row < maxRow; row++)
        for (int column = 0; column < maxColumn; column++)
            cin >> map[row][column];

    // 2. ๋ช…๋ น์–ด ์ˆ˜ํ–‰
    while (numOfInstruction--)
    {
        cin >> directionIndex;
        if (Move(dicePosition, directionIndex, maxRow, maxColumn))
        {
            cout << SurfaceOfDice[TOP] << "\n";
            Copy(map, dicePosition.first, dicePosition.second);
        }
    }

    return 0;
}

 

 

728x90
๋ฐ˜์‘ํ˜•