๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

[BOJ] 3190๋ฒˆ | ๋ฑ€ (C++)

category ๐Ÿค–Algorithm/BOJ 2023. 8. 26. 16:28
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

3190๋ฒˆ: ๋ฑ€

'Dummy' ๋ผ๋Š” ๋„์Šค๊ฒŒ์ž„์ด ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์—๋Š” ๋ฑ€์ด ๋‚˜์™€์„œ ๊ธฐ์–ด๋‹ค๋‹ˆ๋Š”๋ฐ, ์‚ฌ๊ณผ๋ฅผ ๋จน์œผ๋ฉด ๋ฑ€ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค. ๋ฑ€์ด ์ด๋ฆฌ์ €๋ฆฌ ๊ธฐ์–ด๋‹ค๋‹ˆ๋‹ค๊ฐ€ ๋ฒฝ ๋˜๋Š” ์ž๊ธฐ์ž์‹ ์˜ ๋ชธ๊ณผ ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ๋๋‚œ๋‹ค. ๊ฒŒ์ž„

www.acmicpc.net

 

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

๋ฌธ์ œ ๋กœ์ง ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์€๋ฐ, ๊ตฌํ˜„ํ•  ๊ฒŒ ๋งŽ์•„์„œ ์ƒ๋‹นํžˆ ๊ท€์ฐฎ์•˜๋˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์žฅ์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฑ€์˜ ๋จธ๋ฆฌ๊ฐ€ ๋จผ์ € ํ•œ ์นธ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ ์œ„์น˜์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด, ๊ผฌ๋ฆฌ๋„ ํ•œ ์นธ ์ด๋™ํ•˜๊ณ , ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ผฌ๋ฆฌ๋Š” ์ด๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์œ„ ๋ฌธ์žฅ์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด, ๋ฑ€์˜ ๊ผฌ๋ฆฌ๊ฐ€ ์ด๋™ํ•  ๋ฐฉํ–ฅ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฑ€์˜ ๊ผฌ๋ฆฌ๊ฐ€ ์ด๋™ํ•ด์•ผ ํ•  ๋ฐฉํ–ฅ์€ ์ด์ „๊นŒ์ง€ ๋ฑ€์˜ ๋จธ๋ฆฌ๊ฐ€ ์ด๋™ํ–ˆ๋˜ ๋ฐฉํ–ฅ๋“ค์ด๋ฏ€๋กœ, ๋ฑ€์˜ ๋จธ๋ฆฌ๊ฐ€ ์ด๋™ํ–ˆ๋˜ ๊ฒฝ๋กœ๋“ค์ด ์ฐจ๋ก€๋Œ€๋กœ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, Queue๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

ํ•˜์ง€๋งŒ, ๋˜ ํ•˜๋‚˜ ์ค‘์š”ํ•˜๊ฒŒ ๋ด์•ผ ํ•  ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฒŒ์ž„ ์‹œ์ž‘ ์‹œ๊ฐ„์œผ๋กœ๋ถ€ํ„ฐ X์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„, ์™ผ์ชฝ ๋˜๋Š” ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฑ€์ด 90๋„ ๋ฐฉํ–ฅ์„ ํ‹€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๊ฒƒ์€ ์ดˆ๊ธฐ ๊ธฐ๋ณธ ์ด๋™ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ, ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ ์ดˆ๊ธฐ ์ด๋™ ๋ฐฉํ–ฅ์€ ์˜ค๋ฅธ์ชฝ์ž…๋‹ˆ๋‹ค.

๋ฐฉํ–ฅ ์ •๋ณด๋งŒ์„ ๊ด€๋ฆฌํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, (ํ–‰, ์—ด)๋กœ ํ‘œ์‹œํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ„     : (-1, 0)
์•„๋ž˜   : (1, 0)
์™ผ์ชฝ   : (0, -1)
์˜ค๋ฅธ์ชฝ : (0, 1)

 

