๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 14502๋ฒˆ - ์—ฐ๊ตฌ์†Œ

by dkswnkk 2021. 11. 11.

๋ฌธ์ œ

 

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

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

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;

int N, M, ans; //N=์„ธ๋กœํฌ๊ธฐ, M=๊ฐ€๋กœํฌ๊ธฐ, ans=์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ
int map[9][9];
int cpy[9][9];
int virus[9][9];
int dx[] = { -1,1,0,0 };    //๋™ ์„œ ๋‚จ ๋ถ
int dy[] = { 0,0,1,-1 };
queue<pair<int, int>>q;

void bfs() {
    memcpy(virus, cpy, sizeof(cpy));    //cpy๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฌ๋ฉด ๋‹ค์Œ ๋ฒฝ์กฐํ•ฉ์„ ์„ธ์šธ๋•Œ ์ดˆ๊ธฐํ™”๊ฐ€ ์•ˆ๋˜๋‹ˆ ๋‹ค๋ฅธ ๋ฐฐ์—ด์— ๋ณต์‚ฌ.

    for (int i = 0; i < N; i++) {    //ํ˜„์žฌ ์—ฐ๊ตฌ์‹ค์— ์žˆ๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค
        for (int k = 0; k < M; k++) {
            if (virus[i][k] == 2) q.push({ i,k });
        }
    }

    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        for (int i = 0; i < 4; i++) {    //๋™์„œ๋‚จ๋ถ ํƒ์ƒ‰
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < N && ny >= 0 && ny < M) { //์—ฐ๊ตฌ์‹ค์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š์„๋•Œ    
                if (virus[nx][ny] == 0) {    //๋นˆ์นธ์ด๋ฉด
                    virus[nx][ny] = 2;    //๊ทธ ๋ถ€๋ถ„์€ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ํผ์ง„๋‹ค
                    q.push({ nx,ny });

                }
            }
        }
    }
    int ans_check = 0;
    for (int i = 0; i < N; i++) {    //์•ˆ์ „ ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•œ๋‹ค.
        for (int k = 0; k < M; k++) {
            if (virus[i][k] == 0) ans_check++;
        }
    }
    ans = max(ans, ans_check);    //์•ˆ์ „ ์˜์—ญ ํฌ๊ธฐ์˜ ์ตœ๋Œ“๊ฐ’ ์ €์žฅ.
}


void wall(int cnt) {
    if (cnt == 3) {    //๋ฒฝ์„ ๋‹ค ์„ธ์› ์œผ๋ฉด bfs ํƒ์ƒ‰์„ ํ†ตํ•ด ์•ˆ์ „๊ตฌ์—ญ์˜ ํฌ๊ธฐ๋ฅผ ์ฐพ๋Š”๋‹ค.
        bfs();
    }
    else {
        for (int i = 0; i < N; i++) {    //๋‘๋ฒˆ์งธ ๋ฒฝ๋ถ€ํ„ฐ ์„ธ์šด๋‹ค.
            for (int k = 0; k < M; k++) {
                if (cpy[i][k] == 0) {    //๋นˆ์นธ์„ ๋ฐœ๊ฒฌํ•˜๋ฉด
                    cpy[i][k] = 1;    //๋ฒฝ์„ ์„ธ์šฐ๊ณ 
                    wall(cnt + 1);    //์„ธ๋ฒˆ์งธ ๋ฒฝ์„ ์„ธ์šฐ๊ธฐ ์œ„ํ•ด ์žฌ๊ท€๋ฅผ ํ˜ธ์ถœ.
                    cpy[i][k] = 0;    //๋‘๋ฒˆ์งธ ๋ฒฝ์„ ๋‹ค์Œ ์กฐํ•ฉ์œผ๋กœ ์„ธ์šด๋‹ค.
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M;
    for (int i = 0; i < N; i++) {    //์—ฐ๊ตฌ์‹ค ์ •๋ณด ์ž…๋ ฅ
        for (int k = 0; k < M; k++) {
            cin >> map[i][k];
        }
    }



    for (int i = 0; i < N; i++) {    //๋ฒฝ์„ ์„ธ์šฐ๋Š” ๋ถ€๋ถ„
        for (int k = 0; k < M; k++) {
            if (map[i][k] == 0) {    //๋นˆ์นธ์„ ๋ฐœ๊ฒฌํ•˜๋ฉด
                memcpy(cpy, map, sizeof(map));    //์—ฐ๊ตฌ์‹ค ์ •๋ณด๋ฅผ ๋ณต์‚ฌํ•˜๊ณ 
                cpy[i][k] = 1;    //์ฒซ๋ฒˆ์งธ ๋ฒฝ์„์„ธ์šฐ๊ณ 
                wall(1);    //๋‚˜๋จธ์ง€ ๋‘๊ฐœ์˜ ๋ฒฝ์„ ์žฌ๊ท€๋กœ ์„ธ์šด๋‹ค.
                cpy[i][k] = 0;    //์ฒซ๋ฒˆ์งธ ๋ฒฝ์„ ๋‹ค์Œ ์กฐํ•ฉ์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
            }
        }
    }
    cout << ans;
}

๋Œ“๊ธ€