๐Ÿ• Baaaaaarking/0x09๊ฐ• - BFS

[BOJ S1][C++] ๋ฐฑ์ค€ 1926๋ฒˆ : ๊ทธ๋ฆผ

์„ ๋‹ฌ 2022. 3. 6. 17:51
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/1926

 

1926๋ฒˆ: ๊ทธ๋ฆผ

์–ด๋–ค ํฐ ๋„ํ™”์ง€์— ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ ธ ์žˆ์„ ๋•Œ, ๊ทธ ๊ทธ๋ฆผ์˜ ๊ฐœ์ˆ˜์™€, ๊ทธ ๊ทธ๋ฆผ ์ค‘ ๋„“์ด๊ฐ€ ๊ฐ€์žฅ ๋„“์€ ๊ฒƒ์˜ ๋„“์ด๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ๋ผ. ๋‹จ, ๊ทธ๋ฆผ์ด๋ผ๋Š” ๊ฒƒ์€ 1๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ํ•œ ๊ทธ๋ฆผ์ด๋ผ๊ณ  ์ •์˜ํ•˜์ž. ๊ฐ€๋กœ๋‚˜ ์„ธ๋กœ

www.acmicpc.net

 

๋ฌธ์ œ

์–ด๋–ค ํฐ ๋„ํ™”์ง€์— ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ ธ ์žˆ์„ ๋•Œ, ๊ทธ ๊ทธ๋ฆผ์˜ ๊ฐœ์ˆ˜์™€, ๊ทธ ๊ทธ๋ฆผ ์ค‘ ๋„“์ด๊ฐ€ ๊ฐ€์žฅ ๋„“์€ ๊ฒƒ์˜ ๋„“์ด๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ๋ผ. ๋‹จ, ๊ทธ๋ฆผ์ด๋ผ๋Š” ๊ฒƒ์€ 1๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์„ ํ•œ ๊ทธ๋ฆผ์ด๋ผ๊ณ  ์ •์˜ํ•˜์ž. ๊ฐ€๋กœ๋‚˜ ์„ธ๋กœ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒƒ์€ ์—ฐ๊ฒฐ์ด ๋œ ๊ฒƒ์ด๊ณ  ๋Œ€๊ฐ์„ ์œผ๋กœ ์—ฐ๊ฒฐ์ด ๋œ ๊ฒƒ์€ ๋–จ์–ด์ง„ ๊ทธ๋ฆผ์ด๋‹ค. ๊ทธ๋ฆผ์˜ ๋„“์ด๋ž€ ๊ทธ๋ฆผ์— ํฌํ•จ๋œ 1์˜ ๊ฐœ์ˆ˜์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋„ํ™”์ง€์˜ ์„ธ๋กœ ํฌ๊ธฐ n(1 ≤ n ≤ 500)๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ m(1 ≤ m ≤ 500)์ด ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ n+1 ์ค„ ๊นŒ์ง€ ๊ทธ๋ฆผ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (๋‹จ ๊ทธ๋ฆผ์˜ ์ •๋ณด๋Š” 0๊ณผ 1์ด ๊ณต๋ฐฑ์„ ๋‘๊ณ  ์ฃผ์–ด์ง€๋ฉฐ, 0์€ ์ƒ‰์น ์ด ์•ˆ๋œ ๋ถ€๋ถ„, 1์€ ์ƒ‰์น ์ด ๋œ ๋ถ€๋ถ„์„ ์˜๋ฏธํ•œ๋‹ค)

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ๊ทธ๋ฆผ์˜ ๊ฐœ์ˆ˜, ๋‘˜์งธ ์ค„์—๋Š” ๊ทธ ์ค‘ ๊ฐ€์žฅ ๋„“์€ ๊ทธ๋ฆผ์˜ ๋„“์ด๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ๋ผ. ๋‹จ, ๊ทธ๋ฆผ์ด ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๋„“์€ ๊ทธ๋ฆผ์˜ ๋„“์ด๋Š” 0์ด๋‹ค.

 

ํ’€์ด

BFS ๊ธฐ๋ณธ ํ‹€์„ ์™ธ์›Œ๋‘๋Š” ์Šต๊ด€์„ ๊ฐ€์ง€์ž

https://www.youtube.com/watch?v=ftOmGdm95XI&list=PLtqbFd2VIQv4O6D6l9HcD732hdrnYb6CY&index=10 

// Authored by : seondal
// Co-authored by : -

//#include <bits/stdc++.h>
#include <iostream>
#include <queue>

using namespace std;

int board[502][502];
int vis[502][502];

int biggest, num; // ๊ฐ๊ฐ ๊ทธ๋ฆผ์˜ ์ตœ๋Œ€ ๋„“์ด์™€ ๊ทธ๋ฆผ์˜ ๊ฐฏ์ˆ˜

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

int main() {
    // ์ž…๋ ฅ
    int n,m;
    cin >> n >> m;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin >> board[i][j];
        }
    }
    
    // ๊ณ„์‚ฐ
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            
            if(board[i][j] == 0 || vis[i][j] == 1) continue; // ๊ทธ๋ฆผ์ด ์•„๋‹ˆ๊ฑฐ๋‚˜ ๋ฐฉ๋ฌธ์ด ์ด๋ฏธ ๋˜์—ˆ์œผ๋ฉด ํŒจ์Šค
            
            num++; // ๊ทธ๋ฆผ ๊ฐฏ์ˆ˜ ์ฆ๊ฐ€
            queue<pair<int, int>> q;
            vis[i][j] = 1;  // ๋ฐฉ๋ฌธ ํ‘œ์‹œ
            q.push({i,j});
            int area = 0;
            
            while(!q.empty()){
                area++;
                
                pair<int, int> current = q.front();
                q.pop();
                
                for(int dir=0; dir<4; dir++) {
                    int x = current.first + dx[dir];
                    int y = current.second + dy[dir];
                    
                    if(x < 0 || x >= n || y < 0 || y >= m) continue;  // ์˜์—ญ ๋ฐ–์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค๋ฉด ํŒจ์Šค
                    if(vis[x][y] == 1 || board[x][y] == 0) continue;  // ์ด๋ฏธ ๋ฐฉ๋ฌธ๋˜์—ˆ๊ฑฐ๋‚˜ ๊ทธ๋ฆผ์ด ์•„๋‹ˆ๋ผ๋ฉด ํŒจ์Šค
                    
                    // ์กฐ๊ฑด์— ๋งž๋Š”๋‹ค๋ฉด ๋ฐฉ๋ฌธ ํ‘œ์‹œ ํ›„ push
                    vis[x][y] = 1;
                    q.push({x,y});
                }
            }
            if(biggest < area) biggest = area;
        }
    }
    
    // ์ถœ๋ ฅ
    cout << num << '\n' << biggest;
    
    return 0;
}
/*
 */

 

 

 

๋ฐ˜์‘ํ˜•