๊ธฐ๋ณธ ์‹œ์ž‘์ด ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์ด๋ฏ€๋กœ, ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์—์„œ 90๋„๋กœ ํ‹€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์€ ์œ„์ชฝ๊ณผ ์•„๋ž˜์ชฝ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฉํ–ฅ ์ •๋ณด ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// [0]์ƒ, [1]์šฐ, [2]ํ•˜, [3]์ขŒ
vector<Direction> _directions { {-1, 0}, { 0, 1 }, { 1, 0 }, { 0, -1 } };
int directionIdx = 1;  // ์ดˆ๊ธฐ ๊ธฐ๋ณธ ์ด๋™ ๋ฐฉํ–ฅ์€ ์˜ค๋ฅธ์ชฝ์ด๋ฏ€๋กœ 1๋ฒˆ ์ธ๋ฑ์Šค

 

์ด ์ƒํƒœ์—์„œ ๋งŒ์•ฝ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค์— -1์„, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํšŒ์ „ํ•ด์•ผ ํ•œ๋‹ค๋ฉด +1์„ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฌผ๋ก  ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ€ 0์ธ๋ฐ ์™ผ์ชฝ์œผ๋กœ ๊ฐ€์•ผํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰ ์›์†Œ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ€ ๋ฐฐ์—ด ํฌ๊ธฐ๋ฅผ ๋„˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด % ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋Š” ๋ณ„๋„๋กœ ํ•ด์ค˜์•ผ ๊ฒ ์ง€์š”.

 

 

๐Ÿ› ๏ธ์•Œ๊ณ ๋ฆฌ์ฆ˜

์—ฌ๊ธฐ๊นŒ์ง€ ํ–ˆ๋‹ค๋ฉด, ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ํ•ต์‹ฌ ์ฝ”์–ด๋Š” ์™„์„ฑํ•œ ์…ˆ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•˜์—ฌ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋งต์—์„œ ์นธ์„ ๊ตฌ๋ณ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด, ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค.

const int EMPTY = 0;
const int SNAKE = 1;
const int APPLE = 2;

Position _head = Position(0, 0);       // ๋จธ๋ฆฌ ์œ„์น˜
Position _tail = Position(0, 0);       // ๊ผฌ๋ฆฌ ์œ„์น˜
  1. ํ˜„์žฌ ๊ฒŒ์ž„ ์‹œ๊ฐ„์—์„œ ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค๋ฉด, ์ธ๋ฑ์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค.
  2. ๊ฒŒ์ž„ ์ง„ํ–‰ ์‹œ๊ฐ„์„ 1์ดˆ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.
  3. ๋ฑ€์˜ ๋จธ๋ฆฌ๋ฅผ ์ด๋™ ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค.
    • ์ด ๋•Œ, ์ด ์ด๋™ ๋ฐฉํ–ฅ์„ ๋ณ„๋„์˜ Queue์— ์ €์žฅํ•˜์—ฌ, ๋‚˜์ค‘์— ๊ผฌ๋ฆฌ๊ฐ€ ์›€์ง์ผ ๋•Œ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  4. ๋งŒ์•ฝ ๋ฑ€์˜ ๋จธ๋ฆฌ๊ฐ€ ์ด๋™ํ•œ ์œ„์น˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์˜ ๋ชธ์ด๊ฑฐ๋‚˜ ๋ฒฝ์ด๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
  5. ์•„๋‹ˆ๋ผ๋ฉด, ๋จธ๋ฆฌ๊ฐ€ ์ด๋™ํ•œ ์œ„์น˜์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    • ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด, ํ˜„์žฌ ๊ผฌ๋ฆฌ ์œ„์น˜๋Š” EMPTY๋กœ ์ฑ„์šฐ๊ณ , Queue์—์„œ ํ•˜๋‚˜ ๋ฝ‘์•„ ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ ๊ผฌ๋ฆฌ๋ฅผ ํ•œ ์นธ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
    • ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋ชธ์˜ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚˜๋ฏ€๋กœ ์•„๋ฌด๋Ÿฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.
  6. ๋ฑ€์ด ์ด๋™ํ•œ ๋จธ๋ฆฌ ์œ„์น˜ ์นธ์„ SNAKE๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

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

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using Direction = pair<int, int>;

class Board
{
public:
    Board();

    void StartGame();
    int ShowPlayTime() { return _playTime; }
private:
    void CheckTurning();
    void CheckApple();

private:
    struct TurningData
    {
        TurningData() { }
        TurningData(int time, char directionMark) : _time(time), _directionMark(directionMark) { }
        int     _time;
        char    _directionMark;
    };

