๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ๐Ÿฃ: ๐’œ๐“๐‘”๐‘œ๐“‡๐’พ๐“‰๐’ฝ๐“‚/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด

BOJ2468 : ์•ˆ์ „์˜์—ญ (Silver 1)

 

์šฐ์„  ๋‚˜๋Š” make_safezone์œผ๋กœ safe_zone ๋ฐฐ์—ด์— ์นจ์ˆ˜๋˜๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.

rain_height๋ณด๋‹ค ๋†’์ด๊ฐ€ ๋†’์œผ๋ฉด 1, ๋‚ฎ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด 0์œผ๋กœ ์˜์—ญ์ด 1๋กœ ๊ฐ€์‹œ์ ์œผ๋กœ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค์–ด์คŒ

 

๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ count_safezone ํ•ด์„œ ๋งŒ๋“ค์–ด ๋‘” safezone์˜ ์˜์—ญ ํฌ๊ธฐ๋ฅผ DFS๋ฅผ ํ™œ์šฉํ•ด ์„ธ์–ด์ฃผ์—ˆ๋‹ค.

 

์ด ๊ณผ์ •์„ rainheight 0๋ถ€ํ„ฐ 100๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด ์ตœ๋Œ“๊ฐ’์„ ์ฐพ์•„์ฃผ๋ฉด ๋!

 

 

#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

int height[100][100] = {0,};
int safe_zone[100][100] = {0,};


int N = 0; 

void make_safezone(int rain_height);
int count_safezone();
void DFS(int a, int b);

int main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   int rain_height = 0;
   int i, j =0;
   int m, n ;
   int max_safezone = 0;
   cin >> N;
   
   for(i=0; i<N; i++){
    for(j=0; j<N; j++){
        cin >> height[i][j];
    }
   }
    for(rain_height=0; rain_height<=100; rain_height++){
        make_safezone(rain_height);
                // cout << "\n safezone \n";
                // for(n=0; n<N; n++){
                //     for(m=0;m<N;m++){
                //         cout<<safe_zone[n][m]<<" ";
                //     }
                //     cout <<"\n";
                // }
        int num_safezone = count_safezone();
        if (max_safezone < num_safezone)
            max_safezone = num_safezone;
    }
    cout << max_safezone;
}

void make_safezone(int rain_height){
    int i, j = 0;

    for(i=0; i<N; i++){
        for(j=0; j<N; j++){
            if(height[i][j] > rain_height)
                safe_zone[i][j] = 1;
            else
                safe_zone[i][j] = 0;
        }
    }
}

int check[100][100] = {0,};
int count_safezone(){
    int i, j=0;
    int n,m;
    int count = 0;
    memset(check, 0, sizeof(check));
    for(i = 0; i<N; i++){
        for(j=0; j<N; j++){
            if(safe_zone[i][j] && !check[i][j]){
                DFS(i, j);
                // cout << "\n DFS CHECK \n";
                // for(n=0; n<N; n++){
                //     for(m=0;m<N;m++){
                //         cout<<check[n][m]<<" ";
                //     }
                //     cout <<"\n";
                // }
                count ++;
            }
        }
    }
    return count;
}

void DFS (int a, int b){
    if(check[a][b]==1 || safe_zone[a][b] == 0){
        return;
    }
    check[a][b] = 1;

    if(b<N-1) DFS(a, b+1);
    if(a<N-1) DFS(a+1, b);
    if(a>0) DFS(a-1,b);
    if(b>0) DFS(a,b-1);
}

 

๋‚˜์˜ ์ฒซ ๋ฒˆ์งธ ์‹ค์ˆ˜ - DFS if ๋ฌธ ์กฐ๊ฑด ์‹ค์ˆ˜

 

์ด๊ฑด ๊ฑ ๋‚ด๊ฐ€ ๋˜ฅ๋ฉ์ฒญ์ด ใ… ใ… 

 

๋‘ ๋ฒˆ์งธ ์‹ค์ˆ˜ - ๋น„๊ฐ€ ์•ˆ ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์บ์น˜ํ•˜์ง€ ๋ชปํ•˜์˜€๋‹ค

๋น„๊ฐ€ ์•ˆ์˜ฌ์ˆ˜๋„ ์žˆ์–ด์„œ rain_height์˜ ์‹œ์ž‘์ด 0์ด์˜€์–ด์•ผ ํ–ˆ๋Š”๋ฐ, 1๋ถ€ํ„ฐ ํ•ด์„œ ํ•œ์ฐธ ํ•ด๋งด ใ… ใ… 

๋‹คํ–‰ํžˆ ์ธํ„ฐ๋„ท์— ๋ฐ”๋กœ ์ฐพ์œผ๋‹ˆ๊นŒ ๋– ์„œ ๊ณ ์ณค๋‹ค

 

 

 

๊ณ ๋‚œ๊ณผ ์—ญ๊ฒฝ..