[BOJ / C++] #14502 ์—ฐ๊ตฌ์†Œ ๐ŸŒŸ

Inryuยท2021๋…„ 8์›” 14์ผ
0

Problem Solving

๋ชฉ๋ก ๋ณด๊ธฐ
24/51
post-thumbnail

๐Ÿฆ  https://www.acmicpc.net/problem/14502


๐Ÿฆ  ๋ฌธ์ œ ํ’€์ด

DFS(์กฐํ•ฉ), BFS๊ฐ€ ๋ชจ๋‘ ์“ฐ์ด๋Š” ๋ฌธ์ œ๋‹ค.

  1. ์ž…๋ ฅ ๋ฐ›๊ธฐ -> ๋ฐ›์œผ๋ฉด์„œ ๋นˆ ์นธ ์ขŒํ‘œ vector<pair<int,int>>์— ์ €์žฅํ•˜๊ธฐ

  2. ๋ฒฝ 3๊ฐœ ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ (DFS)
    makeWall(0,0)
    ๋ฒฝ 3๊ฐœ์˜ ์กฐํ•ฉ์„ ๊ตฌํ•˜๋Š” DFSํ•จ์ˆ˜์ด๋‹ค.
    ์ €์žฅํ•ด๋‘” ๋นˆ ์นธ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด ์กฐํ•ฉ์„ ๊ตฌํ•œ๋‹ค.
    ๊ตฌํ•œ ์ขŒํ‘œ๋“ค์˜ ์กฐํ•ฉ์€ ๋ฐฐ์—ด์— ์ €์žฅํ•ด๋‘”๋‹ค.
    3๊ฐœ์˜ ์กฐํ•ฉ์„ ๊ตฌํ–ˆ์„ ๋•Œ, ๋ฐ”์ด๋Ÿฌ์Šค ํผํŠธ๋ฆฌ๊ธฐ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ์•ˆ์ „์ง€๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

  3. ๋ฐ”์ด๋Ÿฌ์Šค ํผํŠธ๋ฆฌ๊ธฐ (BFS)
    ๊ธฐ์กด ๋งต์„ tmpMap์— ์นดํ”ผํ•œ ํ›„,
    ๊ณ ๋ฅธ 3๊ฐœ์˜ ๋ฒฝ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด ๊ทธ ๊ณณ์„ 1๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. (์‹ค์ œ๋กœ ๋ฒฝ ์„ธ์šฐ๊ธฐ)
    tmpMap์˜ ๋ฐ”์ด๋Ÿฌ์Šค ์ขŒํ‘œ๋“ค์„ ๋ชจ๋‘ ํ์— ๋„ฃ์€ ํ›„
    ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฐ๋‹ค.

  4. ์•ˆ์ „ ์˜์—ญ ๊ตฌํ•˜๊ธฐ
    tmpMap==0์ธ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ ,
    maxVal์„ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค.

- ์—ฌ๋Ÿฌ๋ฒˆ ์‹คํ–‰๋˜๋Š” BFS ํ•จ์ˆ˜์ด๋ฏ€๋กœ, visited ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ธฐ.
- BFS์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์—ฌ๋Ÿฌ๊ณณ์—์„œ ๋™์‹œ์— ํผ์ง€๋ฏ€๋กœ ์ฒ˜์Œ์— ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค ์ขŒํ‘œ๋ฅผ ํ์— ๋„ฃ์–ด์ฃผ๊ธฐ  
- ์กฐํ•ฉ ๊ตฌํ•  ๋•Œ startIdx for๋ฌธ ์‹ค์ˆ˜ ์กฐ์‹ฌ..!!!

๐Ÿฆ  ์ฝ”๋“œ

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

int N,M;
int map[10][10];
int tmpMap[10][10];
bool visited[10][10]; //BFS ์šฉ
int dr[4]={-1,0,1,0};
int dc[4]={0,1,0,-1};
int maxVal=-1;

vector<pair<int,int>> threeWalls(3); //๊ณ ๋ฅธ 3๊ฐœ์˜ ๋ฒฝ
vector<pair<int,int>> emptySpace;

void copyMap(){
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            tmpMap[i][j]=map[i][j];
        }
    }

}

//3. ์•ˆ์ „ ์˜์—ญ ๊ตฌํ•˜๊ธฐ
void countSafeArea(){
    int cnt=0;
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            if(tmpMap[i][j]==0) cnt++;
        }
    }
    maxVal=max(cnt,maxVal);
}

//2. ๋ฐ”์ด๋Ÿฌ์Šค ํผํŠธ๋ฆฌ๊ธฐ (BFS)
void spreadVirus(){
    copyMap();

    //๋ฒฝ 3๊ฐœ ๊ณ ๋ฅธ ๊ฑฐ ์„ธ์›Œ์ฃผ๊ธฐ
    for(int i=0;i<3;i++){
        int r=threeWalls[i].first;
        int c=threeWalls[i].second;
        tmpMap[r][c]=1;
    }

    memset(visited,false,sizeof(visited));
    queue<pair<int,int>> q;

    //ํผ์งˆ ๋ฐ”์ด๋Ÿฌ์Šค๋“ค ๋„ฃ์–ด์ฃผ๊ธฐ.
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            if(tmpMap[i][j]==2){
                q.push({i,j});
                 visited[i][j]=true;
            }
        }
    }

    //ํผํŠธ๋ฆฌ๊ธฐ
    while(!q.empty()){
        int r=q.front().first;
        int c=q.front().second;
        q.pop();
        tmpMap[r][c]=2;

        for(int d=0;d<4;d++){
            int nr=r+dr[d];
            int nc=c+dc[d];

            if(nr<0||nr>N-1||nc<0||nc>M-1||visited[nr][nc]||tmpMap[nr][nc]!=0) continue;

            q.push({nr,nc});
            visited[nr][nc]=true;
        }
    }
}

//1. ๋ฒฝ 3๊ฐœ ๊ณ ๋ฅด๊ธฐ (emptySpace์—์„œ 3๊ฐœ ๊ตฌํ•ด์•ผํ•จ) DFS
void makeWall(int startIdx, int L){
    if(L==3) { //3๊ฐœ ์กฐํ•ฉ ๊ตฌํ•œ ๊ฒฝ์šฐ
        spreadVirus();
        countSafeArea();
        return;
    }

    // ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ
    for(int i=startIdx;i<emptySpace.size();i++){
        int r=emptySpace[i].first; //๐Ÿฅฒ์ธ๋ฑ์Šค์— startIdx ๊ทธ๋ƒฅ ๋„ฃ๋Š” ์‹ค์ˆ˜๐Ÿฅฒ
        int c=emptySpace[i].second; //๐Ÿฅฒ์ธ๋ฑ์Šค์— startIdx ๊ทธ๋ƒฅ ๋„ฃ๋Š” ์‹ค์ˆ˜๐Ÿฅฒ
        threeWalls[L]=make_pair(r,c); //์กฐํ•ฉ ์ €์žฅ
        makeWall(i+1, L+1); //๐Ÿฅฒ์ธ๋ฑ์Šค์— startIdx ๊ทธ๋ƒฅ ๋„ฃ๋Š” ์‹ค์ˆ˜๐Ÿฅฒ
    }
}

int main() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            int val;
            cin >> val;
            map[i][j] = val;
            if (val == 0) emptySpace.push_back({i, j});
        }
    }

    //์กฐํ•ฉ ์‚ฌ์šฉํ•ด์„œ emptySpace์—์„œ 3๊ฐœ ๊ณจ๋ผ์•ผ ํ•จ.
    makeWall(0, 0);
    cout << maxVal << "\n";

}
profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€