๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 14502 ์—ฐ๊ตฌ์†Œ bfs

by Jouureee 2021. 4. 1.

๋ฌธ์ œ : 

www.acmicpc.net/problem/14502

 

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

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

www.acmicpc.net

 

๋ถ„์„ :

 

c++ ์ฝ”๋“œ :

//
//  14502_lab.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/07.
//

#include <stdio.h>
#include <iostream>
#include <utility>
#include <vector>
#include <queue>
#include <math.h>
#include <string.h>

using namespace std;
int arr[8][8];
int c_arr[8][8];
int N, M;
int zero_cnt;
int ans;
bool check[64]; //๋ฒฝ(1)์„ ๋งŒ๋“ค์–ด ๋ดค๋Š”์ง€ ์•„๋‹Œ์ง€ ๊ฒ€์‚ฌ
bool visit[8][8]; //๋ฐ”์ด๋Ÿฌ์Šค(2)๋ฅผ ๋งŒ๋“ค์–ด ๋ดค๋Š”์ง€ ์•„๋‹Œ์ง€ ๊ฒ€์‚ฌ

vector<pair<int, int>> virusArr;
vector<pair<int, int>> emptyArr;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };


void make_map(){
    cin >> N >> M;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            cin >> arr[i][j];
            //cout << arr[i][j] << " ";
            if(arr[i][j] == 0){//๋นˆ์นธ
                emptyArr.push_back(make_pair(i, j)); // 0 ์ขŒํ‘œ
            }else if(arr[i][j] == 2){//๋ฐ”์ด๋Ÿฌ์Šค
                virusArr.push_back(make_pair(i,j)); // 2 ์ขŒ์ตธ
            }
        }
    }
    zero_cnt = (int)emptyArr.size();
    //cout << "zero_cnt : " << zero_cnt << endl;
}

void make_cmap(){
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            c_arr[i][j] = arr[i][j];
        }
    }
}

void make_BFS(int x, int y){
    queue<pair<int, int>> virusQ;
    virusQ.push(make_pair(x, y)); // ์ฒ˜์Œ ์œ„์น˜์˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์ง‘์–ด ๋„ฃ์Œ
    visit[x][y] = true;
    while(virusQ.empty() == 0){ //not empty ์ผ ๊ฒฝ์šฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 2๋กœ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•จ
        int x = virusQ.front().first;
        int y = virusQ.front().second;
        virusQ.pop();
        
        for(int i = 0; i < 4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if(nx >= 0 && ny >= 0 && nx < N && ny < M){
                if(visit[nx][ny] == false && c_arr[nx][ny] == 0){ // ๋ฐฉ๋ฌธ x์ด๊ณ  0(๋นˆ์นธ)์ด๋ผ๋ฉด
                    c_arr[nx][ny] = 2; // ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ๋„ฃ์–ด์คŒ
                    visit[nx][ny] = true;
                    virusQ.push(make_pair(nx, ny));
                }
            }
        }
        
    }
    
}

int safe_area(){
    int cnt = 0;
    for(int i =0; i <N; i++){
        for(int j = 0; j <M; j++){
            if(c_arr[i][j] == 0) cnt++;
        }
    }
    return cnt;
}


void bfs_mk_virus(){
    int count = 0;
    make_cmap();
    memset(visit, false, sizeof(visit));//visit ๋ฐฐ์—ด์„ false๋กœ ๋ชจ๋‘ ์ดˆ๊ธฐํ™”
    
    //์ดˆ๊ธฐ ์ž‘์—… - ๋ฒฝ 1์„ ์„ธ์›Œ์คŒ
    for(int i = 0; i< zero_cnt; i++){
        if(count == 3) {
            //cout << "get break" << endl;
            break;
        }
        if(check[i] == true){ //1์ด ์žˆ๋Š” ๊ณณ์ด๋ผ๋ฉด
            int x = emptyArr[i].first;
            int y = emptyArr[i].second;
            c_arr[x][y] = 1;// c_arr ์— 1 ์„ธ์›€
            count ++;
        }
    }
    
    for(int i = 0; i < (int)virusArr.size(); i++){ // ๋ฐ”์ด๋Ÿฌ์Šค ๊ฐฏ์ˆ˜๋งŒํผ
        int x = virusArr[i].first;
        int y = virusArr[i].second;
        make_BFS(x, y); //๋ฐ”์ด๋Ÿฌ์Šค ์žˆ๋Š” ์œ„์น˜์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฐ๋‹ค.
    }
    
    int temp_ans = safe_area();
    ans = max(ans, temp_ans);
    
}

void dfs_mk_wall(int index, int count){ // brute force
    if(count == 3){// ๋ฒฝ 3๊ฐœ๋ฅผ ๋‹ค ๋งŒ๋“ค๋ฉด
        bfs_mk_virus();
        //cout << "made wall " << index << endl;
        return;
    }
    for(int i = index; i < zero_cnt; i++){// 0์ด ์žˆ๋Š” ๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ๋ฒฝ(1)์„ ์„ธ์šด๋‹ค.
        if(check[i] == true){ //๋ฒฝ์„ ์„ธ์› ๋˜ ๊ณณ์ด๋ผ๋ฉด
            continue; // ๊ฑ ๋„˜๊น€
        }
        check[i] = true;
        dfs_mk_wall(i, count + 1); // dfs๋กœ ๋ฒฝ์„ ์„ธ์šด๋‹ค
        check[i] = false; // ๋ง‰ํ˜”์„ ๊ฒฝ์šฐ ๋˜๋Œ์•„ ์˜จ๋‹ค.
    }
}


int main(){
    make_map();
    dfs_mk_wall(0,0); //{0,0}์œ„์น˜ ๋ถ€ํ„ฐ 1๋กœ ๋ฒฝ ์„ธ์šฐ๊ธฐ
    cout << ans << endl;
    return 0;
}

๋Œ“๊ธ€