[BOJ] 3055๋ฒˆ | ํƒˆ์ถœ (C++)

2024. 1. 31. 17:43ใ†Coding Test/BOJ

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

3055๋ฒˆ: ํƒˆ์ถœ

์‚ฌ์•…ํ•œ ์•”ํ‘์˜ ๊ตฐ์ฃผ ์ด๋ฏผํ˜์€ ๋“œ๋””์–ด ๋งˆ๋ฒ• ๊ตฌ์Šฌ์„ ์†์— ๋„ฃ์—ˆ๊ณ , ๊ทธ ๋Šฅ๋ ฅ์„ ์‹คํ—˜ํ•ด๋ณด๊ธฐ ์œ„ํ•ด ๊ทผ์ฒ˜์˜ ํ‹ฐ๋–ฑ์ˆฒ์— ํ™์ˆ˜๋ฅผ ์ผ์œผํ‚ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด ์ˆฒ์—๋Š” ๊ณ ์Šด๋„์น˜๊ฐ€ ํ•œ ๋งˆ๋ฆฌ ์‚ด๊ณ  ์žˆ๋‹ค. ๊ณ ์Šด๋„์น˜๋Š” ์ œ

www.acmicpc.net

 

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

 

BFS๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ 17%์—์„œ ์ž๊พธ ํ‹€๋ ธ๋Š”๋ฐ, ๋ฐ˜๋ก€๋ฅผ ๋„๋ฌด์ง€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋‹ค๋ฅธ ๋ถ„๋“ค ์ฝ”๋“œ๋ฅผ ์•ฝ๊ฐ„ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ ํ•ด๋ณด๋‹ˆ, ์ œ๊ฐ€ ๊ฐ„๊ณผํ•œ ์‚ฌ์‹ค๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

  1. ๊ณ ์Šด๋„์น˜์˜ ์‹œ์ž‘ ์œ„์น˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜ ์žˆ๋‹ค.
  2. ๋งค ๋ถ„๋งˆ๋‹ค ์ฃผ์–ด์ง„ ๋ฌผ์˜ ์œ„์น˜๋“ค์„ ๋ชจ๋‘ ํ™•์žฅํ•˜์ง€ ์•Š๊ณ , ํ•˜๋‚˜๋งŒ ํ™•์žฅํ–ˆ๋‹ค.

 

์ด ๋ถ€๋ถ„๋“ค๋งŒ ๊ณ ์ณ์„œ ๋‹ค์‹œ ์ œ์ถœํ•˜๋‹ˆ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์—๋Š” ์กฐ๊ธˆ ๋” ์‹ ๊ฒฝ์จ์•ผ๊ฒ ๋„ค์š”.

 

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

#include <iostream>
#include <vector>
#include <string>
#include <queue>

using namespace std;
using Point = pair<int, int>;

#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define START 'S'
#define DESTINATION 'D'
#define WATER '*'
#define EMPTY '.'
#define STONE 'X'
#define VISIT 'O'
#define INVAILD -1

int MaxRow, MaxColumn;
vector<Point> Directions { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

void RiseWater(vector<string>& field, queue<Point>& waterPositions)
{
    int waterCount = waterPositions.size();

    while (waterCount--)
    {
        Point waterPosition = waterPositions.front();
        waterPositions.pop();

        for (const Point& direction : Directions)
        {
            int nextRow = waterPosition.first + direction.first;
            int nextCol = waterPosition.second + direction.second;

            if (nextRow < 0 or nextRow >= MaxRow or nextCol < 0 or nextCol >= MaxColumn)
                continue;
            
            bool isStone = (field[nextRow][nextCol] == STONE);
            bool isDestination = (field[nextRow][nextCol] == DESTINATION);
            bool isWater = (field[nextRow][nextCol] == WATER);

            if (isStone or isDestination or isWater)
                continue;
                
            field[nextRow][nextCol] = WATER;
            waterPositions.emplace(Point(nextRow, nextCol));
        }
    }

}

bool MoveHedgeHog(vector<string>& field, queue<Point>& hedgehogPositions, int& time)
{
    int hedgehogCount = hedgehogPositions.size();

    while (hedgehogCount--)
    {
        Point hedgehogPoisition = hedgehogPositions.front();
        hedgehogPositions.pop();

        for (const Point& direction : Directions)
        {
            int nextRow = hedgehogPoisition.first + direction.first;
            int nextCol = hedgehogPoisition.second + direction.second;

            if (nextRow < 0 or nextRow >= MaxRow or nextCol < 0 or nextCol >= MaxColumn)
                continue;
            
            if (field[nextRow][nextCol] == DESTINATION)
            {
                time++;
                return true;
            }

            bool isStone = (field[nextRow][nextCol] == STONE);
            bool isWater = (field[nextRow][nextCol] == WATER);
            bool isVisit = (field[nextRow][nextCol] == VISIT);

            if (isStone or isWater or isVisit)
                continue;
            
            field[nextRow][nextCol] = VISIT;
            hedgehogPositions.emplace(Point(nextRow, nextCol));
        }
    }

    return false;
}

int FindTimeToCave(vector<string>& field, queue<Point>& waterPositions, queue<Point>& hedgehogPositions)
{
    bool isAlive = false;
    int arrivalTime = 0;

    while (!hedgehogPositions.empty())
    {
        // 1. ๋ฌผ ์ƒ์Šน
        RiseWater(field, waterPositions);

        // 2. ๊ณ ์Šด๋„์น˜ ์ด๋™
        if (isAlive = MoveHedgeHog(field, hedgehogPositions, arrivalTime))
            break;
        arrivalTime++;     
    }

    return isAlive ? arrivalTime : INVAILD;
}

void Initialize(vector<string>& field, queue<Point>& waterPositions, queue<Point>& hedgehogPositions)
{
    for (int row = 0; row < MaxRow; row++)
    {
        cin >> field[row];

        for (int column = 0; column < MaxColumn; column++)
        {
            if (field[row][column] == START)
            {
                hedgehogPositions.emplace(Point(row, column));
                field[row][column] = VISIT;
            }

            else if (field[row][column] == WATER)
                waterPositions.emplace(Point(row, column));
        }
    }
}

int main()
{
    FAST_IO

    // 1. ์ดˆ๊ธฐํ™”
    cin >> MaxRow >> MaxColumn;

    vector<string> field(MaxRow);
    queue<Point> waterPositions;
    queue<Point> hedgehogPositions;

    Initialize(field, waterPositions, hedgehogPositions);
    
    // 2. BFS ํƒ์ƒ‰ ์‹œ์ž‘
    int time = FindTimeToCave(field, waterPositions, hedgehogPositions);

    if (time == INVAILD)
        cout << "KAKTUS";
    else
        cout << time;
    return 0;
}

 

 

 

728x90
๋ฐ˜์‘ํ˜•