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

[๋ฐฑ์ค€] ์น˜์ฆˆ 2636 bfs

by Jouureee 2021. 4. 2.

๋ฌธ์ œ :

www.acmicpc.net/problem/2636

 

2636๋ฒˆ: ์น˜์ฆˆ

์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํŒ์ด ์žˆ๊ณ , ๊ทธ ์œ„์— ์–‡์€ ์น˜์ฆˆ(ํšŒ์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„)๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค. ํŒ์˜ ๊ฐ€์žฅ์ž๋ฆฌ(<๊ทธ๋ฆผ 1>์—์„œ ๋„ค๋ชจ ์นธ์— X์นœ ๋ถ€๋ถ„)์—๋Š” ์น˜์ฆˆ๊ฐ€ ๋†“

www.acmicpc.net

 

๋ถ„์„ :

์ด ๋ฌธ์ œ๋Š” ์ƒ๊ฐํ•˜๊ธฐ ๋ณต์žกํ–ˆ์ง€๋งŒ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋‹จ์ˆจ์— ์ดํ•ด๊ฐ€ ๊ฐ€๋Š” ๋“ฏ ํ•œ ๋ฌธ์ œ์˜€๋‹ค.

์น˜์ฆˆ์˜ ๊ฒ‰๋ฉด๋งŒ ์‹œ๊ฐ„์— ๋”ฐ๋ผ ์–ด๋–ป๊ฒŒ ๊ณต๊ธฐ๋กœ ๋ฐ”๊ฟ€๊นŒ ๋ฌธ์ œ ๊ทธ๋Œ€๋กœ์˜ ํ๋ฆ„์„ ๋”ฐ๋ผ ๋ฌธ์ œ๋ฅผ ํ’€๋ ค ํ–ˆ์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด์€ ์•ˆ์ชฝ์˜ ๊ณต๊ธฐ์™€ ๋ฐ”๊นฅ์ชฝ์˜ ๊ณต๊ธฐ๊ฐ€ ๊ตฌ๋ถ„์ด ๋˜์ง€ ์•Š์•„ ์ˆœ์„œ๊ฐ€ ์—‰๋ง์ด ๋  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค. 

๊ฐ™์ด ์Šคํ„ฐ๋”” ํ•˜๋Š” ์–ธ๋‹ˆ์˜ ์ฝ”๋“œ ์„ค๋ช…์œผ๋กœ ์น˜์ฆˆ ์ž…์žฅ์—์„œ๊ฐ€ ์•„๋‹Œ ๊ณต๊ธฐ ์ž…์žฅ์—์„œ ๋‹ค์Œ ๊ณต๊ธฐ๊ฐ€ ๋  ๊ฒƒ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•˜๋ฉด ์•ˆ์ชฝ์˜ ๊ณต๊ธฐ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์ด ์‹ ๊ธฐํ–ˆ๋‹ค. ๋…น๋Š” ์ˆœ์„œ๋Š” ๊ณต๊ธฐ์— ๋‹ฟ๋Š” ์ˆœ์„œ์ด๋ฏ€๋กœ ๋จผ์ € ์•ˆ์ชฝ ๊ณต๊ธฐ๋ฅผ ๋งŒ๋‚ ์ผ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

enum์„ ์‚ฌ์šฉํ•ด์„œ ๋”์šฑ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ–ˆ๋”๋ผ ์ตœ๊ณ ๋‹ค ์ตœ๊ณ  

 

find_air ํ•จ์ˆ˜๊ฐ€ false๊ฐ€ ๋  ์กฐ๊ฑด์€ ๋‚จ์•„์žˆ๋Š” ์น˜์ฆˆ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š์ง€ melt_bfs๋กœ ๊ฒ€์‚ฌํ•˜์—ฌ ํŒ๋ณ„ํ•œ๋‹ค. ํ•˜๋‚˜๋ผ๋„ 1 cheese๊ฐ€ ๋‚จ์•„์žˆ์„ ๊ฒฝ์šฐ while๋ฌธ์„ ๋Œ๋ฉฐ cheese๋ฅผ ๋…น์ธ๋‹ค. 

 

c++ ์ฝ”๋“œ :

 

//
//  2636_cheese.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/01.
//

#include <stdio.h>
#include <iostream>
#include <queue>
#include <cstring>


using namespace std;
int arr[101][101];
int visit[101][101];
int cheese_cnt = 0;
int ans = 1;
int M, N;

int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };

enum { CHEESE = 1, PREAIR, AIR};

bool melt_bfs(){
    bool cleared = true;
    for(int i = 0; i < M; i++){
        for(int j = 0; j < N; j++){
            if(arr[i][j] == PREAIR){
                arr[i][j] = AIR; // ๊ณต๊ธฐ๋กœ ๋ฐ”๊ฟ”์คŒ
            }
        }
    }
    
    for(int i = 0; i < M; i++){
        for(int j = 0; j < N; j++){
            if(arr[i][j] == CHEESE){//์น˜์ฆˆ๊ฐ€ ์กฐ๊ธˆ์ด๋ผ๋„ ๋‚จ์•˜๋‹ค๋ฉด false
                cleared = false;
            }
        }
    }
    return cleared;

    
}

bool find_air(){
    queue<pair<int, int>> q;
    q.push(make_pair(0,0));
    memset(visit, false, sizeof(visit));
    while(!q.empty()){
        int y = q.front().first;
        int x = q.front().second;
        
        arr[y][x] = AIR;
        q.pop();
        for(int i = 0; i < 4; i ++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 || nx < 0 || ny >= M || nx >= N || visit[ny][nx])
                continue;
            if(arr[ny][nx] == CHEESE){ // ๊ทผ์ฒ˜ ์น˜์ฆˆ๋ฅผ๋ฐœ๊ฒฌํ•˜๋ฉด preair๋กœ ๋„ฃ์–ด์ค€๋‹ค ..!
                arr[ny][nx] = PREAIR;
                cheese_cnt++;
            }else if (arr[ny][nx] == 0 || arr[ny][nx] == AIR){
                q.push(make_pair(ny, nx)); // ๊ณต๊ธฐ์— ๋Œ€ํ•ด q์— ๋„ฃ์–ด์ค€๋‹ค.
                visit[ny][nx] = true;
            }
            
        }
    }
    return melt_bfs();
}

int main(){
    cin >> M >> N;
    for(int i = 0; i < M; i++){
        int s;
        for(int j = 0; j < N; j++){
            cin >> s;
            arr[i][j] = s;
        }
    }
    
//    for(int i = 0 ; i < M; i++){ //input ํ™•์ธ์šฉ
//        for(int j = 0; j < N; j++){
//            cout << arr[i][j] << " ";
//        }
//        cout << endl;
//    }
    
    while(1){
        cheese_cnt = 0;
        if(find_air())
            break;
        ans++;
    }
    
    cout << ans << '\n';
    cout << cheese_cnt << '\n';
    
    melt_bfs();
    
    
    return 0;
}

 

๋Œ“๊ธ€