[BOJ] 14502๋ฒ | ์ฐ๊ตฌ์ (C++)
2023. 12. 13. 16:45ใCoding Test/BOJ
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
14502๋ฒ: ์ฐ๊ตฌ์
์ธ์ฒด์ ์น๋ช ์ ์ธ ๋ฐ์ด๋ฌ์ค๋ฅผ ์ฐ๊ตฌํ๋ ์ฐ๊ตฌ์์์ ๋ฐ์ด๋ฌ์ค๊ฐ ์ ์ถ๋์๋ค. ๋คํํ ๋ฐ์ด๋ฌ์ค๋ ์์ง ํผ์ง์ง ์์๊ณ , ๋ฐ์ด๋ฌ์ค์ ํ์ฐ์ ๋ง๊ธฐ ์ํด์ ์ฐ๊ตฌ์์ ๋ฒฝ์ ์ธ์ฐ๋ ค๊ณ ํ๋ค. ์ฐ๊ตฌ์๋ ํฌ
www.acmicpc.net
๐ง๐ปํ์ด ๊ณผ์
์ฐ๊ตฌ์์ ํฌ๊ธฐ๊ฐ ์ต๋ \(8 \times 8\) ์ด๊ธฐ ๋๋ฌธ์ ๋ฒฝ์ 3๊ฐ ์ค์นํ๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์์ ๋ํด ํ์ํ์ฌ ํ์์ต๋๋ค. ๋ฐฑํธ๋ํน๊ณผ BFS ํ์์ ์ ๊ตฌํํ๋ ๊ฒ์ด ๋ชฉํ์ธ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค์.
- 2์ฐจ์ ๋ฐฐ์ด์ ์ฐ๊ตฌ์ ์ ๋ณด๋ฅผ ์ ๋ ฅ ๋ฐ๊ณ , ๋ฐ์ด๋ฌ์ค์ ์์น ์ ๋ณด๋ ๋ณ๋์ ๋ฐฐ์ด์ ์ ์ฅํ๋ค.
- ์กฐํฉ(Combination) ๋ฐฉ์์ ์ด์ฉํด ์ด ๋ฐฉํฅ ์์๋ก ๋ฒฝ 3๊ฐ๋ฅผ ์ค์นํ๋ค.
- ๋น ๊ณต๊ฐ์ผ ๋์๋ง ๋ฒฝ์ ์ค์นํ๋ค.
- ๋ฒฝ์ 3๊ฐ ์ค์น ๋ค ํ๋ค๋ฉด, 3๋ฒ ๊ณผ์ ์ ์งํ ํ ๋ฐฑํธ๋ํน์ ํตํด ๋๋์๊ฐ๋ค.
- ๋ฒฝ 3๊ฐ๊ฐ ๋ค ์ค์น๋๋ค๋ฉด, BFS ํ์์ ํตํด ๋ฐ์ด๋ฌ์ค ์ ์ ์๋ฎฌ๋ ์ด์
์ ์์ํ๋ค.
- ์ด ๋ ์คํ(Stack)์ ๋ณ๋๋ก ๋์ด, ์ฌ๊ธฐ์ ๋ฐ์ด๋ฌ์ค๊ฐ ๋ค๋ ๊ฐ๋ ์์น๋ค์ ๋ฃ์ด๋๋ค.
- ๋ฐ์ด๋ฌ์ค ์ ์ ์๋ฎฌ๋ ์ด์ ์ด ๋๋ฌ๋ค๋ฉด, ์ฐ๊ตฌ์ ์ ์ฒด๋ฅผ ์ํํ๋ฉฐ ์์ ์ง์ญ ๊ฐ์๋ฅผ ์ฒดํฌํ๋ค.
- ์์ ์ง์ญ ๊ฐ์ ์ฒดํฌ๊ฐ ๋๋ฌ๋ค๋ฉด, ์คํ์ด ๋น ๋๊น์ง pop() ํ๋ฉฐ ๋ฐ์ด๋ฌ์ค๊ฐ ์ ์ํ ๊ณต๊ฐ์ ๋ค์ ๋น ๊ณต๊ฐ์ผ๋ก ๋๋๋ฆฐ๋ค.
- ์์ ์ง์ญ ๊ฐ์๋ฅผ ์ ์ฅํ๋ ๋ณ์์ ๋ํด, ๋ ํฐ ๊ฐ์ผ๋ก ๊ฐฑ์ ํ๋ค.
- 2 ~ 3๋ฒ ๊ณผ์ ์ ์ฐ๊ตฌ์ ์ ์ฒด ๋งต์ ๋ํด ์งํํ๋ค.
โ๏ธ ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
using Matrix = vector<vector<int>>;
using Position = pair<int, int>;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define EMPTY 0
#define WALL 1
#define VIRUS 2
#define MAX_WALL_COUNT 3
vector<vector<int>> Directions { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
vector<Position> VirusPositions;
int N, M;
int GetSafeArea(const Matrix& field) {
int area = 0;
for (const auto& line : field) {
for (const auto& element : line) {
if (element == EMPTY)
area++;
}
}
return area;
}
int BFS(Matrix& field) {
queue<Position> positions;
for (const auto& virusPos : VirusPositions)
positions.emplace(virusPos.first, virusPos.second);
stack<Position> back;
while (!positions.empty()) {
int currentRow = positions.front().first;
int currentCol = positions.front().second;
positions.pop();
for (const auto& direction : Directions) {
int nextRow = currentRow + direction[0];
int nextCol = currentCol + direction[1];
if (nextRow < 0 or nextRow >= N or nextCol < 0 or nextCol >= M)
continue;
if (field[nextRow][nextCol] != EMPTY)
continue;
field[nextRow][nextCol] = VIRUS;
positions.emplace(nextRow, nextCol);
back.emplace(nextRow, nextCol);
}
}
int safeArea = GetSafeArea(field);
while (!back.empty()) {
int row = back.top().first;
int col = back.top().second;
back.pop();
field[row][col] = EMPTY;
}
return safeArea;
}
void BackTracking(Matrix& field, vector<Position>& newWalls, int& safeArea, int row, int col) {
if (newWalls.size() == MAX_WALL_COUNT) {
int newResult = BFS(field);
safeArea = max(safeArea, newResult);
return;
}
int c;
for (int r = row; r < N; r++) {
// ์ด์ด ๋ฐ๋๋ฉด ๋ค์ ์ด ์ด๊ธฐํ
c = (c >= M) ? 0 : col;
for (; c < M; c++) {
if (field[r][c] == WALL or field[r][c] == VIRUS)
continue;
field[r][c] = WALL;
newWalls.emplace_back(r, c);
BackTracking(field, newWalls, safeArea, r, c + 1);
field[r][c] = EMPTY;
newWalls.pop_back();
}
}
}
int StartSimulation(Matrix& field) {
int safeArea = 0;
vector<Position> newWallPositions;
BackTracking(field, newWallPositions, safeArea, 0, 0);
return safeArea;
}
int main() {
FAST_IO
cin >> N >> M;
Matrix field(N, vector<int>(M));
for (int row = 0; row < N; row++) {
for (int col = 0; col < M; col++) {
cin >> field[row][col];
if (field[row][col] == VIRUS)
VirusPositions.emplace_back(row, col);
}
}
int answer = StartSimulation(field);
cout << answer;
return 0;
}
728x90
๋ฐ์ํ
'Coding Test > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 1916๋ฒ | ์ต์๋น์ฉ ๊ตฌํ๊ธฐ (C++) (0) | 2023.12.13 |
---|---|
[BOJ] 1987๋ฒ | ์ํ๋ฒณ (C++) (0) | 2023.12.13 |
[BOJ] 17472๋ฒ | ๋ค๋ฆฌ ๋ง๋ค๊ธฐ 2 (C++) (0) | 2023.12.12 |
[BOJ] 13549๋ฒ | ์จ๋ฐ๊ผญ์ง 3 (C++) (1) | 2023.12.06 |
[BOJ] 16928๋ฒ | ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์ (C++) (0) | 2023.12.01 |