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

[BOJ S1][C++] ๋ฐฑ์ค€ 2583๋ฒˆ : ์˜์—ญ ๊ตฌํ•˜๊ธฐ

์„ ๋‹ฌ 2022. 3. 21. 16:53
๋ฐ˜์‘ํ˜•

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

 

2583๋ฒˆ: ์˜์—ญ ๊ตฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— M๊ณผ N, ๊ทธ๋ฆฌ๊ณ  K๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. M, N, K๋Š” ๋ชจ๋‘ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ K๊ฐœ์˜ ์ค„์—๋Š” ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ง์‚ฌ๊ฐํ˜•์˜ ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ ์˜ x, y์ขŒํ‘œ๊ฐ’๊ณผ ์˜ค

www.acmicpc.net

 

๋ฌธ์ œ

๋ˆˆ๊ธˆ์˜ ๊ฐ„๊ฒฉ์ด 1์ธ M×N(M,N≤100)ํฌ๊ธฐ์˜ ๋ชจ๋ˆˆ์ข…์ด๊ฐ€ ์žˆ๋‹ค. ์ด ๋ชจ๋ˆˆ์ข…์ด ์œ„์— ๋ˆˆ๊ธˆ์— ๋งž์ถ”์–ด K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ฆด ๋•Œ, ์ด๋“ค K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•์˜ ๋‚ด๋ถ€๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ๋ช‡ ๊ฐœ์˜ ๋ถ„๋ฆฌ๋œ ์˜์—ญ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง„๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด M=5, N=7 ์ธ ๋ชจ๋ˆˆ์ข…์ด ์œ„์— <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ง์‚ฌ๊ฐํ˜• 3๊ฐœ๋ฅผ ๊ทธ๋ ธ๋‹ค๋ฉด, ๊ทธ ๋‚˜๋จธ์ง€ ์˜์—ญ์€ <๊ทธ๋ฆผ 2>์™€ ๊ฐ™์ด 3๊ฐœ์˜ ๋ถ„๋ฆฌ๋œ ์˜์—ญ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๊ฒŒ ๋œ๋‹ค.

<๊ทธ๋ฆผ 2>์™€ ๊ฐ™์ด ๋ถ„๋ฆฌ๋œ ์„ธ ์˜์—ญ์˜ ๋„“์ด๋Š” ๊ฐ๊ฐ 1, 7, 13์ด ๋œ๋‹ค.

M, N๊ณผ K ๊ทธ๋ฆฌ๊ณ  K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•์˜ ์ขŒํ‘œ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ๋ช‡ ๊ฐœ์˜ ๋ถ„๋ฆฌ๋œ ์˜์—ญ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ๋ถ„๋ฆฌ๋œ ๊ฐ ์˜์—ญ์˜ ๋„“์ด๊ฐ€ ์–ผ๋งˆ์ธ์ง€๋ฅผ ๊ตฌํ•˜์—ฌ ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— M๊ณผ N, ๊ทธ๋ฆฌ๊ณ  K๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. M, N, K๋Š” ๋ชจ๋‘ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ K๊ฐœ์˜ ์ค„์—๋Š” ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ง์‚ฌ๊ฐํ˜•์˜ ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ ์˜ x, y์ขŒํ‘œ๊ฐ’๊ณผ ์˜ค๋ฅธ์ชฝ ์œ„ ๊ผญ์ง“์ ์˜ x, y์ขŒํ‘œ๊ฐ’์ด ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ชจ๋ˆˆ์ข…์ด์˜ ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ ์˜ ์ขŒํ‘œ๋Š” (0,0)์ด๊ณ , ์˜ค๋ฅธ์ชฝ ์œ„ ๊ผญ์ง“์ ์˜ ์ขŒํ‘œ๋Š”(N,M)์ด๋‹ค. ์ž…๋ ฅ๋˜๋Š” K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•๋“ค์ด ๋ชจ๋ˆˆ์ข…์ด ์ „์ฒด๋ฅผ ์ฑ„์šฐ๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ถ„๋ฆฌ๋˜์–ด ๋‚˜๋ˆ„์–ด์ง€๋Š” ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ์˜์—ญ์˜ ๋„“์ด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

bfs๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›์•„์„œ ์‚ฌ๊ฐํ˜•์„ ํ‘œ์‹œํ•˜๋Š”๊ฒƒ๋งŒ ์ž˜ ๊ณ ๋ คํ•˜๋ฉด ๋œ๋‹ค.

 

์ž…๋ ฅ์ด "์ขŒํ‘œ"๋กœ ์ฃผ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—
ํ‰์†Œ์— ์‚ฌ์šฉํ•˜๋Š” ํ–‰,์—ด ๊ฐ’๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ํ’€์–ด์•ผ ํ–ˆ๋Š”๋ฐ,

 

๋‚˜๋Š” ์•„๋ž˜ ๋ฐฉ์‹์œผ๋กœ ํ‘œ๋ฅผ ์ƒ๊ฐํ•ด์คฌ๋‹ค.

์ด๋•Œ x์ขŒํ‘œ๋Š” ์—ด์„, y์ขŒํ‘œ๋Š” ํ–‰์„ ์˜๋ฏธํ•œ๋‹ค.

 

    while(k--){
        int x1,y1,x2,y2;
        cin >> x1 >> y1 >> x2 >> y2;
        
        for(int i=y1; i<y2; i++)
            for(int j=x1; j<x2; j++)
                board[i][j] = 1;
    }

 

์ž…๋ ฅ๊ฐ’์„ ์ด์ฐจ์›๋ฐฐ์—ด์— ์ €์žฅํ• ๋•Œ ์‹ ๊ฒฝ์จ์„œ ์ €์žฅํ•ด์คฌ๋‹ค. 

 

// Authored by : seondal
// ํ’€์ด : https://whkakrkr.tistory.com/
// Co-authored by : -

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

using namespace std;

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

int board[101][101] = {0};

int main() {
    int m,n,k;
    cin >> m >> n >> k;
    
    while(k--){
        int x1,y1,x2,y2;
        cin >> x1 >> y1 >> x2 >> y2;
        
        for(int i=y1; i<y2; i++)
            for(int j=x1; j<x2; j++)
                board[i][j] = 1;
    }
    
    int cnt = 0;
    vector<int> areas;
    
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            
            if(board[i][j] == 1) continue;
           
            cnt ++;
            int area = 0;
            
            queue<pair<int,int>> q;
            q.push({i,j});
            board[i][j] = 1;
            
            while(!q.empty()){
                auto c = q.front();
                q.pop();
                area++;
                
                for(int dir=0; dir<4; dir++){
                    int x = c.first + dx[dir];
                    int y = c.second + dy[dir];
                    
                    if(x<0 || x>=m || y<0 || y>=n) continue;
                    if(board[x][y] == 1) continue;
                    
                    q.push({x,y});
                    board[x][y] = 1;
                }
            }
    
            areas.push_back(area);
        }
    }
    
    sort(areas.begin(), areas.end());
    cout << cnt << "\n";
    for(int i=0; i<areas.size(); i++) cout << areas[i] << " ";
        
    return 0;
}
    
/**/

 

๋ฐ˜์‘ํ˜•