[BOJ] 14502๋ฒˆ | ์—ฐ๊ตฌ์†Œ (C++)

2023. 12. 13. 16:45ใ†Coding Test/BOJ

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

14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ

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

www.acmicpc.net

 

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

 

์—ฐ๊ตฌ์†Œ์˜ ํฌ๊ธฐ๊ฐ€ ์ตœ๋Œ€ \(8 \times 8\) ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฒฝ์„ 3๊ฐœ ์„ค์น˜ํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ํƒ์ƒ‰ํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ BFS ํƒ์ƒ‰์„ ์ž˜ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ธ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋„ค์š”.

 

  1. 2์ฐจ์› ๋ฐฐ์—ด์— ์—ฐ๊ตฌ์†Œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜ ์ •๋ณด๋„ ๋ณ„๋„์˜ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
  2. ์กฐํ•ฉ(Combination) ๋ฐฉ์‹์„ ์ด์šฉํ•ด ์—ด ๋ฐฉํ–ฅ ์ˆœ์„œ๋กœ ๋ฒฝ 3๊ฐœ๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
    • ๋นˆ ๊ณต๊ฐ„์ผ ๋•Œ์—๋งŒ ๋ฒฝ์„ ์„ค์น˜ํ•œ๋‹ค.
    • ๋ฒฝ์„ 3๊ฐœ ์„ค์น˜ ๋‹ค ํ–ˆ๋‹ค๋ฉด, 3๋ฒˆ ๊ณผ์ •์„ ์ง„ํ–‰ ํ›„ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ†ตํ•ด ๋˜๋Œ์•„๊ฐ„๋‹ค.
  3. ๋ฒฝ 3๊ฐœ๊ฐ€ ๋‹ค ์„ค์น˜๋๋‹ค๋ฉด, BFS ํƒ์ƒ‰์„ ํ†ตํ•ด ๋ฐ”์ด๋Ÿฌ์Šค ์ž ์‹ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์‹œ์ž‘ํ•œ๋‹ค.
    • ์ด ๋•Œ ์Šคํƒ(Stack)์„ ๋ณ„๋„๋กœ ๋‘์–ด, ์—ฌ๊ธฐ์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋‹ค๋…€๊ฐ”๋˜ ์œ„์น˜๋“ค์„ ๋„ฃ์–ด๋‘”๋‹ค.
    • ๋ฐ”์ด๋Ÿฌ์Šค ์ž ์‹ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์ด ๋๋‚ฌ๋‹ค๋ฉด, ์—ฐ๊ตฌ์†Œ ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ์•ˆ์ „ ์ง€์—ญ ๊ฐœ์ˆ˜๋ฅผ ์ฒดํฌํ•œ๋‹ค.
    • ์•ˆ์ „ ์ง€์—ญ ๊ฐœ์ˆ˜ ์ฒดํฌ๊ฐ€ ๋๋‚ฌ๋‹ค๋ฉด, ์Šคํƒ์ด ๋นŒ ๋•Œ๊นŒ์ง€ pop() ํ•˜๋ฉฐ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ž ์‹ํ•œ ๊ณต๊ฐ„์„ ๋‹ค์‹œ ๋นˆ ๊ณต๊ฐ„์œผ๋กœ ๋˜๋Œ๋ฆฐ๋‹ค.
    • ์•ˆ์ „ ์ง€์—ญ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์— ๋Œ€ํ•ด, ๋” ํฐ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.
  4. 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
๋ฐ˜์‘ํ˜•