[BOJ] 3055๋ฒ | ํ์ถ (C++)
2024. 1. 31. 17:43ใCoding Test/BOJ
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
BFS๋ก ์ฝ๋๋ฅผ ์์ฑํ์ง๋ง 17%์์ ์๊พธ ํ๋ ธ๋๋ฐ, ๋ฐ๋ก๋ฅผ ๋๋ฌด์ง ๋ชจ๋ฅด๊ฒ ์ด์ ๋ค๋ฅธ ๋ถ๋ค ์ฝ๋๋ฅผ ์ฝ๊ฐ ์ฐธ๊ณ ํ์ต๋๋ค.
์ฐธ๊ณ ํด๋ณด๋, ์ ๊ฐ ๊ฐ๊ณผํ ์ฌ์ค๋ค์ด ์์์ต๋๋ค.
- ๊ณ ์ด๋์น์ ์์ ์์น๋ ์ฌ๋ฌ ๊ฐ์ผ ์ ์๋ค.
- ๋งค ๋ถ๋ง๋ค ์ฃผ์ด์ง ๋ฌผ์ ์์น๋ค์ ๋ชจ๋ ํ์ฅํ์ง ์๊ณ , ํ๋๋ง ํ์ฅํ๋ค.
์ด ๋ถ๋ถ๋ค๋ง ๊ณ ์ณ์ ๋ค์ ์ ์ถํ๋ ํต๊ณผํ ์ ์์์ต๋๋ค. ๋ค์์๋ ์กฐ๊ธ ๋ ์ ๊ฒฝ์จ์ผ๊ฒ ๋ค์.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#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
๋ฐ์ํ
'Coding Test > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 13904๋ฒ | ๊ณผ์ (C++) (1) | 2024.02.06 |
---|---|
[BOJ] 2638๋ฒ | ์น์ฆ (C++) (0) | 2024.02.01 |
[BOJ] 2357๋ฒ | ์ต์๊ฐ๊ณผ ์ต๋๊ฐ (C++) (1) | 2024.01.24 |
[BOJ] 2637๋ฒ | ์ฅ๋๊ฐ ์กฐ๋ฆฝ (C++) (0) | 2023.12.21 |
[BOJ] 11779๋ฒ | ์ต์๋น์ฉ ๊ตฌํ๊ธฐ 2 (C++) (1) | 2023.12.20 |