    struct Position
    {
        Position() { }
        Position(int row, int col) : _row(row), _col(col) { }
        Position operator+(const Direction& other) { return Position(_row + other.first, _col + other.second); }

        int _row;
        int _col;
    };

private:
    vector<vector<int>> _board;
    queue<TurningData>  _turningData;
    queue<Direction>    _tailMoveDirections;

    // (-1, 0):์ƒ, (0, 1):์šฐ, (1, 0):ํ•˜, (0, -1):์ขŒ
    vector<Direction> _directions { {-1, 0}, { 0, 1 }, { 1, 0 }, { 0, -1 } };
private:
    int _directionIdx = 1;
    int _playTime = 0;
    int _N;

    Position _head = Position(0, 0);       // ๋จธ๋ฆฌ ์œ„์น˜
    Position _tail = Position(0, 0);       // ๊ผฌ๋ฆฌ ์œ„์น˜
private:
    const int EMPTY = 0;
    const int SNAKE = 1;
    const int APPLE = 2;
};

Board::Board()
{
    int N, appleCount, turningInformations;
    cin >> N >> appleCount;

    _N = N;

    // 1. N x N ๋ณด๋“œํŒ ๋งŒ๋“ค๊ธฐ
    _board.resize(N);
    for (int i = 0; i < N; i++)
        _board[i].resize(N);

    _board[0][0] = SNAKE;  // ๋ฑ€ ์ฒ˜์Œ ์œ„์น˜ ๋“ฑ๋ก

    // 2. ๋งต์— ์‚ฌ๊ณผ ์ฑ„์šฐ๊ธฐ
    for (int i = 0; i < appleCount; i++)
    {
        int row, col;
        cin >> row >> col;
        _board[row - 1][col - 1] = APPLE;
    }

    // 3. ๋ฐฉํ–ฅ ์ „ํ™˜ ํฌ์ธํŠธ ์ €์žฅํ•˜๊ธฐ
    cin >> turningInformations;
    for (int i = 0; i < turningInformations; i++)
    {
        int time;
        char mark;
        cin >> time >> mark;
        _turningData.emplace(TurningData(time, mark));
    }
}

void Board::CheckTurning()
{
    if (!_turningData.empty())
    {
        TurningData data = _turningData.front();

        if (data._time == _playTime)
        {
            _turningData.pop();
            int addIdx = (data._directionMark == 'L') ? -1 : 1;

            if (_directionIdx == 0 and addIdx == -1)
            {
                _directionIdx = _directions.size() - 1;
                return;
            }

            _directionIdx = (_directionIdx + addIdx) % _directions.size();
        }
    }
}

void Board::CheckApple()
{
    if (_tailMoveDirections.empty())
        return;

    // ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด, ๊ผฌ๋ฆฌ ์ด๋™
    if (_board[_head._row][_head._col] != APPLE)
    {
        Direction next = _tailMoveDirections.front();
        _tailMoveDirections.pop();

        _board[_tail._row][_tail._col] = EMPTY;
        _tail = _tail + next;
    }
}

void Board::StartGame()
{
    while (true)
    {
        // 1. ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š”์ง€ ํ™•์ธ
        CheckTurning();
        _playTime++;

        _head = _head + _directions[_directionIdx];
        _tailMoveDirections.emplace(_directions[_directionIdx]);

        // 2. ๋ฒฝ์— ๋ถ€๋”ชํ˜”๋‹ค๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ
        if (_head._row < 0 or _head._row >= _N or _head._col < 0 or _head._col >= _N)
            break;

        // 3. ๋จธ๋ฆฌ๊ฐ€ ์ž๊ธฐ ๋ชธ๊ณผ ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ
        if (_board[_head._row][_head._col] == SNAKE)
            break;

        // 4. ์‚ฌ๊ณผ ์—ฌ๋ถ€ ํ™•์ธ
        CheckApple();

        _board[_head._row][_head._col] = SNAKE;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(NULL); cin.tie(NULL);

    Board gameBoard;
    gameBoard.StartGame();
    cout << gameBoard.ShowPlayTime();
    return 0;
}

 

 

728x90
๋ฐ˜์‘ํ˜•